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
现在我可以联合两个SELECTSs
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
mmm,正确但我需要按服务员交叉联合分组的行!那就是我想要服务员马里奥的单行:
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
这将超级超酷。
答案 0 :(得分:2)
您可以将条件从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
答案 1 :(得分:0)
此查询返回您提出的内容
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