在同一个表中的两列联盟

时间:2013-01-05 03:00:16

标签: sql sql-server

假设我在同一个表中有以下两列

Column 1
--------
1
2
3

Column 2
--------
4
5
6

我如何得到一个给我的结果:

Columns
--------
1
2
3
4
5
6

修改

我真正想要的是确保没有更有效的方法在同一个表中的多个列上键入联合查询,而不必重复多次表,并为每个表重复where条件联盟多次。

实际查询看起来更像是这样:

WITH T1 AS 
( SELECT [Col1] FROM [Table1] 
)
SELECT * FROM (
    SELECT [Cols1-100], COUNT(*) as "Count" FROM (

        SELECT [Col-1] FROM [Table2] AS [Cols1-100], [T1] 
        WHERE [Table2].[Col-1] = [T1].[Col-1] 
        AND [Col-1] != '2' AND [Col-2] != '2' ..... etc ..... AND [Col-100] != '2'
        UNION ALL

        SELECT [Col-2] FROM [Table2] AS [Cols1-100], [T1] 
        WHERE [Table2].[Col-1] = [T1].[Col-1] 
        AND [Col-1] != '2' AND [Col-2] != '2' ..... etc ..... AND [Col-100] != '2'
        UNION ALL

        ....................... etc
        ....................... etc
        ....................... etc
        .... etc

        SELECT [Col-100] FROM [Table2] AS [Cols1-100], [T1] 
        WHERE [Table2].[Col-1] = [T1].[Col-1] 
        AND [Col-1] != '2' AND [Col-2] != '2' ...... etc .... AND [Col-100] != '2'

    ) as [Temp1]
    GROUP BY [Cols1-100]    
) as [Temp2]

使用@Bohemian外部查询我可以执行以下操作但是测试两个查询,它似乎要慢得多。

WITH T1 AS 
( SELECT [Col1] FROM [Table1] 
)
SELECT * FROM (
    SELECT [Cols1-100], COUNT(*) as "Count" FROM (
            SELECT * FROM (
                SELECT [Col-1] AS [Cols1-100], [Col-1], [Col-2], ..etc.. [Col-100] FROM [Table2] 
                UNION ALL
                SELECT [Col-2] AS [Cols1-100], [Col-1], [Col-2], ..etc.. [Col-100] FROM [Table2] 
                UNION ALL
                ....................... etc
                .... etc
                SELECT [Col-100] AS [Cols1-100], [Col-1], [Col-2], ..... etc ..... [Col-100] FROM [Table2] 
            ) AS SUBQUERY WHERE [Col-1] IN (SELECT [Col1] FROM [T1])
            AND [Col-1] != '2' AND [Col-2] != '2' ..... etc ..... AND [Col-100] != '2' 
        ) as [Temp1]
    GROUP BY [Cols1-100]    
) as [Temp2]

3 个答案:

答案 0 :(得分:9)

select column1 as columns from mytable
union
select column2 from mytable

使用union删除重复项(在某些数据库上也可以排序) 如果您想保留重复项,请使用union all

select column1 as columns from mytable
union all
select column2 from mytable

编辑:

要添加where子句,简单但低效的执行方式是将其添加为外部查询:

select * from (
    select column1 as columns from mytable
    union
    select column2 from mytable ) x
where columns ...

更有效的执行方式,但是一个痛苦的长查询,是将它放在每个子查询上:

select column1 as columns from mytable
where ....
union
select column2 from mytable
where ...

答案 1 :(得分:2)

如果你不想使用union,因为你必须多次重复相同的where子句,那么有一个[非常糟糕的]解决方法:

select decode(j.col, 1, column1, 2, column2)
from table t
join (select 1 as col from dual union select 2 from dual) j
 on 1 = 1
where (your where clause)

这个例子来自oracle,在SQLServer上你不需要“from dual”

另外,如果你有很多列要加入(真的不应该发生),你可以在“加入”中使用分层查询来避免大量的“联合”

答案 2 :(得分:0)

你试过UNPIVOT吗?这取决于您的SQL Server版本,但以下示例适用于SQL 2008:

DECLARE @t TABLE (Col1 INT, col2 INT, col3 INT)
INSERT INTO @t
        ( Col1, col2, col3 )
VALUES  ( 1, -- Col1 - int
          2, -- col2 - int
          3  -- col3 - int
          ),
          ( 4, -- Col1 - int
          5, -- col2 - int
          6  -- col3 - int
          )


 SELECT  cols
 FROM (SELECT col1, col2, col3 FROM @t) pvt
 UNPIVOT
 (Cols FOR ID IN (col1, col2, col3))  unpvt     
 WHERE cols <> 2