如何在结果集中选择UNION ALL操作

时间:2009-08-11 05:36:38

标签: sql sql-server tsql

SELECT * FROM 
(SELECT BAR.DIAGNOSES FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.DIAGNOSES IS NOT NULL)
UNION ALL
SELECT BAR.UNDERLYINGCAUSE FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.UNDERLYINGCAUSE IS NOT NULL)
UNION ALL
SELECT BAR.UNDERLYINGCAUSE2 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.UNDERLYINGCAUSE2 IS NOT NULL)
UNION ALL
SELECT BAR.UNDERLYINGCAUSE3 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.UNDERLYINGCAUSE3 IS NOT NULL)
UNION ALL
SELECT BAR.UNDERLYINGCAUSE4 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.UNDERLYINGCAUSE4 IS NOT NULL)
UNION ALL
SELECT BAR.UNDERLYINGCAUSE5 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.UNDERLYINGCAUSE5 IS NOT NULL)
UNION ALL
SELECT BAR.COMPLICATIONS FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.COMPLICATIONS IS NOT NULL)
UNION ALL
SELECT BAR.COMPLICATIONS2 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.COMPLICATIONS2 IS NOT NULL)
UNION ALL
SELECT BAR.COMPLICATIONS3 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.COMPLICATIONS3 IS NOT NULL)
UNION ALL
SELECT BAR.COMPLICATIONS4 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.COMPLICATIONS4 IS NOT NULL)
UNION ALL
SELECT BAR.COMPLICATIONS5 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.COMPLICATIONS5 IS NOT NULL)
UNION ALL
SELECT BAR.OTHERDIAGNOSES FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.OTHERDIAGNOSES IS NOT NULL)
UNION ALL
SELECT BAR.OTHERDIAGNOSES2 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.OTHERDIAGNOSES2 IS NOT NULL)
UNION ALL
SELECT BAR.OTHERDIAGNOSES3 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.OTHERDIAGNOSES3 IS NOT NULL)
UNION ALL
SELECT BAR.OTHERDIAGNOSES4 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.OTHERDIAGNOSES4 IS NOT NULL)
UNION ALL
SELECT BAR.OTHERDIAGNOSES5 FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN WHERE FOO.ADMWARD IN (16,17) AND (BAR.OTHERDIAGNOSES5 IS NOT NULL))

结果消息102,级别15,状态1,行32''''附近的语法不正确。

3 个答案:

答案 0 :(得分:4)

在最后一个paren之后添加一个别名:

select
    *
from
   (.... unions go here ...) a

您不需要将其包装在select *中以获取结果集,但我假设您将其用作子查询。当您将子查询作为结果集进行操作时,需要对其进行别名,否则,列不知道它们来自何处。

答案 1 :(得分:0)

你不需要外部的select * from - 基本上只是将所有选择与union all交错 - 它应该可以工作。

答案 2 :(得分:0)

但当然,你的意思是“UNPIVOT”。

   SELECT * FROM 
   (SELECT FOO.ADMWARD, BAR.* FROM FOO INNER JOIN BAR ON FOO.AN = BAR.AN 
        WHERE FOO.ADMWARD IN (16,17)
   ) t
   UNPIVOT 
   (Diag FOR PropType in (DIAGNOSES
        , UNDERLYINGCAUSE
        , UNDERLYINGCAUSE2
        , UNDERLYINGCAUSE3
        , UNDERLYINGCAUSE4
        , UNDERLYINGCAUSE5
        , COMPLICATIONS
        , COMPLICATIONS2
        , COMPLICATIONS3
        , COMPLICATIONS4
        , COMPLICATIONS5
        , OTHERDIAGNOSES
        , OTHERDIAGNOSES2
        , OTHERDIAGNOSES3
        , OTHERDIAGNOSES4
        , OTHERDIAGNOSES5
        )
   ) u;

不确定要调用列的内容。你可能想要找出一个适当的子查询来输入't'。这也将为你处理NULL。