Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
i have the following nested query in my main query. It retrieves the min authored date of a document, and i would like to get the associated author. (displayname)when i run the query i get all the authors for the documents and the corresponding dates. in order to get the min authored date and the correct author i think i would have to create a query inside this one to get my displayname. not sure how to do that. i think i would have to join the sub query to this query by min authored date to the authored date????SELECT distinctmin(cd.AuthoredDtm) AS INIT_NRS_ASSES,Displayname AS NRS_ROLE_NAME,ClientVisitGUIDFROM dbo.CV3ClientDocument cdINNER JOINdbo.CV3User ON cd.UserGUID = dbo.CV3User.GUIDwhere orderroletype ='Nurse' and entrytype='4'group by ClientVisitGUID,Displayname
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts
Posted - 2011-06-24 : 12:24:43
Would a subquery like this work for you?
SELECT INIT_NRS_ASSES,NRS_ROLE_NAME,ClientVisitGUIDFROM( SELECT ROW_NUMBER() OVER (PARTITION BY ClientVisitGUID ORDER BY cd.AuthoredDtm) AS RN, cd.AuthoredDtm AS INIT_NRS_ASSES, Displayname AS NRS_ROLE_NAME, ClientVisitGUID FROM dbo.CV3ClientDocument cd INNER JOIN dbo.CV3User ON cd.UserGUID = dbo.CV3User.GUID WHERE orderroletype = 'Nurse' AND entrytype = '4') NWHERE RN = 1;