SQL Server - WHERE Clauses for column dynamic -
i have following table,
id idattribute valueattribute idpiece 1 9 '2048' 10 2 18 'ddr3' 10 3 9 '2048' 11 4 18 'ddr3' 12
when doing query:
select * tb_inventary_report (idatribute = 9 , valueatribute = '2048') or (idatribute = 18 , valueatribute = 'ddr3')
i returning records 1, 2, 3 , 4, want return 1 , 2 because have same idpiece, can not inform idpiece, ie, idpiece has iqual between ors pass
how can return 1 , 2?
using exists()
aggregation query count(*)
rows match conditions, having count(*) =
number of conditions need match.
select * tb_inventary_report t exists ( select 1 tb_inventary_report i.idpiece = t.idpiece , ( (idattribute = 9 , valueattribute = '2048') or (idattribute = 18 , valueattribute = 'ddr3') ) group i.idpiece having count(*) = 2 )
rextester demo: http://rextester.com/tkxcj68213
returns:
+----+-------------+----------------+---------+ | id | idattribute | valueattribute | idpiece | +----+-------------+----------------+---------+ | 1 | 9 | 2048 | 10 | | 2 | 18 | ddr3 | 10 | +----+-------------+----------------+---------+
wiki
Comments
Post a Comment