我有5个想要加入的查询。基本上他们所做的就是通过数据库来选择租户支付了多少,以及租户根据他或她多久收费而欠多少钱。
我有四个类别
费用< 30天大
费用< 60 AND> = 30天旧
费用< 90 AND> = 60天旧
充电> 90天
我知道如何分别获得所有这些价值,但我怎样才能将它们合在一起,加上租户支付的金额?
以下是我的疑问:
承租人支付的金额
SELECT TransactionCode, TenantID, SUM(Amount) AS Paid FROM tblTransaction
WHERE Amount > 0
GROUP BY TransactionCode, TenantID
费用不到30天
SELECT TransactionCode, TenantID, SUM(Amount) AS ChargedCurrent FROM tblTransaction
WHERE Amount < 0 AND TransactionDate > DATEADD("dd", -30, GETDATE())
GROUP BY TransactionCode, TenantID
费用不到60天,但超过29天
SELECT TransactionCode, TenantID, SUM(Amount) AS ChargedOver30 FROM tblTransaction
WHERE Amount < 0 AND TransactionDate > DATEADD("dd", -60, GETDATE()) AND TransactionDate <= DATEADD("dd", -30, GETDATE())
GROUP BY TransactionCode, TenantID
费用不到90天,但超过59天
SELECT TransactionCode, TenantID, SUM(Amount) AS ChargedOver60 FROM tblTransaction
WHERE Amount < 0 AND TransactionDate > DATEADD("dd", -90, GETDATE()) AND TransactionDate <= DATEADD("dd", -60, GETDATE())
GROUP BY TransactionCode, TenantID
费用超过89天
SELECT TransactionCode, TenantID, SUM(Amount) AS ChargedOver90 FROM tblTransaction
WHERE Amount < 0 AND TransactionDate <= DATEADD("dd", -90, GETDATE())
GROUP BY TransactionCode, TenantID
如何通过一个查询获取所有这些内容?
答案 0 :(得分:7)
可以这样做:
SELECT TransactionCode, TenantID,
SUM(CASE WHEN Amount > 0 then Amount ELSE 0 END) AS Paid,
SUM(CASE WHEN Amount < 0 AND TransactionDate > DATEADD("dd", -30, GETDATE()) THEN Amount ELSE 0 END) AS ChargedCurrent,
SUM(CASE WHEN Amount < 0 AND TransactionDate > DATEADD("dd", -60, GETDATE()) AND TransactionDate <= DATEADD("dd", -30, GETDATE()) THEN Amount ELSE 0 END) AS ChargedOver30
SUM(CASE WHEN Amount < 0 AND TransactionDate > DATEADD("dd", -90, GETDATE()) AND TransactionDate <= DATEADD("dd", -60, GETDATE()) then Amount Else 0 END) AS ChargedOver60,
SUM(CASE WHEN Amount < 0 AND TransactionDate <= DATEADD("dd", -90, GETDATE()) THEN Amount ELSE 0 END) AS ChargedOver90
FROM tblTransaction
GROUP BY TransactionCode, TenantID
答案 1 :(得分:1)
使用UNION将结果集拼接在一起会起作用。您可能希望使用PIVOT功能对其进行翻译,以将金额分配到单独的列中。对不起,我不能更具体,我没有笔记,我不知道这些东西的确切语法。
答案 2 :(得分:1)
如果您可以在所有5个单独的查询中使查询的投影或形状相同,则可以使用联合不仅将查询组合成单个结果,还可以对结果进行排序。我修改了最后一列是一致的,并代表了从结果中过滤的费用状态:
SELECT TransactionCode, TenantID, SUM(Amount) [Amount], 'Paid' [Status]
FROM tblTransactionWHERE Amount > 0
GROUP BY TransactionCode, TenantID
union
SELECT TransactionCode, TenantID, SUM(Amount) [Amount], 'Charged Current' [Status]
FROM tblTransactionWHERE Amount < 0 AND TransactionDate > DATEADD("dd", -30, GETDATE())
GROUP BY TransactionCode, TenantID
union
SELECT TransactionCode, TenantID, SUM(Amount) [Amount], 'ChargedOver30' [Status]
FROM tblTransactionWHERE Amount < 0 AND TransactionDate > DATEADD("dd", -60, GETDATE()) AND TransactionDate <= DATEADD("dd", -30, GETDATE())
GROUP BY TransactionCode, TenantID
union
SELECT TransactionCode, TenantID, SUM(Amount) [Amount], 'ChargedOver60' [Status]
FROM tblTransactionWHERE Amount < 0 AND TransactionDate > DATEADD("dd", -90, GETDATE()) AND TransactionDate <= DATEADD("dd", -60, GETDATE())
GROUP BY TransactionCode, TenantID
union
SELECT TransactionCode, TenantID, SUM(Amount) [Amount], 'ChargedOver90' [Status]
FROM tblTransactionWHERE Amount < 0 AND TransactionDate <= DATEADD("dd", -90, GETDATE())
GROUP BY TransactionCode, TenantID
order by 4 --Status
答案 3 :(得分:1)
此交叉表查询应该有效:
SELECT
Case WHEN Amount > 0 Then Amount Else 0 End as [Total],
Case WHEN Amount < 0 AND TransactionDate > DATEADD("dd", -30, GETDATE())
Then Amount Else 0 End as [Charge 0-29 Days],
Case WHEN Amount < 0 AND TransactionDate > DATEADD("dd", -60, GETDATE()) AND TransactionDate <= DATEADD("dd", -30, GETDATE())
Then Amount Else 0 End as [Charge 30-59 Days],
Case WHEN Amount < 0 AND TransactionDate > DATEADD("dd", -90, GETDATE()) AND TransactionDate <= DATEADD("dd", -60, GETDATE())
Then Amount Else 0 End as [Charge 60-89 Days],
Case WHEN Amount < 0 AND TransactionDate <= DATEADD("dd", -90, GETDATE())
Then Amount Else 0 End as [Charge 90+ Days],
FROM tblTransaction GROUP BY TransactionCode,TenantID
答案 4 :(得分:0)
为每个查询添加一个额外的列
SELECT TransactionCode,TenantID,SUM(Amount)AS ChargedCurrent,30 as [DaysLate] FROM tblTransaction ...
然后UNION所有查询一起