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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Newbie question

Author  Topic 

Stjepan
Starting Member

5 Posts

Posted - 2010-05-12 : 09:53:35
Hello friends,

Here is my table:

NoteID <int(PK)>
Text <varchar>
Private <bit>
OperatorID <int>


I want to create a query that will give me all the "Notes" that
are not Private (Private=0), and all the "Notes" that are Private=1
but where OperatorID is e.g. equal to 5 (OperatorID=5).

I'm able to create these queries, but in a two different grids.
I want all the data in a same grid.

Please help. Thanks

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-05-12 : 09:57:41
Check out the UNION keyword. It lets you combine two select queries (if they have the same column results)

Or you can do a compound condition in the WHERE clause

WHERE
(
private = 0
OR (
Private = 1
AND operatorId = 5
)
)



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-12 : 09:58:50
where
(Private=0) or (Private=1 and OperatorId=5)


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-12 : 10:00:13
The old rocker was too late again.

I think I will go and get a coffee


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-05-12 : 10:03:42
Hi webfred.

There is a slight difference between my WHERE clause and yours.

Mine encapsulates the checks inside the same logical block and yours doesn't.

Stjepan:

Be careful with the OR -- If you have any other conditions then you'll have to include the whole private check in it's own set of parenthesis () so that your where clause doesn't become

WHERE
<lots of Checks>
OR
(Private=1 and OperatorId=5)




Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Stjepan
Starting Member

5 Posts

Posted - 2010-05-12 : 10:04:33
Thanks, working perfectly!
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-12 : 10:30:26
quote:
Originally posted by Transact Charlie

Hi webfred.

There is a slight difference between my WHERE clause and yours.

Mine encapsulates the checks inside the same logical block and yours doesn't.

Stjepan:

Be careful with the OR -- If you have any other conditions then you'll have to include the whole private check in it's own set of parenthesis () so that your where clause doesn't become

WHERE
<lots of Checks>
OR
(Private=1 and OperatorId=5)




Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION



Thanks.
Only to know if I got you:
In my posted solution there is no problem if there are no other checks - right?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-05-12 : 10:45:33
quote:

Thanks.
Only to know if I got you:
In my posted solution there is no problem if there are no other checks - right?


Yeah -- your solution is fine if there are no other checks.

This has bitten me in the past though so be careful with OR.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -