简体   繁体   English

如何用计数值和伪值聚合联合表?

[英]How to aggregate unioned tables with count and dummy values?

I am counting in two tables some stuff and want one aggregated result (one row). 我在两张桌子中计数一些东西,想要一个汇总结果(一行)。 I write this SQL for this purpose: 我为此编写此SQL:


SELECT  sum (Amount_New) Amount_New, 
        sum(Import_Dropout) Import_Dropout, 
        sum(Import)Import, 
        sum(Processing)Processing, 
        sum(Processing_Dropout)Processing_Dropout, 
        sum(Matching)Matching, 
        sum(Matching_Dropout)Matching_Dropout, 
        sum(Export)Export,
        sum(Exported)Exported, 
        sum(Rejected)Rejected, 
        sum(AmountSubTotal)AmountSubTotal, 
        sum(AmountTotal)AmountTotal
FROM (

SELECT  COUNT(CASE WHEN ProcessStatus='_New' THEN 1 ELSE null END) AS Amount_New,
        COUNT(CASE WHEN ProcessStatus='Import_Dropout' THEN 1 ELSE null END) AS Import_Dropout,
        COUNT(CASE WHEN ProcessStatus='Import' THEN 1 ELSE null END) AS Import,
        COUNT(CASE WHEN ProcessStatus='Processing' THEN 1 ELSE null END) AS Processing,
        COUNT(*) AS AmountTotal,
        0 as Processing_Dropout, 
        0 as Matching, 
        0 as Matching_Dropout, 
        0 as Export, 
        0 as Export_Dropout, 
        0 as Exported, 
        0 as Rejected, 
        0 as AmountSubTotal, 
        0 as UnionOrder
FROM "fileimport$marketscanimportcsv" 
WHERE  ft_boekdat like '%2018%'

UNION 

SELECT  0 AS Amount_New, 
        0 AS Import_Dropout, 
        0 AS Import, 
        COUNT(CASE WHEN ProcessStatus='Processing_Dropout' THEN 1 ELSE null END) AS Processing_Dropout,
        COUNT(CASE WHEN ProcessStatus='Processing' THEN 1 ELSE null END) AS Processing,
        COUNT(CASE WHEN ProcessStatus='Matching' THEN 1 ELSE null END) AS Matching,
        COUNT(CASE WHEN ProcessStatus='Matching_Dropout' THEN 1 ELSE null END) AS Matching_Dropout,
        COUNT(CASE WHEN ProcessStatus='Export' THEN 1 ELSE null END) AS Export,
        COUNT(CASE WHEN ProcessStatus='Export_Dropout' THEN 1 ELSE null END) AS Export_Dropout,
        COUNT(CASE WHEN ProcessStatus='Exported' THEN 1 ELSE null END) AS Exported,
        COUNT(CASE WHEN ProcessStatus='Rejected' THEN 1 ELSE null END) AS Rejected,
        COUNT(CASE WHEN ProcessStatus!= '_New' and ProcessStatus!= 'Import_Dropout' and ProcessStatus!= 'Import'  THEN 1 ELSE null END) AS AmountSubTotal,
        COUNT(*) AS AmountTotal,
        1 as UnionOrder
FROM "matching$marketscanmovement" 
WHERE date_part ('year', BookingDate_BA_MS)= 2018 

)  SK GROUP by unionorder order by unionorder asc 

1) The result is 2 rows, which is not one value as total of that column. 1)结果是2行,这不是该列总计的一个值。 Why is this query not summarizing the unioned same column values? 为什么此查询未汇总联合的相同列值? How should it be written? 应该怎么写?

2) When I try sum "(Amount_New1+Amount_New2) Amount_New" (and changing the subquery column names to amount_new1 / amount_new2) it is not working neither. 2)当我尝试求和“(Amount_New1 + Amount_New2)Amount_New”(并将子查询列名称更改为amount_new1 / amount_new2)时,它都不起作用。 Why? 为什么?

If you want to return a single row, conceptually representing an aggregate over the entire union table, then remove GROUP BY : 如果要返回单行,从概念上讲代表整个联合表的汇总,则删除GROUP BY

SELECT  SUM(Amount_New) Amount_New,
        SUM(Import_Dropout) Import_Dropout,
        SUM(Import) Import,
        SUM(Processing) Processing,
        SUM(Processing_Dropout) Processing_Dropout,
        SUM(Matching) Matching,
        SUM(Matching_Dropout) Matching_Dropout,
        SUM(Export) Export,
        SUM(Exported) Exported,
        SUM(Rejected) Rejected,
        SUM(AmountSubTotal) AmountSubTotal,
        SUM(AmountTotal) AmountTotal
FROM
(
    -- ... your current query
) SK;

You also may want to remove the UnionOrder computed column, since there is no need to sort a single row result set. 您还可能需要删除UnionOrder计算列,因为不需要对单行结果集进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM