question

faranakm avatar image
faranakm asked

please help me to write a scipt in sqlserver 2005

I have a table as below: I want to write a script that return all of _level = 2 and (all _level = 1 which have not _parent_id.it means the _parent_id of _level = 1 not used in _parent_id) I hope that I explain this clearly. Thanks,
sql-server-2005sql-serverscript
1.jpg (39.8 KiB)
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

KenJ avatar image KenJ commented ·
Homework? I would recommend reading up on the WHERE clause - http://technet.microsoft.com/en-us/library/ms188047.aspx
4 Likes 4 ·

1 Answer

·
Grant Fritchey avatar image
Grant Fritchey answered
From the looks of things, you'll probably need to look to a [UNION query][1] to combine the two result sets that you've defined. This is because, logically, you can't just return _level = 2 and combine it with any of the values where _level = 1 inside of a single statement. So, instead, make two statements and combine them. Probably you'll want to use UNION ALL for performance reasons. Read up in the link I sent. [1]: http://technet.microsoft.com/en-us/library/ms180026.aspx
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.