sql where子句查询

时间:2014-02-28 00:23:06

标签: sql sql-server

我将几个xml文件导入到我的数据库中。我有多个survery ID和每个调查的多个varname,每个都有一个值。

我已经能够得到我需要的结果,但我不确定我是否正确地做到了这一点。 我不完全确定如何编写查询以选择所需的调查ID ..

where varname in ('age') and value >18

会给我所有参与者年龄超过18岁的调查ID

但是如果我有多个变量而且有些是数字...所以我不能写> 18如果我有其他变量也是数字...

如何将值与该varname相关联?

SURVEY_ID   VARNAME VALUE
674078265   PROVID  provider name
674078265   SEX Female
674078265   age 55
674078265   SP  Internal Med
674078265   ID# 12345
674111111   ADJSAMP Included
674111111   PROVID  provider name2
674111111   SEX Male
674111111   age 34
674111111   SP  Surgery
674111111   ADJSAMP Included
674111111   ID# 6789

3 个答案:

答案 0 :(得分:2)

SELECT *
FROM TableName 
WHERE SURVEY_ID IN (SELECT SURVEY_ID
                    FROM TableName
                    WHERE VARNAME = 'age'
                    AND VALUE > 18)

或者更有效的方式

SELECT *
FROM TableName t
WHERE EXISTS (SELECT 1
              FROM TableName 
              WHERE SURVEY_ID = t.SURVEY_ID
              AND VARNAME = 'age'
              AND VALUE > 18)

OR

SELECT t1.*
FROM TABLE_Name t1 LEFT JOIN TABLE_Name t2
ON t1.SURVEY_ID = t2.SURVEY_ID
WHERE t2.VARNAME = 'age' 
AND   t2.VALUE > 18

答案 1 :(得分:0)

扩展@ M.Ali回答以实际回答您的问题 - 根据您的需要为所有现场条件添加任意数量:

SELECT *
FROM TableName t
WHERE EXISTS (SELECT 1
          FROM TableName 
          WHERE SURVEY_ID = t.SURVEY_ID
          AND VARNAME = 'age'
          AND VALUE > 18)
  and EXISTS (SELECT 1
          FROM TableName 
          WHERE SURVEY_ID = t.SURVEY_ID
          AND VARNAME = 'sex'
          AND VALUE = 'Male')

答案 2 :(得分:0)

我实际上会提供两个答案的混合,而不仅仅是一个查询,而是为什么。

看来你想要一个给定调查的所有元素,只要它符合你正在寻找的特定标准......这,我会赞扬M.Ali的第一个答案,但第二个基于你可能感兴趣的多个标准in。

但是,为了进一步分析数据,每次调查的多个记录可能不实际......相反,我会选择交叉表结果。要对记录进行评估,它将是一个优化的。

首先,从简单查询开始,以获得符合多个条件的调查。在这种情况下,我会在所有三个字段的表上有一个索引(SURVEY_ID,VARNAME,VALUE)

SELECT 
      s1.*
   FROM 
      YourTable s1
         JOIN ( SELECT s2.SURVEY_ID
                           FROM YourTable s2

                              JOIN YourTable s3
                                 on s2.survey_id = s3.survey_id
                                 AND s3.varname = 'SEX'
                                 AND s3.value = 'Male'

                              JOIN YourTable s4
                                 on s2.survey_id = s4.survey_id
                                 AND s3.varname = 'ADJSAMP'
                                 AND s3.value = 'Included'

                          WHERE s2.VARNAME = 'age'
                            AND s2.VALUE > 18 ) PreQual
         on s1.SURVEY_ID = PreQual.Survey_ID

因此,在内部查询中,您可以以任一方式交换条件,并为更多条件(或更少)添加其他联接。无论你怀疑什么是最小的结果标准,我会把“年龄”> 18 where where子句,并调整其他子句。

因此,内部查询首先开始获取年龄>在图18中,通过在相同的调查ID上添加附加连接(别名s3),但是不同的标准几乎就像并排放置列以进行分析。如果它们不存在,整个调查将被忽略。找到合格的调查ID后,将连接到主表以获取所有行元素。

现在,让它更易于管理。放入每列的交叉表。只需更改选择字段条件并为调查ID添加GROUP BY,但查询的其余部分将保持不变。

SELECT 
      s1.survey_id,
      max( case when varname = 'PROVID' then value end ) as Provider,
      max( case when varname = 'SEX' then value end ) as Gender,
      max( case when varname = 'age' then value end ) as Age,
      max( case when varname = 'SP' then value end ) as Specialty,
      max( case when varname = 'ID#' then value end ) as IDNumber,
      max( case when varname = 'ADJSAMP' then value end ) as AdjustSample
   FROM
      [rest of original query sample above]
   GROUP BY
      s1.survey_id

现在,如果您没有应用任何条件过滤,则上述查询会为您提供类似......

的结果
survey_id  Provider        Gender  Age  Specialty     IDNumber  AdjustSample
674078265  provider name   Female  55   Internal Med  12345     Included
674111111  provider name2  Male    34   Surgery       6789      Included