perireddy.arikatla (9/22/2013)
When i Run the query without giving party_code in where clause
i.e
SELECT party_code, sum(inst_amt) as total_insts,
(Select Sum(paid_amt) as total_paid
From party_payments
Where paid_date < '11/27/2012'
Group by party_code) as TotalPaid
FROM party_payments
GROUP BY party_code
It was Giving Following Error. . .
Why are you not trying and testing out the code that was actually posted?? After I put together my OWN DLL and sample data, the query itself became trivial. NOTE: It is very close to the air code posted earlier.
SELECT t1.party_code, sum(t1.inst_amt) as total_insts,
(Select Sum(t2.paid_amt)
From @party_payments t2
WHERE t2.paid_date < '2012-11-27' and t2.party_code = t1.party_code
Group by t2.party_code) as TotalPaid
FROM @party_payments t1
GROUP BY t1.party_code
Since you are unwilling or unable to provide the requested information, DLL and insert statements, below is an example of what should be provided for any future questions.
declare @party_payments table
(
party_code char(7),
inst_amt int,
inst_date datetime,
paid_amt int,
paid_date datetime
)
Insert @party_payments (party_code, inst_amt, inst_date, paid_amt, paid_date) values
('HA20076',3000,'2012-08-26 00:00:00.000', 3000, '2012-08-23 00:00:00.000')
,('HA20076',2917,'2012-09-15 00:00:00.000', 2900, '2012-09-17 00:00:00.000')
,('HA20076',2917,'2012-10-15 00:00:00.000', 2900, '2012-10-18 00:00:00.000')
,('HA20076',2917,'2012-11-15 00:00:00.000', 2900, '2012-11-27 00:00:00.000')
,('HA20076',2917,'2012-12-15 00:00:00.000', 2900, '2012-12-31 00:00:00.000')
,('HA20076',2917,'2013-01-15 00:00:00.000', 0, NULL)
,('HA20076',2917,'2013-02-15 00:00:00.000', 0, NULL)
,('HA20076',2917,'2013-03-15 00:00:00.000', 0, NULL)
,('HA20076',2917,'2013-04-15 00:00:00.000', 0, NULL)
,('HA20076',2917,'2013-05-15 00:00:00.000', 0, NULL)
,('HA20076',2917,'2013-06-15 00:00:00.000', 0, NULL)
,('HA20077',3000,'2012-08-26 00:00:00.000', 3000, '2012-08-23 00:00:00.000')
,('HA20077',3000,'2012-09-15 00:00:00.000', 3000, '2012-09-17 00:00:00.000')
,('HA20077',3000,'2012-11-15 00:00:00.000', 3000, '2012-11-27 00:00:00.000')
Expected Output:
party_codetotal_instsTotalPaid
HA20076 32170 8800
HA20077 9000 6000
__________________________________________________________________________________________________________
How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/