sql server 2008 - Get only rows where 2 conditions are fulfilled in Microsoft SQL -
i have table mapping table t1. looks following:
+-------+---------+-------------+ | reqid | fieldid | listitemid | +-------+---------+-------------+ | 219 | 76 | 3548 | | 219 | 86 | 2382 | | 220 | 76 | 3548 | | 220 | 86 | 3491 | | 221 | 77 | 3550 | | 221 | 87 | 2387 | +-----------------+-------------+
now want todo select distinct reqids has both of following
select * t1 (fieldid='76' , listitemid='3548') or (fieldid='77' , listitemid='3550') or ((fieldid='86' , (listitemid='3491' or listitemid='2380')) or (fieldid='87' , (listitemid='3494' or listitemid='2386'))) order requirementid
this gives me rows has 1 of above requirements. want todo get
select * t1 ((fieldid='76' , listitemid='3548') or (fieldid='77' , listitemid='3550')) , (((fieldid='86' , (listitemid='3491' or listitemid='2380')) or (fieldid='87' , (listitemid='3494' or listitemid='2386')))) order requirementid
the above don't return rows. points want todo.
what query should return 220 since thats rows contain both fieldid=76 , listitemid=3548 , fieldid=86 , listitemid=3491.
is done in query or have write 2 separate querys , create 2 arrays , compare ids occurs in both arrays?
you had it. need select reqid
matching conditions, , rows reqid
. can accomplished sub-query.
done using 2 sub-queries:
select * t1 reqid in ( select t11.reqid t1 t11 (t11.fieldid='76' , t11.listitemid='3548') or (t11.fieldid='77' , t11.listitemid='3550') ) , reqid in ( select t11.reqid t1 t11 (t11.fieldid='86' , (t11.listitemid='3491' or t11.listitemid='2380')) or (t11.fieldid='87' , (t11.listitemid='3494' or t11.listitemid='2386')) ) order requirementid
this can further translated single sub-query using join
.
select * t1 reqid in ( select t11.reqid t1 t11 join t1 t12 on t11.reqid = t12.reqid ((t11.fieldid='76' , t11.listitemid='3548') or (t11.fieldid='77' , t11.listitemid='3550')) , ( (t12.fieldid='86' , (t12.listitemid='3491' or t12.listitemid='2380')) or (t12.fieldid='87' , (t12.listitemid='3494' or t12.listitemid='2386')) ) ) order requirementid
Comments
Post a Comment