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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 joining tables

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-01-18 : 08:32:53
sigaldo writes "i have three tables as shown below:

tblCustomer
NEC Number Employee Count Class
1 ABC 1
2 XYZ 1
3 MNO 2

tblEstimate
NEC Number Month Year Due Date Estimated Amount
1 8 2005 15/09/2005 300-00
2 8 2005 15/09/2005 380-00
1 9 2005 15/10/2005 390-00
2 9 2005 15/10/2005 500-00
1 10 2005 15/11/2005 410-00
2 10 2005 15/11/2005 350-00

tblPayment
Date NEC Number Month Year Amount
27/08/2005 1 8 2005 255-00
22/08/2005 2 8 2005 350-00
17/09/2005 1 9 2005 300-00
19/09/2005 2 9 2005 300-00
02/10/2005 2 9 2005 200-00
20/10/2005 1 10 2005 410-00
24/10/2005 2 10 2005 250-00

the estimated amount in tblestimate is calculated monthly for each customer based on the employee count. due date in tblestimate is

last day of business month, which is the fifteenth. payments in tblpayment can be done on any day during business month.

desired output

NEC Number Customer Current Due 30 Due 60 Due >60
1 ABC 0-00 90-00 45-00 0-00
2 XYZ 100-00 0-00 30-00 0-00

i want to list for each customer (nec number) the current amount due, amount due greater than 30 days but less than 60, amount due

greater that 60 but less than 90, and then amount due greater than 90 days. i want a query that will do the calculations, or is there

anoother way i can achieve this"

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-01-18 : 09:07:54
You can do something like this for the aging. However I am not sure how did you derived at the output
quote:
desired output

NEC Number Customer Current Due 30 Due 60 Due >60
1 ABC 0-00 90-00 45-00 0-00
2 XYZ 100-00 0-00 30-00 0-00

create table #tblCustomer
(
[NEC Number] int,
[Employee] varchar(10),
[Count Class] int
)

insert into #tblCustomer
select 1, 'ABC', 1 union all
select 2, 'XYZ', 1 union all
select 3, 'MNO', 2


create table #tblEstimate
(
[NEC Number] int,
[Month] int,
[Year] int,
[Due Date] datetime,
[Estimated Amount] numeric(10,2)
)

insert into #tblEstimate
select 1, 8, 2005, '15/09/2005', 300.00 union all
select 2, 8, 2005, '15/09/2005', 380.00 union all
select 1, 9, 2005, '15/10/2005', 390.00 union all
select 2, 9, 2005, '15/10/2005', 500.00 union all
select 1, 10, 2005, '15/11/2005', 410.00 union all
select 2, 10, 2005, '15/11/2005', 350.00

create table #tblPayment
(
[Date] datetime,
[NEC Number] int,
[Month] int,
[Year] int,
[Amount] numeric(10,2)
)

insert into #tblPayment
select '27/08/2005', 1, 8, 2005, 255.00 union all
select '22/08/2005', 2, 8, 2005, 350.00 union all
select '17/09/2005', 1, 9, 2005, 300.00 union all
select '19/09/2005', 2, 9, 2005, 300.00 union all
select '02/10/2005', 2, 9, 2005, 200.00 union all
select '20/10/2005', 1, 10, 2005, 410.00 union all
select '24/10/2005', 2, 10, 2005, 250.00


select a.[NEC Number], c.[Employee],
sum(case when Due <= 30 then Amount else 0 end) as [Current],
sum(case when Due > 30 and Due <= 60 then Amount else 0 end) as [Due 30],
sum(case when Due > 60 and Due <= 90 then Amount else 0 end) as [Due 60],
sum(case when Due > 90 then Amount else 0 end) as [Due > 60]
from
(
select p.[NEC Number], Amount, datediff(day, [Date], [Due Date]) as Due
from #tblPayment p inner join #tblEstimate e
on p.[NEC Number] = e.[NEC Number]
and p.[Month] = e.[Month]
and p.[Year] = e.[Year]
) as a inner join #tblCustomer c
on a.[NEC Number] = c.[NEC Number]
group by a.[NEC Number], c.[Employee]

drop table #tblCustomer
drop table #tblEstimate
drop table #tblPayment


-----------------
'KH'

Go to Top of Page
   

- Advertisement -