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 |
benni-baumgaertner
Starting Member
1 Post |
Posted - 2013-01-15 : 07:56:03
|
Hello, i hope anyone can help me with a query to the following table:id parent_id artikel_id1 NULL 875852 NULL 55973 3 NULL 187534 2 851125 2 118836 1 251237 NULL 99983The condition for the query:1) Show all records with parent_id is NULL (that's easy) AND2) Show only records which no other record include this id in the parent_id.The results should show:3 NULL 187537 NULL 99983 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2013-01-15 : 08:12:13
|
[code]-- making testdatadeclare @sample table(id int, parent_id int, artikel_id int)insert @sampleselect 1, NULL, 87585 union allselect 2, NULL, 55973 union allselect 3, NULL, 18753 union allselect 4, 2, 85112 union allselect 5, 2, 11883 union allselect 6, 1, 25123 union allselect 7, NULL, 99983-- show testdataselect * from @sample-- solutionselect * from @sample as t1where not exists(select * from @sample as t2 where t1.id = t2.parent_id)and t1.parent_id is null[/code] Too old to Rock'n'Roll too young to die. |
|
|
|
|
|
|
|