我编写了一个查询,用于从不同的表中选择一些结果,并使用 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