sql - is it possible to self join to ask for different dates? -


tried search answer, reading posts this: sql self-join data comparison different days not able quite understand how work in scenario.

would appreciate help;

i have table

  • userid (number)
  • usertype (string, shows if member or guest)

  • sales_date (datestamp field)

  • (plus other columns bought , cost of item not interested in right now)

i trying write query tell me how many people went between being member , being guest, per month. can answer questions "how many people here in september , came in october?" "how many people members in september downgraded become guests in october?" "how many people guests in september upgraded being members in october?"

1: self-join way go when needing ask 2 different date ranges same table/same query?

2: thinking need ask userid,then usertype sept vs usertype october. sound right? not sure how ask 2 different dates

select       t1.userid,       t1.usertype usertypesept,       t2.usertype usertypeoct           my_table t1          join my_table t2             on t1.userid = t2.userid            , t2.day > '2015-01-01' , t2.day < '2015-02-01'          t1.day  >'2015-02-01' , t1.day <'2015-03-01' ; 

am thinking along right lanes? if works, not tell me how many changed "members" "guests" sept-october, @ least show values in 2 different columns

thanks

1: self-join way go when needing ask 2 different date ranges same table/same query?

not really! depends! in case - see below #2

2: thinking need ask userid,then usertype sept vs usertype october

i think below expect.
please note: looks usertype @ end of each month , uses usertype respective month.

/* my_table (   select 1 userid, 'member' usertype, timestamp '2015-09-01' sales_date union   select 1 userid, 'member' usertype, timestamp '2015-09-02' sales_date union   select 1 userid, 'member' usertype, timestamp '2015-09-03' sales_date union   select 1 userid,  'guest' usertype, timestamp '2015-09-10' sales_date union   select 1 userid,  'guest' usertype, timestamp '2015-10-01' sales_date union   select 1 userid,  'guest' usertype, timestamp '2015-10-02' sales_date union   select 2 userid,  'guest' usertype, timestamp '2015-09-01' sales_date union   select 2 userid, 'member' usertype, timestamp '2015-10-01' sales_date union   select 3 userid,  'guest' usertype, timestamp '2015-09-01' sales_date union   select 3 userid,  'guest' usertype, timestamp '2015-10-01' sales_date union   select 4 userid,  'guest' usertype, timestamp '2015-09-01' sales_date union   select 4 userid, 'member' usertype, timestamp '2015-10-01' sales_date )  */ select    userid,   max(case when sales_year_month = '2015-09' usertypeatendofmonth end) usertypesept,   max(case when sales_year_month = '2015-10' usertypeatendofmonth end) usertypeoct (   select      userid,      format_date('%y-%m', date(sales_date)) sales_year_month,     array_agg(usertype order sales_date desc limit 1)[offset(0)] usertypeatendofmonth   my_table    group 1, 2 ) group 1 

you can remove comments if want test on sample data


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -