我正在使用SQL Server 2008。
我创建了一个这样的存储过程:
SELECT SUM(m.BookingAmt) + SUM(m.FormFee) AS 'SIP'
FROM dbo.Member AS m
INNER JOIN dbo.PlanMaster AS pm ON m.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster AS ptm ON pm.PlanTypeId = ptm.PlanTypeId
WHERE ptm.IsSingleInstallment = 'true'
AND m.CompanyId = @CompanyId
AND m.CscId = @CscId
AND m.CommencementDate = @Date
SELECT SUM(i.PaymentAmt) + SUM(m.FormFee) AS 'Fresh'
FROM dbo.Installment AS i
INNER JOIN dbo.Member AS m ON i.MemberId = m.MemberId
INNER JOIN dbo.PlanMaster AS pm ON i.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster AS ptm ON pm.PlanTypeId = ptm.PlanTypeId
WHERE i.InstallmentNo = 1
AND m.CompanyId = @CompanyId
AND m.CscId = @CscId
AND i.PaymentDate = @Date
AND ptm.IsSingleInstallment = 'false'
SELECT SUM(i.PaymentAmt) AS '1stYear'
FROM dbo.Installment AS i
INNER JOIN dbo.Member AS m ON i.MemberId = m.MemberId
WHERE i.InstallmentNo > 1
AND i.InstallmentNo < 13
AND m.CompanyId = @CompanyId
AND m.CscId = @CscId
AND i.PaymentDate = @Date
SELECT SUM(i.PaymentAmt) AS '2ndYear'
FROM dbo.Installment AS i
INNER JOIN dbo.Member AS m ON i.MemberId = m.MemberId
WHERE i.InstallmentNo > 12
AND i.InstallmentNo < 25
AND m.CompanyId = @CompanyId
AND m.CscId = @CscId
AND i.PaymentDate = @Date
SELECT SUM(i.PaymentAmt) AS '3rdYear'
FROM dbo.Installment AS i
INNER JOIN dbo.Member AS m ON i.MemberId = m.MemberId
WHERE i.InstallmentNo > 24
AND i.InstallmentNo < 37
AND m.CompanyId = @CompanyId
AND m.CscId = @CscId
AND i.PaymentDate = @Date
SELECT SUM(i.PaymentAmt) AS '4thYear'
FROM dbo.Installment AS i
INNER JOIN dbo.Member AS m ON i.MemberId = m.MemberId
WHERE i.InstallmentNo > 36
AND m.CompanyId = @CompanyId
AND m.CscId = @CscId
AND i.PaymentDate = @Date
但我希望得到这样的结果......
---------------------------------------------------------------------
Date | SIP | Fresh | 1stYear | 2ndYear | 3rdYear | 4thYear |
---------------------------------------------------------------------
01/02/2013 | 2000 | 2500 | 5000 | 3500 | 4500 | 6500 |
------------------------------------------------------- --------------
02/02/2013 | 6500 | 5000 | 1500 | 4500 | 3520 | 1852 |
--------------------------------------------------------------------
03/02/2013 | 2560 | 2500 | 3500 | 4500 | 2000 | 2000 |
如何使用一个查询获得上述结果?请帮我。感谢
答案 0 :(得分:2)
您可以使用带有1stYear
表达式的聚合函数来合并生成2ndYear
,CASE
等的查询,以创建列而不是行。
然后,您可以为其他两个创建子查询,最后在日期中将它们连接在一起。所以你的最终查询可能是:
SELECT coalesce(q1.CommencementDate, q2.PaymentDate, q3.PaymentDate) date,
q1.SIP,
q2.Fresh,
q3.[1stYear],
q3.[2ndYear],
q3.[3rdYear],
q3.[4thYear]
FROM
(
SELECT m.CommencementDate,
SUM(m.BookingAmt) + SUM(m.FormFee) AS 'SIP'
FROM dbo.Member AS m
INNER JOIN dbo.PlanMaster AS pm
ON m.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster AS ptm
ON pm.PlanTypeId = ptm.PlanTypeId
WHERE ptm.IsSingleInstallment = 'true'
AND m.CompanyId = @CompanyId
AND m.CscId = @CscId
AND m.CommencementDate = @Date
) q1
FULL OUTER JOIN
(
SELECT i.PaymentDate,
SUM(i.PaymentAmt) + SUM(m.FormFee) AS 'Fresh'
FROM dbo.Installment AS i
INNER JOIN dbo.Member AS m
ON i.MemberId = m.MemberId
INNER JOIN dbo.PlanMaster AS pm
ON i.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster AS ptm
ON pm.PlanTypeId = ptm.PlanTypeId
WHERE i.InstallmentNo = 1
AND m.CompanyId = @CompanyId
AND m.CscId = @CscId
AND i.PaymentDate = @Date
AND ptm.IsSingleInstallment = 'false'
) q2
ON q1.CommencementDate = q2.PaymentDate
FULL OUTER JOIN
(
SELECT i.PaymentDate,
sum(case
when i.InstallmentNo > 1 AND i.InstallmentNo < 13
then i.PaymentAmt else 0 end) AS [1stYear],
sum(case
when i.InstallmentNo > 12 AND i.InstallmentNo < 25
then i.PaymentAmt else 0 end) AS [2ndYear],
sum(case
when i.InstallmentNo > 24 AND i.InstallmentNo < 37
then i.PaymentAmt else 0 end) AS [3rdYear],
sum(case
when i.InstallmentNo > 36
then i.PaymentAmt else 0 end) AS [4thYear]
SUM(i.PaymentAmt) AS '1stYear'
FROM dbo.Installment AS i
INNER JOIN dbo.Member AS m
ON i.MemberId = m.MemberId
WHERE m.CompanyId = @CompanyId
AND m.CscId = @CscId
AND i.PaymentDate = @Date
GROUP BY i.PaymentDate
) q3
ON q1.CommencementDate = q3.PaymentDate;