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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Simplifying "OR" and "<>" in a query

Author  Topic 

Mizi
Starting Member

2 Posts

Posted - 2012-05-03 : 06:17:24
I was wondering if there is a 'cleaner' or neat way to arrange this SQL query without repeating the same statement so many times but getting the 'wanted' result?

Wanted SQL query:
select * from Jenayah_Kekerasan 
where n_balai='BP Pulau Tikus'
and Jenis <> 'Cabul Kehormatan' and Jenis <> 'Merusuh' and Jenis <> 'Pemerasan' and Jenis <> 'Ugutan Jenayah'
and YEAR(T_Mula)=2012

OR n_balai='BP Jelutong'
and Jenis <> 'Cabul Kehormatan' and Jenis <> 'Merusuh' and Jenis <> 'Pemerasan' and Jenis <> 'Ugutan Jenayah'
and YEAR(T_Mula)=2012

OR n_balai='BP Bandar Baru'
and Jenis <> 'Cabul Kehormatan' and Jenis <> 'Merusuh' and Jenis <> 'Pemerasan' and Jenis <> 'Ugutan Jenayah'
and YEAR(T_Mula)=2012

OR n_balai='BP Jalan Patani'
and Jenis <> 'Cabul Kehormatan' and Jenis <> 'Merusuh' and Jenis <> 'Pemerasan' and Jenis <> 'Ugutan Jenayah'
and YEAR(T_Mula)=2012

OR n_balai='BP Bayan Baru'
and Jenis <> 'Cabul Kehormatan' and Jenis <> 'Merusuh' and Jenis <> 'Pemerasan' and Jenis <> 'Ugutan Jenayah'
and YEAR(T_Mula)=2012

OR n_balai='BP Bukit Mertajam'
and Jenis <> 'Cabul Kehormatan' and Jenis <> 'Merusuh' and Jenis <> 'Pemerasan' and Jenis <> 'Ugutan Jenayah'
and YEAR(T_Mula)=2012



this is what i tried to simplified that query but didn't get the same result as 'wanted' query:
select * from Jenayah_Kekerasan 
where n_balai='BP Pulau Tikus' OR n_balai='BP Jelutong' OR n_balai='BP Bandar Baru' OR n_balai='BP Jalan Patani' OR n_balai='BP Bayan Baru' OR n_balai='BP Bukit Mertajam'
and Jenis <> 'Cabul Kehormatan' and Jenis <> 'Merusuh' and Jenis <> 'Pemerasan' and Jenis <> 'Ugutan Jenayah'
and YEAR(T_Mula)=2012


I wonder where i did wrong?
thanks in advance :)

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-05-03 : 07:08:29
Try
 n_balai IN ('BP Bukit Mertajam' , 'BP Bayan Baru' ...) AND Jenis <> ....










How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-05-03 : 07:09:31
You need to put a few brackets to make the precedence clear. http://msdn.microsoft.com/en-us/library/ms190276.aspx

May be this is what you want?
select * from Jenayah_Kekerasan 
where n_balai IN ('BP Pulau Tikus' , 'BP Jelutong','BP Bandar Baru')
AND Jenis NOT IN ('Cabul Kehormatan','Merusuh','Pemerasan' ,'Ugutan Jenayah')
and YEAR(T_Mula)=2012
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-05-03 : 08:05:39
[code]SELECT *
FROM dbo.Jenayah_Kekerasan
WHERE n_balai IN ('BP Pulau Tikus', 'BP Jelutong', 'BP Bandar Baru', 'BP Jalan Patani', 'BP Bayan Baru', 'BP Bukit Mertajam')
AND Jenis NOT IN ('Cabul Kehormatan', 'Merusuh', 'Pemerasan', 'Ugutan Jenayah')
AND T_Mula >= '20120101'
AND T_Mula < '20130101'[/code]


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Mizi
Starting Member

2 Posts

Posted - 2012-05-03 : 22:05:15
yeah putting them IN brackets works! I never thought it was possible to do it that way. thank you very much.
Go to Top of Page
   

- Advertisement -