简体   繁体   English

如何组合这些查询?

[英]How can I combine these queries?

I have 5 queries I'd like to join together. 我有5个查询,我想加入。 Basically what they do is they go though the database and select how much a tenant has paid, and how much a tenant owes based on how long ago he or she was charged. 基本上他们所做的是通过数据库并选择租户支付了多少,以及租户根据他或她多久收费而欠多少钱。

I have four categories 我有四个类别
Charge < 30 days old 收费<30天
Charge < 60 AND >= 30 days old 充电<60 AND> = 30天
Charge < 90 AND >= 60 days old 充电<90 AND> = 60天
Charge > 90 days old 收费> 90天

I know how to get all those values separately, but how can I get them together, plus the amount the tenant has paid? 我知道如何分别获得所有这些价值,但我怎样才能将它们合并在一起,加上租户支付的金额?

Here are my queries: 这是我的疑问:
Amount the Tenant Has Paid 承租人支付的金额

SELECT TransactionCode, TenantID, SUM(Amount) AS Paid FROM tblTransaction
WHERE Amount > 0
GROUP BY TransactionCode, TenantID

Charge is less than 30 days old 收费不到30天

SELECT TransactionCode, TenantID, SUM(Amount) AS ChargedCurrent FROM tblTransaction
WHERE Amount < 0 AND TransactionDate > DATEADD("dd", -30, GETDATE())
GROUP BY TransactionCode, TenantID

Charge is less than 60 days old, but older than 29 days 收费不到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

Charge is less than 90 days old, but older than 59 days 收费不到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

Charge is older than 89 days 收费超过89天

SELECT TransactionCode, TenantID, SUM(Amount) AS ChargedOver90 FROM tblTransaction
WHERE Amount < 0 AND TransactionDate <= DATEADD("dd", -90, GETDATE())
GROUP BY TransactionCode, TenantID

How can I get these all with one query? 如何通过一个查询获取所有这些内容?

It can be done like this: 它可以这样做:

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

using UNION to stitch the result sets together would work. 使用UNION将结果集拼接在一起会起作用。 You may want to translate it using the PIVOT capabilities to get the amounts into separate columns. 您可能希望使用PIVOT功能对其进行翻译,以将金额分配到单独的列中。 Sorry I can't be more specific, I don't have my notes and I don't know the exact syntax for this stuff off the top of my head. 对不起,我不能更具体,我没有我的笔记,我不知道这个东西的确切语法。

If you can make the projection or shape of the query the same across all 5 individual queries, you can use a union to not only combine the queries into a single result, but also order the results. 如果您可以在所有5个单独的查询中使查询的投影或形状相同,则可以使用联合不仅将查询组合到单个结果中,还可以对结果进行排序。 I'vee modified last column to be consistent and represent a state the charge is in for your to filter from the results: 我修改了最后一列是一致的,并代表了从结果中过滤的费用状态:

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

This cross-tab query should work: 此交叉表查询应该有效:

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 FROM tblTransaction GROUP BY TransactionCode,TenantID

Add an extra column to each query 为每个查询添加一个额外的列

SELECT TransactionCode, TenantID, SUM(Amount) AS ChargedCurrent, 30 as [DaysLate] FROM tblTransaction... SELECT TransactionCode,TenantID,SUM(Amount)AS ChargedCurrent,30 as [DaysLate] FROM tblTransaction ...

and then UNION ALL the queries together 然后UNION将所有查询放在一起

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

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