Question

I have written a query for select some results from different tables with a union all. After the from you can use the where. In that where I want to use a column that I 'made' in the query. How can I do this?

example

 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

Is that possible to take the K1 and set in into the where? Or is there an other option?


UPDATE: New example

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**

As you can see I want in the first select K1 en K3 and in the second I want K2 end K4

I hope you can solve this problem too, this was a good solution.

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
Was it helpful?

Solution

You can use it, but as a result of your subquery

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

UPDATED

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???

OTHER TIPS

Unfortunately, you can't use the ALIAS on the WHERE clause that was created on the same level. There are two options that can do:

Use the original column:

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

Or wrap it with subquery:

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

You'll need to use a subquery or 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

You can check for Column1 instead of K1 since K1 is alias name hence cannot be used in where condition. Like:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top