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 |
murd
Starting Member
24 Posts |
Posted - 2012-09-12 : 12:08:06
|
I am trying to show the recent date of an entry for each person. here is what i have so farselect pp.searchname, pp.birthdate, max(o.obsdate) as obs_date, o.OBSVALUEfrom patientprofile ppinner join document d on pp.pid = d.pidinner join obs o ON o.SDID = d.SDID inner join obshead h ON o.HDID = h.HDID where h.NAME = 'BMI' and pp.Birthdate between '9-12-1994' and '9-12-2007' group by pp.searchname, pp.birthdate , o.OBSVALUEorder by pp.searchnameresults Name | birthdate | date entry | valueJane Doe 2003-08-31 2008-09-11 15:31:13.000 14.43Jane Doe 2003-08-31 2011-03-23 18:06:36.000 14.69Jane Doe 2003-08-31 2009-12-21 14:28:49.000 16.60John Doe 2002-09-27 2009-09-11 10:26:06.000 13.67John Doe 2002-09-27 2011-10-10 10:29:13.000 14.69so i would like to just see Jane Doe 2003-08-31 2011-03-23 18:06:36.000 14.69John Doe 2002-09-27 2011-10-10 10:29:13.000 14.69what am i doing wrong? |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-12 : 12:16:59
|
[code]select searchname, birthdate, obs_date, OBSVALUEfrom(select row_number() over (partition by Name,birthdate order by o.OBSVALUE DESC) AS Seq,pp.searchname, pp.birthdate, max(o.obsdate) as obs_date, o.OBSVALUEfrom patientprofile ppinner join document d on pp.pid = d.pidinner join obs o ON o.SDID = d.SDID inner join obshead h ON o.HDID = h.HDID where h.NAME = 'BMI' and pp.Birthdate between '9-12-1994' and '9-12-2007' group by pp.searchname, pp.birthdate , o.OBSVALUE)twhere Seq=1order by searchname[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
murd
Starting Member
24 Posts |
Posted - 2012-09-12 : 13:18:27
|
hmm...i get this Msg 8120, Level 16, State 1, Line 1Column 'obshead.NAME' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.Msg 8120, Level 16, State 1, Line 1Column 'obshead.NAME' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-12 : 13:21:27
|
[code]select searchname, birthdate, obs_date, OBSVALUEfrom(select row_number() over (partition by pp.searchname,pp.birthdate order by o.OBSVALUE DESC) AS Seq,pp.searchname, pp.birthdate, max(o.obsdate) as obs_date, o.OBSVALUEfrom patientprofile ppinner join document d on pp.pid = d.pidinner join obs o ON o.SDID = d.SDID inner join obshead h ON o.HDID = h.HDID where h.NAME = 'BMI' and pp.Birthdate between '9-12-1994' and '9-12-2007' group by pp.searchname, pp.birthdate , o.OBSVALUE)twhere Seq=1order by searchname[/code]just use correct field name------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
murd
Starting Member
24 Posts |
Posted - 2012-09-12 : 13:26:27
|
YES! your the man thanks |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-12 : 13:53:46
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
murd
Starting Member
24 Posts |
Posted - 2012-09-12 : 14:50:46
|
one more question..I am trying to add some more columns to this. select searchname, birthdate, obs_date, OBSVALUEfrom(select row_number() over (partition by pp.searchname,pp.birthdate order by o.OBSVALUE DESC) AS Seq,pp.searchname, pp.birthdate, max(o.obsdate) as obs_date, o.OBSVALUE, ob.OBSVALUE, obb.OBSVALUEfrom patientprofile ppinner join document d on pp.pid = d.pidinner join obs o ON o.SDID = d.SDID inner join obs ob ON ob.SDID = d.SDID inner join obs obb ON obb.SDID = d.SDID inner join obshead h ON o.HDID = h.HDID inner join obshead hh ON ob.HDID = hh.HDID inner join obshead hhh ON obb.HDID = hhh.HDIDwhere h.NAME = 'BMI' and hh.name = 'HEIGHT' and hhh.name = 'WEIGHT' and pp.Birthdate between '9-12-1994' and '9-12-2007' group by pp.searchname, pp.birthdate , o.OBSVALUE, ob.OBSVALUE, obb.OBSVALUE)twhere Seq=1order by pp.searchnameit says OBSVALUE was specified multiple times for 't' |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-12 : 14:54:34
|
you cant have multiple columns with same name. so give them alisesselect searchname, birthdate, obs_date, OBSVALUE,OB_OBSVALUE,OBB_OBSVALUEfrom(select row_number() over (partition by pp.searchname,pp.birthdate order by o.OBSVALUE DESC) AS Seq,pp.searchname, pp.birthdate, max(o.obsdate) as obs_date, o.OBSVALUE, ob.OBSVALUE AS OB_OBSVALUE, obb.OBSVALUE AS OBB_OBSVALUEfrom patientprofile ppinner join document d on pp.pid = d.pidinner join obs o ON o.SDID = d.SDID inner join obs ob ON ob.SDID = d.SDID inner join obs obb ON obb.SDID = d.SDID inner join obshead h ON o.HDID = h.HDID inner join obshead hh ON ob.HDID = hh.HDID inner join obshead hhh ON obb.HDID = hhh.HDIDwhere h.NAME = 'BMI' and hh.name = 'HEIGHT' and hhh.name = 'WEIGHT' and pp.Birthdate between '9-12-1994' and '9-12-2007' group by pp.searchname, pp.birthdate , o.OBSVALUE, ob.OBSVALUE, obb.OBSVALUE)twhere Seq=1order by pp.searchname ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
murd
Starting Member
24 Posts |
Posted - 2012-09-12 : 15:01:38
|
oh ok great! learned something new! thanks again |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-12 : 15:37:03
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|