mysql - SQL query for displaying records on Highcharts graph -


i working on achieve dynamic highcharts graph – basic column , need in making sql in mysql. need results last 12 months (irrespective of data month (it can 0 – 12 months records should fetched)) current month showing how many members (4 types of users) have registered on site particular month.

there 4 types of users:

  1. agents
  2. individuals
  3. builders
  4. real estate companies

for months column should retrieve last 12 months current month - aug, sept, oct, nov, dec, jan, feb, mar, apr, may, jun, jul.

i have tried following query:

select case when u.`usertypeid`=1 count(`usertypeid`) else 0 end agent, case when u.`usertypeid`=2 count(`usertypeid`) else 0 end individuals, case when u.`usertypeid`=3 count(`usertypeid`) else 0 end builders, case when u.`usertypeid`=4 count(`usertypeid`) else 0 end real_estate_companies,  u.`userregistreddate` 'timestamp' `dp_users` u left join `dp_user_types` ut on u.`usertypeid` = ut.`type_id` u.`userregistreddate` < now( ) , u.`userregistreddate` > date_add( now( ) , interval -12 month ) group date_format( u.`userregistreddate`, '%b' ) 

output (incorrect):

| agent | individuals | builders | real_estate_companies |                   timestamp | ---------------------------------------------------------------------------------------- |     0 |           0 |        9 |                     0 | july, 01 2013 17:14:35+0000 | |     3 |           0 |        0 |                     0 |  may, 15 2013 14:14:26+0000 | 

output (required: correct):

| agent | individuals | builders | real_estate_companies |                   timestamp | ---------------------------------------------------------------------------------------- |     3 |           2 |        2 |                     2 | july, 01 2013 17:14:35+0000 | |     1 |           2 |        0 |                     0 |  may, 15 2013 14:14:26+0000 | 

another way tried sub-query, please find both examples links below:

http://sqlfiddle.com/#!2/ed101/53 http://sqlfiddle.com/#!2/ed101/54

hoping find favorable solution, thanks.

try this

    select month(u.`userregistreddate`), sum(case when u.`usertypeid`=1 1 else 0 end) agent, sum(case when u.`usertypeid`=2 1 else 0 end) individuals, sum(case when u.`usertypeid`=3 1 else 0 end) builders, sum(case when u.`usertypeid`=4 1 else 0 end) real_estate_companies,  u.`userregistreddate` 'timestamp',m.month ( select 'january' month union select 'february' month union select 'march' month union select 'april' month union select 'may' month union select 'june' month union select 'july' month union select 'august' month union select 'september' month union select 'october' month union select 'november' month union select 'december' month ) m left join `dp_users` u  on m.month = monthname(u.`userregistreddate`) , u.`userregistreddate` < now( ) , u.`userregistreddate` > date_add( now( ) , interval -12 month ) left join `dp_user_types` ut on u.`usertypeid` = ut.`type_id`  group  m.month order field(m.month,'july','august','september','october','november','december','january','february','march','april','may','june') 

please check out link


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

c++ - End of file on pipe magic during open -