1
我與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 |
如何使用一個查詢獲得以上結果?請幫幫我。謝謝