sql server - Avoid picking rows from joined table and use it only to get column value -
dbo.matchdate(@date) :an inline tvf returns results dbo.patients table .the inline tvf returns these columns : rowid , percentmatch .
i have query far:
insert #temp2 (rownumber,valfromfunc,funcweight,percentage) select rownumber, d.percentmatch, @constval, d.percentmatch * @constval dbo.matchdate(@date) d inner join dbo.patients p on d.rownumber = p.rowid inner join dbo.resultsstored rs on rs.rowid = d.rownumber p.modifiedat > rs.modifiedat i needed check if modifiedat value of rows returned dbo.matchdate(@date) function dbo.patients table greater modifiedat of rows in resultsstored table rowid's same returned dbo.matchdate(@date). since dbo.matchdate(@date) returns rowid , percentmatch, modifiedat of rows, joined patients table on rowid column. comparison sake, modifiedat resultsstored table, joined further dbo.resultsstored on rowid column only.
problem : results in duplicated rows inserted i.e not getting rows dbo.matchdate(@date) dbo.resultsstored table not @ intended. dbo.resultsstored check modifiedat value same row id's.
how may modify above query avoid duplication of rows in #temp2 because of dbo.resultsstored table ?
i think can use exists check want:
insert #temp2 (rownumber,valfromfunc,funcweight,percentage) select rownumber, d.percentmatch, @constval, d.percentmatch * @constval dbo.matchdate(@date) d exists ( select 1 dbo.patients p inner join dbo.resultsstored rs on rs.rowid = d.rownumber (d.rownumber = p.rowid) , (p.modifiedat > rs.modifiedat) ) this insert #temp2 records tvf, provided there at least one record, same rowid, dbo.patients modifiedat greater modifiedat of corresponding record dbo.resultsstored.
Comments
Post a Comment