具有多列的子查询

时间:2015-01-06 12:21:19

标签: sql sql-server select group-by where

我的数据库中有一个设计不佳的表,其中包含我需要提取的一些信息。我有以下问题:

SELECT
    (SELECT I FROM X WHERE X.A = FOO AND X.B = KEY),
    (SELECT J FROM X WHERE X.A = BAR AND X.B = KEY),
    (SELECT K FROM X WHERE X.A = BAZ AND X.B = KEY)

我需要扩展查询以选择其他一些字段。我的第一个想法是:

SELECT
    (SELECT I FROM X WHERE X.A = FOO AND X.B = KEY),
    (SELECT J FROM X WHERE X.A = BAR AND X.B = KEY),
    (SELECT K FROM X WHERE X.A = BAZ AND X.B = KEY)
    (SELECT L, M, N FROM X WHERE X.A = QUX AND X.B = KEY)

但是,我收到一条错误,指出获取列的子查询只能获取一列,除非它带有EXISTS子句。我可以写下最后一个子查询:

    (SELECT L, M, N FROM X AS OUTER
    WHERE EXISTS 
        (SELECT ID FROM X WHERE X.A = QUX AND X.B = KEY AND X.ID = OUTER.ID)(

赢得这个解决方案是不必要的沉重?还有另一个解决方案吗?

编辑:

我需要将信息提取到一行。我还应该提一下,不能保证每个X.A都存在(可能只有FOO的键,其他的有BAR和QUX等),所以加入是不可能的。

编辑:@Saharsh Shah要求提供一些样本数据:

|X.B |X.A|X.I     |X.J     |X.K     |X.L     |X.M     |X.N     |
|KEY1|FOO|ICONTENT|        |        |        |        |        |
|KEY1|BAR|        |JCONTENT|        |        |        |        |
|KEY1|BAZ|        |        |KCONTENT|        |        |        |
|KEY1|QUX|        |        |        |LCONTENT|MCONTENT|NCONTENT|
|KEY2|BAR|        |JCONTENT|        |        |        |        |
|KEY3|FOO|ICONTENT|        |        |        |        |        |
|KEY3|QUX|        |        |        |LCONTENT|MCONTENT|NCONTENT|

预期结果: 对于关键1:

X.I     |X.J     |X.K     |X.L     |X.M     |X.N     |
ICONTENT|JCONTENT|KCONTENT|LCONTENT|MCONTENT|NCONTENT|

对于密钥2:

X.I     |X.J     |X.K     |X.L     |X.M     |X.N     |
        |JCONTENT|        |        |        |        |

对于关键3:

X.I     |X.J     |X.K     |X.L     |X.M     |X.N     |
ICONTENT|        |        |LCONTENT|MCONTENT|NCONTENT|

3 个答案:

答案 0 :(得分:1)

使用 CASE 语句验证条件

试试这个:

SELECT X.B, 
       MAX(CASE WHEN X.A = FOO THEN I ELSE '' END) AS I, 
       MAX(CASE WHEN X.A = BAR THEN J ELSE '' END) AS J, 
       MAX(CASE WHEN X.A = BAZ THEN K ELSE '' END) AS K, 
       MAX(CASE WHEN X.A = QUX THEN L ELSE '' END) AS L, 
       MAX(CASE WHEN X.A = QUX THEN M ELSE '' END) AS M, 
       MAX(CASE WHEN X.A = QUX THEN N ELSE '' END) AS N
FROM X 
WHERE X.B = KEY
GROUP BY X.B;

答案 1 :(得分:0)

这可以通过两种方式完成: -

select i, j , k , l , m, n
from x 
where i in (select i from x where x.a = 'FOO' or x.B = 'KEY') and 
j in (select j from x where x.b = 'BAR' or x.B = 'KEY') and 
k in (select k from x where x.c = 'BAZ' or x.B = 'KEY')

或另一种方式是:

select (select count(i) from x where x.a = 'foo') as i  , (select count(j) from x where x.b in ('KEY', 'BAR') as j , (select count(k) from x where x.c = 'BAZ' and x.B = 'KEY') as k , l, m , n
from x

答案 2 :(得分:0)

您可以使用派生表作为此

Select foo.I, bar.J , baz.K, qux.L, qux.M ,qux.N  from x
left outer join (Select * from x where x.A = foo) foo on foo.b = x.b
left outer join (Select * from x where x.A = bar) bar on bar.b = x.b
left outer join (Select * from x where x.A = baz) baz on baz.b = x.b
left outer join (Select * from x where x.A = qux) qux on qux.b = x.b
where x.B = key 

如果您确定存在a = foo,...的数据,则内连接也适用