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 2008 Forums
 Transact-SQL (2008)
 Join on sub query not working properly

Author  Topic 

kdeutsch
Yak Posting Veteran

83 Posts

Posted - 2013-09-16 : 14:40:43
What I am doing is trying to get the last record for an individual inserted into this table as part of my sub query. tblSRPAttendance

I have tried row_number, top 1 but nothing seems to limit it to the last record inserted.

ALTER view [dbo].[vw_UnitSRpReadiness]
as
Select s.SSN_SM strssn,
s.RANK,
s.NAME,
Convert(varchar(10), s.DOB, 111) DOB,
COnvert(varchar(10), a.dttimein, 111) dttimein,
s.RPT_SEQ_CODE,
s.ATCH_CODE,
s.STAT,
s.UIC
from OPENQUERY (SIDPERS, 'SELECT vw.SSN_SM, vw.RANK,
vw.NAME,
vw.RPT_SEQ_CODE,
vw.ATCH_CODE,
vw.STAT,
pt.DOB,
Case WHEN ATCH_CODE = ''A'' THEN ATCH_UIC ELSE ASGN_UIC END UIC
from DOIM_UMR_VW vw
LEFT JOIN PERS_PERSON_TBL pt on pt.SSN_SM = vw.SSN_SM WHERE vw.SSN_SM IS NOT NULL') s
LEFT JOIN (select dttimein, strssn, ROW_NUMBER() over(partition by strssn order by dttimein desc) as rn from tblAttendance) a on a.strSSN = s.SSN_SM
Where s.SSN_SM not in (Select strssn from tblPersonnel where intSRpId = 156 and dtExpire > GETDATE())
or
s.SSN_SM not in (Select strssn from tblPersonnel where intSRpId = 157 and dtExpire > GETDATE())

GO

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-09-16 : 15:04:39
quote:
Originally posted by kdeutsch

What I am doing is trying to get the last record for an individual inserted into this table as part of my sub query. tblSRPAttendance

I have tried row_number, top 1 but nothing seems to limit it to the last record inserted.

ALTER view [dbo].[vw_UnitSRpReadiness]
as
Select s.SSN_SM strssn,
s.RANK,
s.NAME,
Convert(varchar(10), s.DOB, 111) DOB,
COnvert(varchar(10), a.dttimein, 111) dttimein,
s.RPT_SEQ_CODE,
s.ATCH_CODE,
s.STAT,
s.UIC
from OPENQUERY (SIDPERS, 'SELECT vw.SSN_SM, vw.RANK,
vw.NAME,
vw.RPT_SEQ_CODE,
vw.ATCH_CODE,
vw.STAT,
pt.DOB,
Case WHEN ATCH_CODE = ''A'' THEN ATCH_UIC ELSE ASGN_UIC END UIC
from DOIM_UMR_VW vw
LEFT JOIN PERS_PERSON_TBL pt on pt.SSN_SM = vw.SSN_SM WHERE vw.SSN_SM IS NOT NULL') s
LEFT JOIN (select dttimein, strssn, ROW_NUMBER() over(partition by strssn order by dttimein desc) as rn from tblAttendance) a on a.strSSN = s.SSN_SM
Where s.SSN_SM not in (Select strssn from tblPersonnel where intSRpId = 156 and dtExpire > GETDATE())
or
s.SSN_SM not in (Select strssn from tblPersonnel where intSRpId = 157 and dtExpire > GETDATE())

GO

I don't see a tblSRPAttendance in your query. Assuming you meant tblAttendance, add the code shown in red to your query.
ALTER view [dbo].[vw_UnitSRpReadiness]
as
Select s.SSN_SM strssn,
s.RANK,
s.NAME,
Convert(varchar(10), s.DOB, 111) DOB,
COnvert(varchar(10), a.dttimein, 111) dttimein,
s.RPT_SEQ_CODE,
s.ATCH_CODE,
s.STAT,
s.UIC
from OPENQUERY (SIDPERS, 'SELECT vw.SSN_SM, vw.RANK,
vw.NAME,
vw.RPT_SEQ_CODE,
vw.ATCH_CODE,
vw.STAT,
pt.DOB,
Case WHEN ATCH_CODE = ''A'' THEN ATCH_UIC ELSE ASGN_UIC END UIC
from DOIM_UMR_VW vw
LEFT JOIN PERS_PERSON_TBL pt on pt.SSN_SM = vw.SSN_SM WHERE vw.SSN_SM IS NOT NULL') s
LEFT JOIN (select dttimein, strssn,
ROW_NUMBER() over(partition by strssn order by dttimein desc) as rn from tblAttendance) a
on a.strSSN = s.SSN_SM AND rn=1
Where s.SSN_SM not in (Select strssn from tblPersonnel where intSRpId = 156 and dtExpire > GETDATE())
or
s.SSN_SM not in (Select strssn from tblPersonnel where intSRpId = 157 and dtExpire > GETDATE())

GO
Go to Top of Page
   

- Advertisement -