SQL - 如何组合UNION

时间:2016-10-03 06:23:40

标签: sql sql-server union

更新:

根据要求,这是'真正的'SQL:

    SELECT      p.ReferenceNumber, 
            p.Name, 
            '' as 'Size of Prize',
            '' as 'Complexity', 
            pfc.ConfigurationValue as 'Strategic Fit'
FROM        FormConfigurations AS fc INNER JOIN
                      ProjectForms AS pf ON pf.FormID = fc.FormID INNER JOIN
                      ProjectFormConfigurations AS pfc ON pfc.ProjectFormID = pf.ID AND pfc.FormConfigurationID = fc.ID INNER JOIN
                      Project AS p ON p.ID = pf.ProjectID
WHERE     (fc.[Key] = 'Strategic Fit') 
GROUP BY p.ReferenceNumber, p.Name, pfc.ConfigurationValue, fc.[Key]

UNION

SELECT      p.ReferenceNumber, 
            p.Name, 
            '' as 'Size of Prize',
            pfc.ConfigurationValue as 'Complexity', 
            '' 'Strategic Fit'
            FROM         FormConfigurations AS fc INNER JOIN
                      ProjectForms AS pf ON pf.FormID = fc.FormID INNER JOIN
                      ProjectFormConfigurations AS pfc ON pfc.ProjectFormID = pf.ID AND pfc.FormConfigurationID = fc.ID INNER JOIN
                      Project AS p ON p.ID = pf.ProjectID
WHERE     (fc.[Key] = 'Complexity') 
GROUP BY p.ReferenceNumber, p.Name, pfc.ConfigurationValue, fc.[Key]

UNION

SELECT      p.ReferenceNumber, 
            p.Name, 
    pfc.ConfigurationValue  as 'Size of Prize',
    '' as 'Complexity',
    '' as 'Strategic Fit'
    FROM         FormConfigurations AS fc INNER JOIN
                      ProjectForms AS pf ON pf.FormID = fc.FormID INNER JOIN
                      ProjectFormConfigurations AS pfc ON pfc.ProjectFormID = pf.ID AND pfc.FormConfigurationID = fc.ID INNER JOIN
                      Project AS p ON p.ID = pf.ProjectID
WHERE     (fc.[Key] = 'Projected EBIT') 
GROUP BY p.ReferenceNumber, p.Name, pfc.ConfigurationValue, fc.[Key]

我有一个具有KeyValue列的表,该列充当占位符,用于设计时未知的列,这是另一个大型软件所必需的。我需要根据KeyValue列中的某些值获取记录;在这种情况下,让我们调用所需的值Foo,Bar和Baz。检索到的每条记录必须包含所有三个值,否则它们根本不会显示。

以下是我尝试使用工会的查询:

SELECT  KeyValue as 'Foo',
        '' as 'Bar', 
        '' as 'Baz' 
FROM        Table1
WHERE     KeyValue = 'Foo' 
UNION
SELECT  '' as 'Foo',
        KeyValue as 'Bar',
        '' as 'Baz'
FROM        Table1
WHERE     KeyValue = 'Bar' 
SELECT  '' as 'Foo',
        '' as 'Bar', 
        KeyValue as  'Baz'
FROM        Table1
WHERE     KeyValue = 'Baz' 

不幸的是,发生的事情是我填充了所有列的锯齿状记录而不是一条记录。也就是说,而不是我得到如下所示的所需记录:

Foo     Bar     Baz
 -       -       -
 -       -       -
 -       -       -
 -       -       -
 -       -       -

我得到了这些:

Foo     Bar     Baz
 -              
         -       
                 -
 -              
         -       
                 -

我需要做些什么来纠正我的查询?

4 个答案:

答案 0 :(得分:0)

试试这个

select t1.KeyValue as 'Foo', t2.KeyValue as 'Bar', t3.KeyValue as 'Baz',
from Table1 t1, Table1 t2, Table1 t3
where t1.KeyValue = 'Foo' and t2.KeyValue = 'Bar' and t3.KeyValue = 'Baz'

答案 1 :(得分:0)

您需要使用的是选择子查询而不是联合。 union会将多个行源合并为1行源,但您需要的是,对于单行,您需要从多个表中获取值。

示例:

select *, 
  (select KeyValue from table1 where somekey = outertable.somekey) as Foo
from outertable where somerestrictions

答案 2 :(得分:0)

如果我理解了您的要求,那么有很多方法可以获得您提到的记录,其中一种简单的方法是:

SELECT  KeyValue as 'Foo',
        (SELECT  KeyValue FROM zzz WHERE KeyValue = 'Bar') as 'Bar', 
        (SELECT  KeyValue FROM zzz WHERE KeyValue = 'Baz') as 'Baz'
FROM table1
WHERE KeyValue = 'Foo' 

答案 3 :(得分:0)

首先,你没有把一个'联盟'在第三次选择查询之前

如果你想消除重复的行,你应该使用' UNION ALL'如下所示:



SELECT  KeyValue as 'Foo',
        '' as 'Bar', 
        '' as 'Baz' 
FROM        Table1
WHERE     KeyValue = 'Foo' 
UNION All
SELECT  '' as 'Foo',
        KeyValue as 'Bar',
        '' as 'Baz'
FROM        Table1
WHERE     KeyValue = 'Bar' 
UNION All
SELECT  '' as 'Foo',
        '' as 'Bar', 
        KeyValue as  'Baz'
FROM        Table1
WHERE     KeyValue = 'Baz' 




如果你不想消除重复,那么你应该只使用' UNION'如下所示:



SELECT  KeyValue as 'Foo',
        '' as 'Bar', 
        '' as 'Baz' 
FROM        Table1
WHERE     KeyValue = 'Foo' 
UNION
SELECT  '' as 'Foo',
        KeyValue as 'Bar',
        '' as 'Baz'
FROM        Table1
WHERE     KeyValue = 'Bar' 
UNION
SELECT  '' as 'Foo',
        '' as 'Bar', 
        KeyValue as  'Baz'
FROM        Table1
WHERE     KeyValue = 'Baz'




我认为应该解决你的问题。 如果你想要,你可以看到'UNION'之间的区别。和' UNION ALL'。

谢谢