Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hi,
i have table like this schéma
STATION_ID|SENSORNAME|TIME_TAG  |VALUE
AIN SEBOU |NIVEAU    |2015-03-01|20
AIN SEBOU |NIVEAU    |2015-03-02|70
AIN SEBOU |PLUIE1hr  |2015-03-01|20
AIN SEBOU |PLUIE1hr  |2015-03-02|10
AZZABA    |NIVEAU    |2015-03-01|20
AZZABA    |NIVEAU    |2015-03-02|70
AZZABA    |PLUIE1hr  |2015-03-01|20
AZZABA    |PLUIE1hr  |2015-03-02|10

i want to seelect ligne wich containe STATION_ID = AIN SEBOU and AZZABA and for
SENSORNAME NIVEAU and PLUIE1hr for AIN SEBOU and NIVEAU for AZZABA

i have use this qyery but i get all (sensor name)
SQL
select *
from XC_DATA1
where (STATION_ID='AIN SEBOU'or STATION_ID= 'AZZABA')
and (SENSORNAME='NIVEAU' OR SENSORNAME='PLUIE1HR' )


but me i want rslt like this
AIN SEBOU |NIVEAU    |2015-03-01|20
AIN SEBOU |NIVEAU    |2015-03-02|70
AIN SEBOU |PLUIE1hr  |2015-03-01|20
AIN SEBOU |PLUIE1hr  |2015-03-02|10
AZZABA    |NIVEAU    |2015-03-01|20
AZZABA    |NIVEAU    |2015-03-02|70

for primary key of my table are STATION_ID,SENSORNAME and TIME_TAG
Posted
Updated 29-Jul-15 4:04am
v3

To open up you WHERE clause...
SQL
STATION_ID = 'AIN SEBOU' AND SENSORNAME = 'NIVEAU')
OR
(STATION_ID = 'AIN SEBOU' AND SENSORNAME = 'PLUIE1HR')
OR
(STATION_ID = 'AZZABA' AND SENSORNAME = 'NIVEAU')
OR
(STATION_ID = 'AZZABA' AND SENSORNAME = 'PLUIE1HR')

Where the last line actually not as you requested...
So what you need is...
SQL
STATION_ID = 'AIN SEBOU' AND SENSORNAME = 'NIVEAU')
OR
(STATION_ID = 'AIN SEBOU' AND SENSORNAME = 'PLUIE1HR')
OR
(STATION_ID = 'AZZABA' AND SENSORNAME = 'NIVEAU')

To make it short
SQL
(STATION_ID = 'AIN SEBOU' AND (SENSORNAME = 'NIVEAU' OR SENSORNAME = 'PLUIE1HR')
OR
(STATION_ID = 'AZZABA' AND SENSORNAME = 'NIVEAU')
 
Share this answer
 
Comments
Member 11573837 29-Jul-15 9:54am    
hi Kornfeld Eliyahu Peter,
thank you a lot
Kornfeld Eliyahu Peter 29-Jul-15 9:56am    
It is always a good thing to start with a more detailed WHERE clause that works and after that simplify it...
Wendelius 29-Jul-15 10:12am    
Nice answer, 5
Kornfeld Eliyahu Peter 29-Jul-15 10:20am    
Thank you...
Just to throw another option, I often like to use IN since it makes the query a bit more simplified and often removes the need for additional parenthesis around OR section.

In other words to modify the good answer from Kornfeld Eliyahu Peter you can use something like
SQL
SELECT a.STATION_ID, 
       a.SENSORNAME,
       a.TIME_TAG,
       a.VALUE
FROM XC_DATA1 a
WHERE (STATION_ID = 'AIN SEBOU' AND SENSORNAME IN ('NIVEAU', 'PLUIE1HR'))
OR    (STATION_ID = 'AZZABA' AND SENSORNAME IN ('NIVEAU'))

The latter IN could also be equality but I used IN to keep the comparisons similar.

See IN (Transact-SQL)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900