在列上的多个条件上从匹配返回单行

时间:2018-01-16 11:43:59

标签: sql sql-server tsql

我在表格中跟踪数据

id  Parameter   Value
10  Location    New York
10  Business    SME
9   Location    London
9   Business    SME
8   Location    New York
8   Business    IT

我想要以下一行:

where location = New York and Business = SME

由于OR条件,下面的Query返回多行。

SELECT * from TABLEA WHERE
(Parameter='Location' AND DataValue = 'New York')
OR ( Parameter='Business' AND DataValue = 'SME')

更新:

感谢大家的回复,存在和内部联接解决了我的问题。但在我的情况下,列检查将根据某些条件动态发生。 即

 id  Parameter   Value       Value2    Value3 
 10  Location    New York    L1
 10  Business    SME         B1 
  9   Location   London      L2
  9   Business   SME         B2
  8   Location   New York    L3
  8   Business   IT          B3

是否可以开发一些查询来动态检查列?

8 个答案:

答案 0 :(得分:2)

你可以使用它。

file_names= dir('DirContainingFiles1,2,3,...');
for imgj=1: length(file_names)
    file= file_names(imgj).name;
    ......
    ......
end

答案 1 :(得分:2)

此处的其他答案可能有效,但联接可能会给查询增加不必要的成本,或者查询本身可能难以概括。

只扫描一次(无连接)并且相当一般的数据的一种方法是接受查询,汇总结果并检查是否匹配了足够数量的条件。

然后可以使用条件聚合从结果中选择单个属性/参数。

SELECT
  id,
  MAX(CASE WHEN Parameter = 'Location' THEN DataValue END)   AS Location,
  MAX(CASE WHEN Parameter = 'Business' THEN DataValue END)   AS Business
FROM
  TABLEA
WHERE
     (Parameter='Location' AND DataValue = 'New York')
  OR (Parameter='Business' AND DataValue = 'SME')
GROUP BY
  id
HAVING
  COUNT(*) = 2

查询“实体,属性,值”表时,此查询通常是合理的。在您的情况下,“实体”是id列,“属性”是parameter列,“值”是DataValue列。

然而,您无疑会发现,这些 非常差 用于搜索您正在进行的操作。这是因为许多不同的实体可能会匹配一些但不是所有的条件,所有条件都必须进行检查,这使得它非常慢。

当查询有WHERE id = ???并且您想要选择位置或业务时,它们非常快,只是它们使用它们的速度非常慢。

我建议搜索“EAV”表的用例,优化和替代方案。

答案 2 :(得分:1)

您需要将表格重新连接到自身以将两行合并为一行。例如:

SELECT 
    Businesses.ID,
    Locations.[Value] AS [Location],
    Businesses.[Value] AS Business
FROM TABLEA AS Locations
JOIN TABLEA AS Businesses
    ON Locations.ID = Businesses.ID
WHERE Locations.Parameter='Location' AND Locations.DataValue = 'New York'
AND Businesses.Parameter='Business' AND Businesses.DataValue = 'SME'

答案 3 :(得分:1)

当您想要获得与多个属性匹配的实体时,通常会在键/值表上进行聚合:

select id
from keyvalue
group by id
having count(case when parameter = 'Location' AND datavalue = 'New York' then 1 end) > 0
   and count(case when parameter = 'Business' AND datavalue = 'SME' then 1 end) > 0;

select id
from keyvalue
where (parameter = 'Location' AND datavalue = 'New York')
   or (parameter = 'Business' AND datavalue = 'SME')
group by id
having count(distinct parameter) = 2;

答案 4 :(得分:0)

您可以使用TOP(1)表达式。

SELECT TOP(1) ... columns ... FROM my_table WHERE ... filter condition ...

答案 5 :(得分:0)

AND

更改
SELECT * from TABLEA WHERE
(Parameter='Location' AND DataValue = 'New York' )
AND ( Parameter='Business' AND DataValue = 'SME')

答案 6 :(得分:0)

使用TOP关键字。

SELECT TOP 1 * from TABLEA WHERE
(Parameter='Location' AND DataValue = 'New York' )
OR ( Parameter='Business' AND DataValue = 'SME')

答案 7 :(得分:-1)

可能如下:

SELECT Top 1 * from TABLEA 
WHERE (Parameter='Location' AND DataValue = 'New York') 
AND ( Parameter='Business' AND DataValue = 'SME')
相关问题