我有这4个查询我想从这样的表中选择数据
---------------------------------------------------------------
Month Total Fee Fee Deposit Fee Due Fine
---------------------------------------------------------------
October 200,000 100,000 100,000 5,00
我的疑问是:
1
SELECT Sum(Fee)AS'TotalFee', DueDate AS'Date'
FROM VoucherTB
GROUP BY DueDate
2
SELECT Sum(VoucherTB.Fee)AS'FeeDeposit', DueDate AS'Date'
FROM VoucherTB
WHERE FeeSubmission='Paid'
GROUP BY VoucherTB.DueDate
3
SELECT Sum(Fee)AS'FeeDue', DueDate AS'Date'
FROM VoucherTB
WHERE FeeSubmission='UnPaid'
GROUP BY DueDate
4
SELECT Sum(RevenueTB.Fine)AS'Fine', VoucherTB.DueDate AS'Date'
FROM RevenueTB
INNER JOIN VoucherTB
ON venueTB.VoucherNo=VoucherTB.VoucherNo
GROUP BY VoucherTB.DueDate
答案 0 :(得分:5)
你可以这样做:
SELECT
v.DueDate AS'Date',
Sum(CASE WHEN v.FeeSubmission = 'Paid' THEN v.Fee ELSE 0 END) AS 'FeeDeposit',
Sum(CASE WHEN v.FeeSubmission = 'UnPaid' THEN v.Fee ELSE 0 END) AS 'FeeDue',
Sum(r.Fine)AS'Fine',
Sum(r.Fee) AS 'TotalFee'
FROM RevenueTB AS r
INNER JOIN VoucherTB AS v ON v.VoucherNo = r.VoucherNo
GROUP BY v.DueDate
答案 1 :(得分:0)
Select * from
(
SELECT Sum(Fee)AS'TotalFee', DueDate AS'Date'
FROM VoucherTB
GROUP BY DueDate
) A
left join
(
SELECT Sum(VoucherTB.Fee)AS'FeeDeposit', DueDate AS'Date'
FROM VoucherTB
WHERE FeeSubmission='Paid'
GROUP BY VoucherTB.DueDate
)B on B.Date = A.Date
left join
(
SELECT Sum(Fee)AS'FeeDue', DueDate AS'Date'
FROM VoucherTB
WHERE FeeSubmission='UnPaid'
GROUP BY DueDate
) C on C.Date = A.Date
left join
(
SELECT Sum(RevenueTB.Fine)AS'Fine', VoucherTB.DueDate AS'Date'
FROM RevenueTB
INNER JOIN VoucherTB
ON venueTB.VoucherNo=VoucherTB.VoucherNo
GROUP BY VoucherTB.DueDate
) D on D.Date = A.Date