sql查询数据格式

时间:2017-08-31 19:42:44

标签: mysql sql

这是我对数据的查询,如:

select 
    ('moviestimeidea') as 'circle',
    count(distinct msisdn),
    vendor,
    hour(proess_datetime) 
from 
    campaing_info 
where 
    date(proess_datetime) = curdate() 
    and length(msisdn) > 4 
group by 
    vendor,
    hour(proess_datetime) 
order by 
    hour(proess_datetime);

返回此结果:

circle            count(distinct ani)    vendor    hour(date_time) 
------------------------------------------------------------------
xyz               1                      default   0
xyx               5                      a         0
xyx               5                      b         1
xyx               27                     c         1
xyx               5                      a         3
xyx               54                     b         3
xyx               57                     c         4
xyx               5                      d         5

但我想要这个结果:

 circle            vendors            hour(date_time) 
 ----------------------------------------------------
 -            default a   b c d          -
xyx               1   -   - - -          0
xyx               -   5   - - -          0
xyx               -   -   5 - -          1
xyx               -   -   - - -          1
xyx               -  54   - - -          3
xyx               -   -  54 - -          3
xyx               -   -   - - -          4
xyx               -   -   - - -          5

喜欢c和d供应商的数据请帮忙

1 个答案:

答案 0 :(得分:0)

select
    hour(date_add(tbl.date_time,interval 335 minute)) as 'hour'
    ,count(distinct tbl1.ani) as 'XYZ' 
    ,count(distinct tbl2.ani) as 'ASDF' 
    ,count(distinct tbl3.ani) as 'FGH'
    ,count(distinct tbl4.ani) as 'HJK'
    ,count(distinct tbl5.ani) as 'KLO'
    from TABLE NAME tbl
    left outer join TABLE NAME tbl1 on tbl.id=tbl1.id  and tbl1.vendor='XYZ'
    left outer join TABLE NAME tbl2 on tbl.id=tbl2.id  and tbl2.vendor='ASDF'
    left outer join TABLE NAMEl tbl3 on tbl.id=tbl3.id  and tbl3.vendor='FGH'
    left outer join TABLE NAME tbl4 on tbl.id=tbl4.id  and tbl4.vendor='HJK'
    left outer join TABLE NAME tbl5 on tbl.id=tbl5.id  and tbl5.vendor='KLO'
    Where date(tbl.date_time)=date(date_add(now(),interval 335 minute)) group by hour(date_add(tbl.date_time,interval 335 minute));