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)
 aggregate join - my brain is not working today

Author  Topic 

melissar
Starting Member

15 Posts

Posted - 2003-05-21 : 13:59:28
Please help. I have the following tables and data
ProcedID VisitID ChgAmt AmtAllowed CoPay
1 145 120 70 10
2 2500 150 75 30
3 145 100 50 20
4 2500 250 100 20
5 2500 100 50 10

ProcedIDBalDate BalAdjAmt BalCurAmt
1 1/1/2002 13:23 80 60
1 7/1/2003 40 20
2 4/2/2003 16:00 45 105
3 5/1/2003 20 20
3 6/1/2003 10 40
4 5/2/2003 40 60
4 7/1/2003 40 20
5 1/1/2001 13:30 25 75
5 2/2/2003 23:00 15 60

I need to sum the BalCurAmt of the procedures where the BalDate is the MAX date for the procedure ID based on the VisitID. So for VisitID 145 I should have 60 and VisitID 2500 should be 100.

Any help would be greatly appreciated

mtomeo
Starting Member

30 Posts

Posted - 2003-05-21 : 14:36:05
My math is a bit rusty, but I show BalCurAmt for Visit 2500 as 185 (ProcID 2 = 100, 4 = 20, 5 = 60....185?).


create table tbl1
(ProcedID int, VisitID varchar(4), ChgAmt int, AmtAllowed int, CoPay int)

create table tbl2
(ProcedID int, BalDate datetime, BalAdjAmt int, BalCurAmt int)

Insert into tbl1 (ProcedID, VisitID, ChgAmt, AmtAllowed, CoPay) values (1, '145', 120, 70, 10)
Insert into tbl1 (ProcedID, VisitID, ChgAmt, AmtAllowed, CoPay) values (2, '2500', 150, 75, 30)
Insert into tbl1 (ProcedID, VisitID, ChgAmt, AmtAllowed, CoPay) values (3, '145', 100, 50, 20)
Insert into tbl1 (ProcedID, VisitID, ChgAmt, AmtAllowed, CoPay) values (4, '2500', 250, 100, 20)
Insert into tbl1 (ProcedID, VisitID, ChgAmt, AmtAllowed, CoPay) values (5, '2500', 100, 50, 10)

Insert into tbl2 (ProcedID, BalDate, BalAdjAmt, BalCurAmt) values (1, '1/1/2002 13:23', 80, 60)
Insert into tbl2 (ProcedID, BalDate, BalAdjAmt, BalCurAmt) values (1, '7/1/2003', 40, 20)
Insert into tbl2 (ProcedID, BalDate, BalAdjAmt, BalCurAmt) values (2, '4/2/2003 6:00', 45, 105)
Insert into tbl2 (ProcedID, BalDate, BalAdjAmt, BalCurAmt) values (3, '5/1/2003', 20, 20)
Insert into tbl2 (ProcedID, BalDate, BalAdjAmt, BalCurAmt) values (3, '6/1/2003', 10, 40)
Insert into tbl2 (ProcedID, BalDate, BalAdjAmt, BalCurAmt) values (4, '5/2/2003', 40, 60)
Insert into tbl2 (ProcedID, BalDate, BalAdjAmt, BalCurAmt) values (4, '7/1/2003', 40, 20)
Insert into tbl2 (ProcedID, BalDate, BalAdjAmt, BalCurAmt) values (5, '1/1/2001 13:30', 25, 75)
Insert into tbl2 (ProcedID, BalDate, BalAdjAmt, BalCurAmt) values (5, '2/2/2003 23:00', 15, 60)


Select VisitID, sum(BalCurAmt) BalCurAmt
From tbl1, tbl2 a
Where tbl1.ProcedID = a.ProcedID and
BalDate = (Select max(BalDate)
From tbl2 b
Where tbl1.ProcedID = b.ProcedID)
Group By VisitID


VisitID BalCurAmt
-----------------
145 60
2500 185


Go to Top of Page

melissar
Starting Member

15 Posts

Posted - 2003-05-21 : 14:57:56
Thanks!

Go to Top of Page
   

- Advertisement -