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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query of history question 1

Status
Not open for further replies.

skiflyer

Programmer
Sep 24, 2002
2,213
US
Ok, I believe I can boil this down to one table to ask my question... Say I have the below data set, we'll call the first column letters, the second numbers. I want to return all the letters which have a number entry which is 1 and a number entry which is 5... can I do this without using temporary tables? I'm almost sure I can, but am totally stumped at the moment.

A 1
A 2
B 4
B 5
C 1
C 5

Thanks,
Rob
 
No, I meant where it has been both 1 and 5... here's what I got... what I'm really doing is looking through a history table, and finding items which have been in two different states within a given time range. Anyway, I think I've solved it... for anyone who may come later

Code:
SELECT tbl1.letter
FROM tblname AS tbl1
JOIN tblname AS tbl2 ON tbl1.letter=tbl2.letter
WHERE tbl1.number=1
AND tbl2.number=5

But, if anyone sees a problem with this, please let me know.

-Rob
 
that's good for two conditions

for three or more, it gets rather awkward

this works too:

[tt]select letter
from tblname
where number = 1
or number = 5
group
by letter
having count(*) = 2[/tt]

the nice part about this you can easily see how to extend it to more conditions

rudy
 
Granted... however, if a letter has visited 1 twice, it will be a false positive.

-Rob
 
Sorry for the double reply... and if it has visited 1 twice, and 5 once, it will be a false negative.

If it's a unidirectional non-repeating graph (I forget the proper acronym) that would work fine, unfortunately, I don't have that luxury.

-Rob
 
oh ye of little faith

just change it to

... having count(distinct number) = 2


rudy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top