1

我编写了一个查询,用于从不同的表中选择一些结果,并使用 union all。在from之后你可以使用where。在那我想使用我在查询中“制作”的列。我怎样才能做到这一点?

例子

 SELECT    
    [column1] AS 'K1', 
    ISNULL([column2], 'else')  + ' ' + ISNULL([column3], 'else') AS 'K2'    
 FROM 
    [table]  
 FULL JOIN
    [table] ON A.Name = C.AccountIdName 
 WHERE
    A.Name IS NOT NULL  
    AND K1 IS NOT NULL

有没有可能把K1放在哪里?还是有其他选择?


更新:新示例

SELECT    
        [column1] AS 'K1', 
        ISNULL([column2], 'else')  + ' ' + ISNULL([column3], 'else') AS 'K2'    
     FROM 
        [table]  
     FULL JOIN
        [table] ON A.Name = C.AccountIdName 
     WHERE
        A.Name IS NOT NULL  
        **AND K1 IS NOT NULL
        AND K3 IS NOT NULL**

UNION ALL

SELECT    
        [column1] AS 'K1', 
        ISNULL([column2], 'else')  + ' ' + ISNULL([column3], 'else') AS 'K2'    
     FROM 
        [table]  
     FULL JOIN
        [table] ON A.Name = C.AccountIdName 
     WHERE
        A.Name IS NOT NULL  
        **AND K2 IS NOT NULL 
        AND K4 IS NOT NULL**

如您所见,我想要在第一个选择 K1 en K3 中,在第二个中我想要 K2 end K4

我希望你也能解决这个问题,这是一个很好的解决方案。

SELECT * FROM 
(
 SELECT [column1] as 'K1' , ISNULL([column2], 'else') + ' ' + ISNULL([column3], 'else') as 'K2' 
 FROM [table] full join [table] on A.Name = C.AccountIdName where A.Name IS NOT NULL
) 
res WHERE res.K1 IS NOT NULL
4

4 回答 4

0

不幸的是,您不能使用在同一级别上创建ALIAS的子句。WHERE有两个选项可以做到:

使用原始列:

SELECT  [column1] as 'K1' , 
        ISNULL([column2], 'else') + ' ' + ISNULL([column3], 'else') as 'K2' 
FROM    [table] full join [table] 
          on A.Name = C.AccountIdName 
where   A.Name IS NOT NULL AND [column1] IS NOT NULL

或者用子查询包装它:

SELECT K1, K2
FROM
(
    SELECT  [column1] as 'K1' , 
            ISNULL([column2], 'else') + ' ' + ISNULL([column3], 'else') as 'K2' 
    FROM    [table] full join [table] 
              on A.Name = C.AccountIdName
    where   A.Name IS NOT NULL
) s
WHERE K1 IS NOT NULL
于 2013-04-17T09:03:53.223 回答
0

您需要使用子查询或 CTE:

;WITH CTE AS
(
 SELECT    
    [column1] AS 'K1', 
    ISNULL([column2], 'else')  + ' ' + ISNULL([column3], 'else') AS 'K2'    
 FROM 
    [table]  
 FULL JOIN
    [table] ON A.Name = C.AccountIdName 
 WHERE
    A.Name IS NOT NULL
) 
SELECT    
   K1, K2
FROM 
   CTE
WHERE
   K1 IS NOT NULL
于 2013-04-17T09:04:05.757 回答
0

您可以检查Column1而不是K1因为K1是别名,因此不能在 where 条件下使用。喜欢:

SELECT 
    [column1] as 'K1' , 
    ISNULL([column2], 'else') + ' ' + ISNULL([column3], 'else') as 'K2' 
FROM [table] full join [table] on A.Name = C.AccountIdName 
where 
    A.Name IS NOT NULL AND 
    [column1] IS NOT NULL
于 2013-04-17T09:04:44.230 回答
0

您可以使用它,但由于您的子查询

SELECT * FROM 
(
 SELECT [column1] as 'K1' , ISNULL([column2], 'else') + ' ' + ISNULL([column3], 'else') as 'K2' 
 FROM [table] full join [table] on A.Name = C.AccountIdName where A.Name IS NOT NULL
) 
res WHERE res.K1 IS NOT NULL

更新

SELECT * FROM 
(
   SELECT [column1] as 'K1' , ISNULL([column2], 'else') + ' ' + ISNULL([column3], 'else') as 'K2' 
   FROM [table] full join [table] on A.Name = C.AccountIdName where A.Name IS NOT NULL
) 
res1 WHERE res1.K1 IS NOT NULL AND res1.K3 IS NOT NULL
UNION ALL
SELECT * FROM 
(
   SELECT [column1] as 'K1' , ISNULL([column2], 'else') + ' ' + ISNULL([column3], 'else') as 'K2' 
   FROM [table] full join [table] on A.Name = C.AccountIdName where A.Name IS NOT NULL
) 
res2 WHERE res2.K2 IS NOT NULL AND res2.K4 IS NOT NULL //BUT DOES IT HAS K4???
于 2013-04-17T09:02:51.553 回答