sql server - How to filter on ROW_NUMBER() -
i trying select distinct name dataset return other columns. have working degree cant figure out how bring together.
i suspect need x( or unsure
here code , image of data returns. here want show where rn=1
shown circled in red in image
select row_number() on (partition tagname order adddate) rn, tagname, right(v_alarmswithtagname.tagname,charindex('.',reverse(v_alarmswithtagname.tagname))-1) scada_tag, convert(varchar(12) , adddate , 101) adddate, left(tagname,charindex('.',tagname)-1) 'table', [cc_note_log].dbo.scada_db.groupid 'group', [cc_note_log].dbo.scada_db.dataset 'dataset', [cc_note_log].dbo.scada_db.description 'description' "buckeye kb".dbo.v_alarmswithtagname join [cc_note_log].dbo.scada_db on right(v_alarmswithtagname.tagname,charindex('.',reverse(v_alarmswithtagname.tagname))-1)=[cc_note_log].dbo.scada_db.scada_sr_tag [cc_note_log].dbo.scada_db.groupid<>'test' , [cc_note_log].dbo.scada_db.groupid not 'keep%' , [cc_note_log].dbo.scada_db.groupid not 'delete%' , tagname not '%.es_%' , tagname not '%.opc_%'
you need common table expression (cte)
. use following syntax: ;with cte (query) select whatever cte
...please take @ below code block.
;with cte ( select row_number() on (partition tagname order adddate) rn, tagname, right(v_alarmswithtagname.tagname,charindex('.',reverse(v_alarmswithtagname.tagname))-1) scada_tag, convert(varchar(12) , adddate , 101) adddate, left(tagname,charindex('.',tagname)-1) 'table', [cc_note_log].dbo.scada_db.groupid 'group', [cc_note_log].dbo.scada_db.dataset 'dataset', [cc_note_log].dbo.scada_db.description 'description' "buckeye kb".dbo.v_alarmswithtagname join [cc_note_log].dbo.scada_db on right(v_alarmswithtagname.tagname,charindex('.',reverse(v_alarmswithtagname.tagname))-1)=[cc_note_log].dbo.scada_db.scada_sr_tag [cc_note_log].dbo.scada_db.groupid<>'test' , [cc_note_log].dbo.scada_db.groupid not 'keep%' , [cc_note_log].dbo.scada_db.groupid not 'delete%' , tagname not '%.es_%' , tagname not '%.opc_%' ) select * cte [rn] = 1
it important note cte doesn't have directly followed select
. please see following link cte guidelines: https://msdn.microsoft.com/en-us/library/ms175972.aspx
Comments
Post a Comment