![](/img/trans.png)
[英]SQL One to Many Relationship - Query to retrieve data in JSON format
[英]sql query for data format
这是我对数据的查询就像:
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供应商的数据,请帮助
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.