Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
indralang
Starting Member
7 Posts |
Posted - 2014-02-06 : 03:35:37
|
I have 2 tablesCREATE TABLE BILL(BILL_ID INT INDENTITY NOTNULLBILL_DATE DATETIMEBILL_AMOUNT NUMERIC(10,2)BILL_BALANCE NUMERIC(10,2))CREATE TABLE PAYMENT(PAYMENT_ID INT IDENTITY NOTNULLBILL_ID INTPAYMENT_DATE DATETIMEPAYMENT_AMOUNT NUMERIC(10,2)) I have data like this:Bill: $100In January, I paid $50In Febuary, I paid $30In March, I paid $20I need to create report like below: In January the report will be:Bill: $100Payment1: $50In Febuary:Bill: $100Payment1: $50Payment2: $30In March:Bill: $100Payment1: $50Payment2: $30Payment3: $20In May it won't appear because it already paid in March.This is my Query:SELECT A.BILL_AMOUNT, A.BILL_DATE, B.PAYMENT_AMOUNT, B.PAYMENT_DATEFROM BILL AS ALEFT JOIN PAYMENT AS B ON A.BILL_ID = B.BILL_IDWHERE MONTH(A.BILL_DATE) <= @PERIOD_MONTH AND YEAR(A.BILL_DATE) = @PERIOD_YEARI need help to fix my query so the result like the report. |
|
sree203509
Starting Member
2 Posts |
Posted - 2014-02-06 : 03:59:44
|
Try thisSELECT A.BILL_AMOUNT, A.BILL_DATE, B.PAYMENT_AMOUNT, B.PAYMENT_DATEFROM BILL AS ALEFT JOIN PAYMENT AS B ON A.BILL_ID = B.BILL_IDWHERE MONTH(B.PAYMENT_DATE) <= @PERIOD_MONTH AND YEAR(B.PAYMENT_DATE) = @PERIOD_YEAR |
|
|
indralang
Starting Member
7 Posts |
Posted - 2014-02-06 : 04:19:53
|
quote: Originally posted by sree203509 Try thisSELECT A.BILL_AMOUNT, A.BILL_DATE, B.PAYMENT_AMOUNT, B.PAYMENT_DATEFROM BILL AS ALEFT JOIN PAYMENT AS B ON A.BILL_ID = B.BILL_IDWHERE MONTH(B.PAYMENT_DATE) <= @PERIOD_MONTH AND YEAR(B.PAYMENT_DATE) = @PERIOD_YEAR
Thanks for answering, but its not the answer if there is a bill in January but no payment in the same month. For example the first payment in Febuary.If that happened, the report should be like this:JanuaryBill: $100Payment: $0FebuaryBill: $100Payment: $50etc. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2014-02-06 : 05:41:14
|
[code]DECLARE @PERIOD_YEAR INT = 2014, @PERIOD_MONTH INT = 3; with PYMT AS( SELECT p.BILL_ID, p.PAYMENT_AMOUNT, p.PAYMENT_DATE FROM PAYMENT p WHERE p.PAYMENT_DATE < DATEADD(MONTH, @PERIOD_MONTH, DATEADD(YEAR, @PERIOD_YEAR - 1900, 0)) )SELECT b.BILL_ID, b.BILL_AMOUNT, b.BILL_DATE, p.PAYMENT_AMOUNT, p.PAYMENT_DATEFROM BILL b LEFT JOIN ( SELECT p.BILL_ID, PAYMENT_AMOUNT = SUM(p.PAYMENT_AMOUNT), LAST_PYMT_DATE = MAX(p.PAYMENT_DATE) FROM PYMT p GROUP BY p.BILL_ID ) tp on b.BILL_ID = tp.BILL_ID LEFT JOIN PYMT p on b.BILL_ID = p.BILL_IDWHERE b.BILL_AMOUNT > ISNULL(tp.PAYMENT_AMOUNT, 0) -- if the bill is not fully paidOR ( -- or the last payment is the report date YEAR(tp.LAST_PYMT_DATE) = @PERIOD_YEAR AND MONTH(tp.LAST_PYMT_DATE) = @PERIOD_MONTH )ORDER BY b.BILL_ID, p.PAYMENT_DATE[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
indralang
Starting Member
7 Posts |
Posted - 2014-02-06 : 21:08:50
|
quote: Originally posted by khtan
DECLARE @PERIOD_YEAR INT = 2014, @PERIOD_MONTH INT = 3; with PYMT AS( SELECT p.BILL_ID, p.PAYMENT_AMOUNT, p.PAYMENT_DATE FROM PAYMENT p WHERE p.PAYMENT_DATE < DATEADD(MONTH, @PERIOD_MONTH, DATEADD(YEAR, @PERIOD_YEAR - 1900, 0)) )SELECT b.BILL_ID, b.BILL_AMOUNT, b.BILL_DATE, p.PAYMENT_AMOUNT, p.PAYMENT_DATEFROM BILL b LEFT JOIN ( SELECT p.BILL_ID, PAYMENT_AMOUNT = SUM(p.PAYMENT_AMOUNT), LAST_PYMT_DATE = MAX(p.PAYMENT_DATE) FROM PYMT p GROUP BY p.BILL_ID ) tp on b.BILL_ID = tp.BILL_ID LEFT JOIN PYMT p on b.BILL_ID = p.BILL_IDWHERE b.BILL_AMOUNT > ISNULL(tp.PAYMENT_AMOUNT, 0) -- if the bill is not fully paidOR ( -- or the last payment is the report date YEAR(tp.LAST_PYMT_DATE) = @PERIOD_YEAR AND MONTH(tp.LAST_PYMT_DATE) = @PERIOD_MONTH )ORDER BY b.BILL_ID, p.PAYMENT_DATE KH[spoiler]Time is always against us[/spoiler]
This is what I really need!! Thank you very much!! |
|
|
|
|
|
|
|