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 |
ssunny
Posting Yak Master
133 Posts |
Posted - 2012-05-30 : 16:06:03
|
Hello Guys,I need some urgent help.Here's the table with some sample data:create table test_insert(mid int null,mname varchar (500) null,nid int null,total int null)insert into test_insertvalues (1,'NULL',0,230)insert into test_insertvalues (1,'test',111,230)insert into test_insertvalues (2,'test1',222,567)insert into test_insertvalues (3,'NULL',0,657)goselect * from test_insert give me mid mname nid total1 NULL 0 2301 test 111 2302 test1 222 5673 NULL 0 657Expected output:mid mname nid total1 test 111 2302 test1 222 5673 NULL 0 657In above case I have more than one row for mid 1 and with nid 0 and 111 so i don't want to show row with nid 0. So condition would be : don't show a record where nid = 0 only if there is already a record where nid > 0 for the same mid.Thanks. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-30 : 16:09:10
|
[code]SELECT mid, mname, nid, totalFROM(SELECT *,SUM(CASE WHEN nid >0 THEN 1 END) OVER ( PARTITION BY mid) AS OccFROM table)tWHERE Occ =0OR nid >0[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
ssunny
Posting Yak Master
133 Posts |
Posted - 2012-05-30 : 16:15:39
|
Hi Visakh,Thanks for the reply but your query gives me mid mname nid total1 test 111 2302 test1 222 567I want mid mname nid total1 test 111 2302 test1 222 5673 NULL 0 657since there is only one record with mid = 3 I want that too.Thanks. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-30 : 16:33:49
|
quote: Originally posted by ssunny Hi Visakh,Thanks for the reply but your query gives me mid mname nid total1 test 111 2302 test1 222 567I want mid mname nid total1 test 111 2302 test1 222 5673 NULL 0 657since there is only one record with mid = 3 I want that too.Thanks.
sorry there was a typoSELECT mid, mname, nid, totalFROM(SELECT *,SUM(CASE WHEN nid >0 THEN 1 ELSE 0 END) OVER ( PARTITION BY mid) AS OccFROM #test_insert)tWHERE Occ =0OR nid >0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
ssunny
Posting Yak Master
133 Posts |
Posted - 2012-05-30 : 16:39:47
|
Perfect. Thank you sir. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-30 : 17:17:03
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|