我需要修复postgres查询的结果

时间:2017-05-15 15:13:03

标签: postgresql

我试图过滤一个数组子句,每个"日期(t1.starttime)"只有一行。当前返回为数组中的每个项创建一行。我尝试在t1.starttime上选择distinct,但这只是从第一个t3.store返回结果。

SELECT
t2.name,
date (t1.starttime),
Count(t1.starttime) as "# Plays",
Count(distinct(t1.playerid)) as "# Stores"

FROM
public.billing t1 JOIN public.medias t2 on t1.mediaitemid = t2.id 
JOIN public.players t3 on t1.playerid = t3.id 

WHERE
t3.store in  ('0038','3285') and
t2.name LIKE any (array['COMM_7032%']) and
t1.starttime >= date('2017-04-26') and 
t1.starttime < date('2017-05-01') and
t1.channelname = 'myNetwork'

GROUP BY
date (t1.starttime),
t2.name,
t1.playerid

ORDER BY
date (t1.starttime)

返回数据......

"COMM_7032";"2017-04-27";20;1
"COMM_7032";"2017-04-27";20;1
"COMM_7032";"2017-04-28";34;1
"COMM_7032";"2017-04-28";34;1
"COMM_7032";"2017-04-29";35;1
"COMM_7032";"2017-04-29";35;1
"COMM_7032";"2017-04-30";21;1
"COMM_7032";"2017-04-30";22;1

尝试合并日期,使其显示如下......

"COMM_7032";"2017-04-27";40;2 
"COMM_7032";"2017-04-28";68;2 
"COMM_7032";"2017-04-29";70;2 
"COMM_7032";"2017-04-30";43;2

1 个答案:

答案 0 :(得分:0)

如果适合,请试试这个:

SELECT
array_agg(t2.name),
array_agg(t1.playerid),
date (t1.starttime),
Count(t1.starttime) as "# Plays",
Count(distinct(t1.playerid)) as "# Stores"

FROM
public.billing t1 JOIN public.medias t2 on t1.mediaitemid = t2.id 
JOIN public.players t3 on t1.playerid = t3.id 

WHERE
t3.store in  ('0038','3285') and
t2.name LIKE any (array['COMM_7032%']) and
t1.starttime >= date('2017-04-26') and 
t1.starttime < date('2017-05-01') and
t1.channelname = 'myNetwork'

GROUP BY
date (t1.starttime)

ORDER BY
date (t1.starttime)