Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can I use the betwwen operator three times to query three different ranges of values

Status
Not open for further replies.

capronton

MIS
Jun 28, 2013
159
US
Can someone get the query below to work? I need to query several different ranges of values in one query.

select ts,tr.loc_n,bus,route,ttp,grp,des,seq,tpbc

from tr, ppd

where ts >= '2013-09-01 04:00:00.000' and ts <= '2013-10-01 03:59:00.000'
and tr.loc_n=ppd.loc_n
and tr.id=ppd.id
and tr.tr_seq=ppd.tr_seq
and seq between 9262 and 9333
or seq between 9348 and 9361
or seq between 9396 and 9427
and tpbc in (100)
and ttp in (25)

order by ts;
 
Code:
select ts,tr.loc_n,bus,route,ttp,grp,des,seq,tpbc
from tr, 
      ppd
where ts >= '2013-09-01 04:00:00.000' 
  and ts <= '2013-10-01 03:59:00.000'
  and tr.loc_n=ppd.loc_n
  and tr.id=ppd.id
  and tr.tr_seq=ppd.tr_seq
  and (seq between 9262 and 9333
   or seq between 9348 and 9361
   or seq between 9396 and 9427)
  and tpbc in (100)
  and ttp in (25)
order by ts;
Note the ()'s. Order of operations is probably giving you more records than you wanted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top