MySQL pivot query -


i used have query

select ps_target_ecpm, ps_actual_ecpm publisher_stats join domain on domain.dmn_id = ps_dmn_id left join langue on langue.lng_id = domain.default_lng_id month(ps_month) = 05  

the result need should

may_target_ecmp, may_actual_ecpm, april_target_ecpm, april_actual_ecpm, march_target_ecpm, march_actual_ecpm.

for april month(ps_month) = 04 , march month(ps_month) = 03 respectively.

after questioning around ended query looks this

select (case when month(ps_month) = 4 ps_target_ecpm else 0 end) april_target_ecpm,  (case when month(ps_month) = 4 ps_actual_ecpm else 0 end) april_actual_ecpm, (case when month(ps_month) = 3 ps_target_ecpm else 0 end) march_target_ecpm,  (case when month(ps_month) = 3 ps_actual_ecpm else 0 end) march_actual_ecpm  publisher_stats join domain on domain.dmn_id = ps_dmn_id left join langue on langue.lng_id = domain.default_lng_id 

the resultset not quite need. example response is:

0           0       0.48    0.27 0.48        0.47    0       0 

while need in 1 row

0.48    0.47    0.48    0.27 

could please me figure out how make query intended to. in advance

p.s. question come way question - mysql pivoting - how can fetch data same table different columns?

just use aggregate function, max example work fine, might need use sum if need total each month, if there multiple entries ps_target_ecpm each month. this:

select   max(case when month(ps_month) = 4 ps_target_ecpm else 0 end) april_target_ecpm,    max(case when month(ps_month) = 4 ps_actual_ecpm else 0 end) april_actual_ecpm,   max(case when month(ps_month) = 3 ps_target_ecpm else 0 end) march_target_ecpm,    max(case when month(ps_month) = 3 ps_actual_ecpm else 0 end) march_actual_ecpm  publisher_stats join domain on domain.dmn_id = ps_dmn_id left join langue on langue.lng_id = domain.default_lng_id 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -