如何仅按一列分组?

时间:2019-04-09 10:08:08

标签: sql postgresql

我只想选择一个列(Failed_operation)和带有隐藏列的唯一列(SN),如下面的代码所示,但出现错误 错误:“ rw_pcba.sn”列必须出现在GROUP BY子句中或在聚合函数中使用

我尝试删除(SN)上的非重复字符,然后出现了结果,但结果也包括重复的SN。我不希望结果中出现重复的SN。

SELECT DISTINCT ON (sn) Failed_operation
    ,count(CASE WHEN (extract(day FROM NOW() - fail_timestamp)) > 0
                AND (extract(day FROM NOW() - fail_timestamp)) <= 15 THEN 1 ELSE NULL END) AS AgingLessThan15
    ,count(CASE WHEN (extract(day FROM NOW() - fail_timestamp)) > 15
                AND (extract(day FROM NOW() - fail_timestamp)) <= 30 THEN 1 ELSE NULL END) AS Aging16To30
    ,count(CASE WHEN (extract(day FROM NOW() - fail_timestamp)) > 30
                AND (extract(day FROM NOW() - fail_timestamp)) <= 60 THEN 1 ELSE NULL END) AS Aging31To60
    ,count(CASE WHEN (extract(day FROM NOW() - fail_timestamp)) > 60 THEN 1 ELSE NULL END) AS AgingGreaterThan60
    ,count(CASE WHEN (extract(day FROM NOW() - fail_timestamp)) <= 0 THEN 1 ELSE NULL END) AS Aging0
FROM rw_pcba
WHERE rework_status = 'In-Process'
GROUP BY Failed_operation
ORDER BY sn
    ,Failed_operation ASC

2 个答案:

答案 0 :(得分:1)

您需要使用列sn进行分组,当您使用分组依据时,这将是sn和failed_operation的独特组合,您不必指定distinct。

SELECT sn, Failed_operation,
count  (case when (extract(day from NOW() - fail_timestamp)) >0 and (extract(day from NOW() - fail_timestamp))<=15 then 1 else null end) as AgingLessThan15,
count  (case when (extract(day from NOW() - fail_timestamp)) >15 and (extract(day from NOW() - fail_timestamp))<=30 then 1 else null end) as Aging16To30,
count  (case when (extract(day from NOW() - fail_timestamp)) >30 and (extract(day from NOW() - fail_timestamp))<=60 then 1 else null end) as Aging31To60,
count  (case when (extract(day from NOW() - fail_timestamp)) >60 then 1 else null end) as AgingGreaterThan60,
count  (case when (extract(day from NOW() - fail_timestamp)) <=0 then 1 else null end) as Aging0
FROM rw_pcba where rework_status='In-Process' 
GROUP by sn,Failed_operation  ORDER BY  sn,Failed_operation ASC

答案 1 :(得分:0)

您要按snfailed_operation进行汇总。我还认为您可以简化每列的计算:

SELECT sn, Failed_operation,
       count(*) filter (where fail_timestamp > current_date and fail_timestamp < current_date + interval '15 day') as AgingLessThan15,
       count(*) filter (where fail_timestamp > current_date + interval '15 day' and fail_timestamp < current_date + interval '30 day') as Aging16To30,
       count(*) filter (where fail_timestamp > current_date + interval '30 day' and fail_timestamp < current_date + interval '600 day') as Aging31To60,
       count(*) filter (where fail_timestamp > current_date + interval '60 day') as AgingGreaterThan60,
       count(*) filter (where fail_timestamp <= current_date) as Aging0
FROM rw_pcba
WHERE rework_status = 'In-Process'
GROUP BY sn, Failed_operation
ORDER BY sn, Failed_operation ASC;

对于这种类型的逻辑,我更喜欢直接进行日期比较,而不是处理日期之间的差异。我只是发现更容易遵循。例如,使用current_date而不是now()消除了now()的时间部分发生什么的问题。

编辑:

在较旧的Postgres版本中,您可以使用sum来表达这一点:

       sum( (fail_timestamp > current_date and fail_timestamp < current_date + interval '15 day')::int ) as AgingLessThan15,
相关问题