繁体   English   中英

带有COUNTs的SQL和带有跨UNION GROUP BY的SUM

[英]SQL with COUNTs and SUMs with cross-UNION GROUP BY

SQL大师需要,这是杀死我的神经元。 我有这样的数据库结构(简化):

DESC documents;
    id          INT PK
    bill_id     INT FK
    dtype       INT -- 1=receipts, 0=invoices
    total       DECIMAL

DESC bills;
    id          INT PK
    waiter_id   INT FK

DESC waiters;
    id          INT PK
    name        VARCHAR
    surname     VARCHAR

相当不言自明,但我需要计算所有收据(documents.dtype = 1)和发票(documents.dtype = 0)的总数,并按服务员分组。 我做了两个SELECT:

SELECT
        B.waiter_id,
        WA.name, 
        WA.surname, 
        COUNT(D.id) AS Receipts, 
        SUM(D.total) AS TotReceipts
FROM    
    documents D
    JOIN bills B ON (B.id = D.bill_id)
    JOIN waiters WA ON (WA.id = B.waiter_id)
WHERE 
    D.dtype = 1          
GROUP BY 
    waiter_id;

好吧,我明白了:

1, 'Mario',   'Rossi',   6, 485.20
2, 'Luigino', 'Bianchi', 1, 456.00

做了第二个SELECT,只是将documents.dtype更改为0:

SELECT
        B.waiter_id,
        WA.name, 
        WA.surname, 
        COUNT(D.id) AS Invoices, 
        SUM(D.total) AS TotInvoices
FROM    
    documents D
    JOIN bills B ON (B.id = D.bill_id)
    JOIN waiters WA ON (WA.id = B.waiter_id)
WHERE 
    D.dtype = 0 
GROUP BY 
    waiter_id;

现在我得到:

1, 'Mario', 'Rossi', 1, 38.00

现在我可以联合两个SELECTS

SELECT
        B.waiter_id,
        WA.name, 
        WA.surname, 
        COUNT(D.id) AS Receipts, 
        SUM(D.total) AS TotReceipts
FROM    
    documents D
    JOIN bills B ON (B.id = D.bill_id)
    JOIN waiters WA ON (WA.id = B.waiter_id)
WHERE 
    D.dtype = 1      
GROUP BY 
    waiter_id
UNION SELECT
        B.waiter_id,
        WA.name, 
        WA.surname, 
        COUNT(D.id) AS Invoices, 
        SUM(D.total) AS TotInvoices
FROM    
    documents D
    JOIN bills B ON (B.id = D.bill_id)
    JOIN waiters WA ON (WA.id = B.waiter_id)
WHERE 
    D.dtype = 0 
GROUP BY 
    waiter_id;

我得到:

1, 'Mario',   'Rossi',   6, 485.20
2, 'Luigino', 'Bianchi', 1, 456.00
1, 'Mario',   'Rossi',   1, 38.00

嗯,正确,但我需要由服务员交叉联合分组的行! 那是我想要服务员马里奥的一行:

wid wname       wsurname    receipts    totreceipts     invoices        totinvoices
1,  'Mario',    'Rossi',    6,          485.20          1               38.0
2,  'Luigino',  'Bianchi',  1,          456.00          0               0.0

那会很棒,但我还想再增加两列来总结这些数字:

wid wname       wsurname    receipts    totreceipts     invoices        totinvoices     docs    totdocs
1,  'Mario',    'Rossi',    6,          485.20          1               38.0            7       523.20
2,  'Luigino',  'Bianchi',  1,          456.00          0               0.0             1       456.00

这将超级超酷。

您可以将conditon从where子句移动到case语句,如:

SELECT
        B.waiter_id,
        WA.name, 
        WA.surname, 
        SUM(case when d.dtype = 1 then 1 end) AS Receipts, 
        SUM(case when d.dtype = 1 then D.total end) AS TotReceipts,
        SUM(case when d.dtype = 0 then 1 end) AS Invoices, 
        SUM(case when d.dtype = 0 then D.total end) AS TotInvoices
FROM    
    documents D
    JOIN bills B ON (B.id = D.bill_id)
    JOIN waiters WA ON (WA.id = B.waiter_id)
GROUP BY 
    waiter_id

此查询返回您提出的问题

 SELECT
    B.waiter_id,
    WA.name, 
    WA.surname,
    (select count(id)             from documents where dtype = 0 and bill_id = B.id) AS Invoices, 
    (select isnull(sum(total), 0) from documents where dtype = 0 and bill_id = B.id) AS TotInvoices, 
    (select count(id)             from documents where dtype = 1 and bill_id = B.id) AS Receipts, 
    (select isnull(sum(total), 0) from documents where dtype = 1 and bill_id = B.id) AS TotReceipts
  FROM bills B
 inner join waiters WA on WA.id = B.waiter_id

要汇总所有总计,您可以进行内部选择,以便将总计总结为:

select  data.*, 
        data.Invoices + data.Receipts as docs,
        data.TotInvoices + data.TotReceipts as totaldocs
  from (
  select 
    B.waiter_id,
    WA.name, 
    WA.surname,
    (select count(id)             from documents where dtype = 0 and bill_id = B.id) AS Invoices, 
    (select isnull(sum(total), 0) from documents where dtype = 0 and bill_id = B.id) AS TotInvoices, 
    (select count(id)             from documents where dtype = 1 and bill_id = B.id) AS Receipts, 
    (select isnull(sum(total), 0) from documents where dtype = 1 and bill_id = B.id) AS TotReceipts
  from bills B
 inner join waiters WA on WA.id = B.waiter_id
 ) data

暂无
暂无

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

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