Denslat
Denslat

Reputation: 179

Select only oldest unique records

The below code should Grab multiple values from multiple tables across 2 databases but as I attempted to implement T-SQL select rows by oldest date and unique category solution to an issue I encountered an error where the final line of my code would say "Incorrect syntax near ')'."

--Create Table #tmp2(FK_clientids varchar(50))
--Create table #tmp (phonenums varchar(50))
Delete from #tmp2
Delete from #tmp
Use Database2
INSERT INTO #tmp2
SELECT fk_clientid
FROM DM_ClientApplicants
where DM_ClientApplicants.FK_ApplicationID in (39155)
Use Database2
INSERT INTO #tmp
Select phonenum2 from DM_PhoneNumbers
where FK_ApplicationID in 
(
   Select FK_clientIDs from #tmp2
)
INSERT INTO #tmp
Select PhoneNum1 from DM_PhoneNumbers
where FK_ApplicationID in 
(
   Select FK_clientIDs from #tmp2
)
INSERT INTO #tmp
Select PhoneNum2 from DM_PhoneNumbers
where FK_ApplicationID in 
(
   Select FK_clientIDs from #tmp2
)

INSERT INTO #tmp
Select PhoneNum3 from DM_PhoneNumbers
where FK_ApplicationID in 
(
   Select FK_clientIDs from #tmp2
)
INSERT INTO #tmp
Select Partnerphonehome from DM_PhoneNumbers
where FK_ApplicationID in 
(
   Select FK_clientIDs from #tmp2
)
INSERT INTO #tmp
Select Partnerphonemobile from DM_PhoneNumbers
where FK_ApplicationID in 
(
   Select FK_clientIDs from #tmp2
)
INSERT INTO #tmp
Select Partnerphonework from DM_PhoneNumbers
where FK_ApplicationID in 
(
   Select FK_clientIDs from #tmp2
)
Use Database1
Select PhoneNum,sourcetable,c.FK_ApplicationID,SolRef,DT.Subject,dt.CreatedDate 
from (select PhoneNum,sourcetable,c.FK_ApplicationID,SolRef,DT.Subject,dt.CreatedDate,
             row_number() over(partition by OrderNO order by dt.CreatedDate desc) rn 
      from Dial D
      join Database2.dbo.DM_PhoneNumbers P on PhoneNum collate Latin1_General_CI_AS = PhoneNum1
      join Database2.dbo.DM_ClientApplicants C on P.FK_ApplicationID = C.FK_ClientID
      join Database2.dbo.DM_Sol S on C.FK_ApplicationID = S.FK_ApplicationID
      join Database2.dbo.DM_ApplicationDetails AD on AD.FK_ApplicationID = S.FK_ApplicationID
      join Database2.dbo.Tasks DT on DT.FK_ApplicationID = S.FK_ApplicationID
where PhoneNum in
(Select phonenums from #tmp)

UPDATE: So the bracket near "RN" is open, but if I try to close it the rest of the statement is not understood, anyone know why?

Upvotes: 0

Views: 109

Answers (1)

Angus Chung
Angus Chung

Reputation: 1587

The end of your sql should be:

Use Database1
Select PhoneNum,sourcetable,FK_ApplicationID,SolRef,Subject,CreatedDate 
from 
(
    select PhoneNum,sourcetable,c.FK_ApplicationID,SolRef,DT.Subject,dt.CreatedDate
    ,row_number() over(partition by OrderNO order by dt.CreatedDate desc) rn 
    from Dial D
    join Database2.dbo.DM_PhoneNumbers P on PhoneNum collate Latin1_General_CI_AS = PhoneNum1
    join Database2.dbo.DM_ClientApplicants C on P.FK_ApplicationID = C.FK_ClientID
    join Database2.dbo.DM_Sol S on C.FK_ApplicationID = S.FK_ApplicationID
    join Database2.dbo.DM_ApplicationDetails AD on AD.FK_ApplicationID = S.FK_ApplicationID
    join Database2.dbo.Tasks DT on DT.FK_ApplicationID = S.FK_ApplicationID
    where PhoneNum in
    (
        Select phonenums from #tmp
    )
)a -- add

Upvotes: 1

Related Questions