我将这个问题简化为一个非常简单的形式:
SELECT
'A' AS [Literal]
UNION
SELECT
'B' AS [Literal]
GROUP BY
[Literal]
我收到消息:
消息 207,级别 16,状态 1,第 7 行无效的列名称“文字”。
我尝试了这个,结果相同:
SELECT
t.literal FROM
(SELECT 'A' AS [Literal]) t
UNION
SELECT
'B'
GROUP BY
[Literal]
我将这个问题简化为一个非常简单的形式:
SELECT
'A' AS [Literal]
UNION
SELECT
'B' AS [Literal]
GROUP BY
[Literal]
我收到消息:
消息 207,级别 16,状态 1,第 7 行无效的列名称“文字”。
我尝试了这个,结果相同:
SELECT
t.literal FROM
(SELECT 'A' AS [Literal]) t
UNION
SELECT
'B'
GROUP BY
[Literal]
当你想要一个派生表时,你需要GROUP BY
一个UNION
:
SELECT X.[Literal], [Count] = COUNT(*)
FROM(
SELECT [Literal] = 'A'
UNION ALL
SELECT [Literal] = 'B'
) X
GROUP BY X.[Literal]
你可以用c...
;WITH cte AS (
SELECT
'A' AS [Literal]
UNION
SELECT
'B' AS [Literal])
SELECT [Literal]
FROM cte
GROUP BY [Literal]