3

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

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 Outer Query 我可以执行以下操作,但测试两个查询,它似乎慢了很多。

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]
4

3 回答 3

11
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 ...
于 2013-01-05T03:03:23.417 回答
3

如果您不想使用联合,因为您必须多次重复相同的 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”

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

于 2013-01-05T04:12:45.937 回答
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
于 2013-01-06T04:24:23.933 回答