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.
Author |
Topic |
mukejee
Starting Member
5 Posts |
Posted - 2014-02-11 : 04:04:05
|
Hi experts,I am having a table as given below id desc flag1 abc 02 abc 13 xyz 04 cde 0I want to write a SQL which will pull record with unique desc, also if there is a duplicate then u should get the record for which flag value is 1here out put isid desc flag2 abc 13 xyz 04 cde 0ThanksMUS |
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-02-11 : 04:39:05
|
[code];with aCTEAS( SELECT 1 as ID,'abc' as [desc],0 as [flag] union all SELECT 2, 'abc', 1 union all SELECT 3 ,'xyz',0 union all SELECT 4,'cde', 0)SELECT * from (SELECT *,ROW_NUMBER() OVER(PARTITION BY [DESC] ORDER BY [flag] desc) as rnfrom aCTE)Awhere A.rn=1[/code]SCe-am pe mine am si-n dulap, cand ma-mbrac zici ca ma mutsabinWeb MCP |
|
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-02-11 : 04:39:28
|
the outputID desc flag rn2 abc 1 14 cde 0 13 xyz 0 1 Ce-am pe mine am si-n dulap, cand ma-mbrac zici ca ma mutsabinWeb MCP |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-02-12 : 00:02:18
|
[code]SELECT *FROM Table tWHERE NOT EXISTS (SELECT 1 FROM tableWHERE desc = t.descAND flag > t.flag)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|