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

Can't think of the right query

Status
Not open for further replies.

abenstex

Programmer
Jan 9, 2005
47
DE
Hi everyone,

i have a table with one field called "formulaId", another called "criterion" and another called "value". The thing is that for each formula, there are more criteria possible and of course more values as each criterion needs a value. For example:

formulaId criterion value
fm01 length 6000
fm01 printed yes

So now i want the formulaId where the criterion is length = 6000 AND printed='yes' but if i create a where clause like:
select formulaId from mytable where length=6000 and printed='yes' it does not retrieve anything.
What am i doing wrong?
 
Your SQL is failing because printed and length are not names of fields. The design of the table makes this a difficult query but I'd try a subquery:
Code:
Select formulaId from myTable 
  Where criterion = "length" and value=6000 and
  formulaID In 
  (Select formulaId from myTable 
     Where criterion = "printed" and value="yes")

Geoff Franklin
 
going the subquery route will be difficult when there are more than two conditions
Code:
select formulaId 
  from myTable 
 where criterion='length' and value='6000'
    or criterion='printed' and value='yes'
group
    by formulaId
having count(*) = 2

r937.com | rudy.ca
 
Thanks! That seems to be exactly what i need.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top