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 |
tmeans11
Starting Member
3 Posts |
Posted - 2009-01-18 : 16:22:36
|
I've never put a query on a forum before. I am learning sql and wondering if anyone can give me some tips. Is there anyway in my query to only show the rows with the top dateofentry or max dateofentry? My query is showing multiple rows for each patient record dateofentry. I only want to see the max dateofentry for each row...___________________________________________________________Drop table #tmp1select guarantor.guarantorid, guarantor.first, guarantor.middle, guarantor.last, guarantor.address1, guarantor.address2, guarantor.city, guarantor.state, guarantor.zip, guarantor.ssn, guarantor.phone1, guarantor.phone2, guarantor.lastStatement, patientprofileagg.patbalance, paymentmethod.dateofentryinto #tmp1from guarantorinner join patientprofile on guarantor.guarantorid = patientprofile.guarantoridinner join patientprofileagg on patientprofile.patientprofileid = patientprofileagg.patientprofileidInner join patientvisit on patientprofile.patientprofileid = patientvisit.patientprofileidinner join VisitTransactions ON patientvisit.patientvisitid = visittransactions.patientvisitidinner join transactions on visittransactions.visittransactionsid = transactions.visittransactionsid INNER JOIN PaymentMethod ON visittransactions.paymentmethodid = PaymentMethod.paymentmethodidWhere patientprofileagg.patbalance > 50.00and transactions.type = 't'and guarantor.lastStatement is not nullAND transactions.action = 't'and patientprofile.deathdate is nullAND PaymentMethod.dateofentry >= '2008-02-01'and PaymentMethod.dateofentry <'2008-08-31'order by guarantor.last Ascselect *from #tmp1_____________________________________________________Can anyone help me? I can also include a spreadsheet if that will help. Basically I want to see one row per patient with the max payment date...I am at a loss... |
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2009-01-18 : 21:37:52
|
Hi, What I normally do in this situation is create a temp table containing the ID (guarantor ID in your case), and the max date of entry. Then you link this new table to the existing table by the ID and MaxDateOfEntry. A bit hard without a diagram but I hope you get my drift.Cheers,Tim |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-18 : 23:04:42
|
show some sample data and explain what you want |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-24 : 00:35:33
|
quote: Originally posted by tmeans11 I've never put a query on a forum before. I am learning sql and wondering if anyone can give me some tips. Is there anyway in my query to only show the rows with the top dateofentry or max dateofentry? My query is showing multiple rows for each patient record dateofentry. I only want to see the max dateofentry for each row...___________________________________________________________Drop table #tmp1select guarantor.guarantorid, guarantor.first, guarantor.middle, guarantor.last, guarantor.address1, guarantor.address2, guarantor.city, guarantor.state, guarantor.zip, guarantor.ssn, guarantor.phone1, guarantor.phone2, guarantor.lastStatement, patientprofileagg.patbalance, paymentmethod.dateofentryinto #tmp1from guarantorinner join patientprofile on guarantor.guarantorid = patientprofile.guarantoridinner join patientprofileagg on patientprofile.patientprofileid = patientprofileagg.patientprofileidInner join patientvisit on patientprofile.patientprofileid = patientvisit.patientprofileidinner join VisitTransactions ON patientvisit.patientvisitid = visittransactions.patientvisitidinner join transactions on visittransactions.visittransactionsid = transactions.visittransactionsid INNER JOIN PaymentMethod ON visittransactions.paymentmethodid = PaymentMethod.paymentmethodidWhere patientprofileagg.patbalance > 50.00and transactions.type = 't'and guarantor.lastStatement is not nullAND transactions.action = 't'and patientprofile.deathdate is nullAND PaymentMethod.dateofentry >= '2008-02-01'and PaymentMethod.dateofentry <'2008-08-31'order by guarantor.last Ascselect *from #tmp1_____________________________________________________Can anyone help me? I can also include a spreadsheet if that will help. Basically I want to see one row per patient with the max payment date...I am at a loss...
Iam not producing exact solution for ur problem but iam giving an example which is some what similar to your description. Please look into it. It may be helpful..declare @temp table ( patientid int, Patientname varchar(40), Dateofentry datetime)insert into @temp select 1,'George',Getdate()-100 union allselect 2,'Dean',Getdate()-20 union allselect 1,'George',Getdate()-5 union allselect 2,'Dean',Getdate()-2 select patientid,patientname, dateofentryfrom ( select row_number() over (partition by patientid order by dateofentry desc ) as sno,* from @temp )t where t.sno = 1 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-01-24 : 00:48:53
|
quote: Originally posted by raky
quote: Originally posted by tmeans11 I've never put a query on a forum before. I am learning sql and wondering if anyone can give me some tips. Is there anyway in my query to only show the rows with the top dateofentry or max dateofentry? My query is showing multiple rows for each patient record dateofentry. I only want to see the max dateofentry for each row...___________________________________________________________Drop table #tmp1select guarantor.guarantorid, guarantor.first, guarantor.middle, guarantor.last, guarantor.address1, guarantor.address2, guarantor.city, guarantor.state, guarantor.zip, guarantor.ssn, guarantor.phone1, guarantor.phone2, guarantor.lastStatement, patientprofileagg.patbalance, paymentmethod.dateofentryinto #tmp1from guarantorinner join patientprofile on guarantor.guarantorid = patientprofile.guarantoridinner join patientprofileagg on patientprofile.patientprofileid = patientprofileagg.patientprofileidInner join patientvisit on patientprofile.patientprofileid = patientvisit.patientprofileidinner join VisitTransactions ON patientvisit.patientvisitid = visittransactions.patientvisitidinner join transactions on visittransactions.visittransactionsid = transactions.visittransactionsid INNER JOIN PaymentMethod ON visittransactions.paymentmethodid = PaymentMethod.paymentmethodidWhere patientprofileagg.patbalance > 50.00and transactions.type = 't'and guarantor.lastStatement is not nullAND transactions.action = 't'and patientprofile.deathdate is nullAND PaymentMethod.dateofentry >= '2008-02-01'and PaymentMethod.dateofentry <'2008-08-31'order by guarantor.last Ascselect *from #tmp1_____________________________________________________Can anyone help me? I can also include a spreadsheet if that will help. Basically I want to see one row per patient with the max payment date...I am at a loss...
Iam not producing exact solution for ur problem but iam giving an example which is some what similar to your description. Please look into it. It may be helpful..declare @temp table ( patientid int, Patientname varchar(40), Dateofentry datetime)insert into @temp select 1,'George',Getdate()-100 union allselect 2,'Dean',Getdate()-20 union allselect 1,'George',Getdate()-5 union allselect 2,'Dean',Getdate()-2 select patientid,patientname, dateofentryfrom ( select row_number() over (partition by patientid order by dateofentry desc ) as sno,* from @temp )t where t.sno = 1
hi raky,Row_number will not work in sql2000 i think so |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-24 : 00:53:14
|
quote: Originally posted by bklr
quote: Originally posted by raky
quote: Originally posted by tmeans11 I've never put a query on a forum before. I am learning sql and wondering if anyone can give me some tips. Is there anyway in my query to only show the rows with the top dateofentry or max dateofentry? My query is showing multiple rows for each patient record dateofentry. I only want to see the max dateofentry for each row...___________________________________________________________Drop table #tmp1select guarantor.guarantorid, guarantor.first, guarantor.middle, guarantor.last, guarantor.address1, guarantor.address2, guarantor.city, guarantor.state, guarantor.zip, guarantor.ssn, guarantor.phone1, guarantor.phone2, guarantor.lastStatement, patientprofileagg.patbalance, paymentmethod.dateofentryinto #tmp1from guarantorinner join patientprofile on guarantor.guarantorid = patientprofile.guarantoridinner join patientprofileagg on patientprofile.patientprofileid = patientprofileagg.patientprofileidInner join patientvisit on patientprofile.patientprofileid = patientvisit.patientprofileidinner join VisitTransactions ON patientvisit.patientvisitid = visittransactions.patientvisitidinner join transactions on visittransactions.visittransactionsid = transactions.visittransactionsid INNER JOIN PaymentMethod ON visittransactions.paymentmethodid = PaymentMethod.paymentmethodidWhere patientprofileagg.patbalance > 50.00and transactions.type = 't'and guarantor.lastStatement is not nullAND transactions.action = 't'and patientprofile.deathdate is nullAND PaymentMethod.dateofentry >= '2008-02-01'and PaymentMethod.dateofentry <'2008-08-31'order by guarantor.last Ascselect *from #tmp1_____________________________________________________Can anyone help me? I can also include a spreadsheet if that will help. Basically I want to see one row per patient with the max payment date...I am at a loss...
Iam not producing exact solution for ur problem but iam giving an example which is some what similar to your description. Please look into it. It may be helpful..declare @temp table ( patientid int, Patientname varchar(40), Dateofentry datetime)insert into @temp select 1,'George',Getdate()-100 union allselect 2,'Dean',Getdate()-20 union allselect 1,'George',Getdate()-5 union allselect 2,'Dean',Getdate()-2 select patientid,patientname, dateofentryfrom ( select row_number() over (partition by patientid order by dateofentry desc ) as sno,* from @temp )t where t.sno = 1
hi raky,Row_number will not work in sql2000 i think so
If row_number() doesnot work then try thisdeclare @temp table ( patientid int, Patientname varchar(40), Dateofentry datetime)insert into @temp select 1,'George',Getdate()-100 union allselect 2,'Dean',Getdate()-20 union allselect 1,'George',Getdate()-5 union allselect 2,'Dean',Getdate()-2 select t1.patientid,t1.patientname,t1.dateofentry from @temp t1inner join ( select patientid, max(dateofentry) as doe from @temp group by patientid) t2 on t2.patientid = t1.patientid and t1.dateofentry = t2.doeorder by t1.patientid |
|
|
tmeans11
Starting Member
3 Posts |
Posted - 2009-01-27 : 20:39:59
|
Hey Raky - thanks so much I will give it a try, Wow all of you are awesome! I only hope one day I will be able to help someone... I still have a little more learning to do. thanks again! |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-28 : 06:11:19
|
welcome.... |
|
|
|
|
|
|
|