3

我将这个问题简化为一个非常简单的形式:

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

2 回答 2

6

当你想要一个派生表时,你需要GROUP BY一个UNION

SELECT X.[Literal], [Count] = COUNT(*)
FROM(
    SELECT [Literal] = 'A'
    UNION ALL 
    SELECT [Literal] = 'B'
) X
GROUP BY X.[Literal]

演示

于 2013-06-11T15:33:08.297 回答
1

你可以用c...

;WITH cte AS (
 SELECT
 'A' AS [Literal]
 UNION 
 SELECT
 'B' AS [Literal])
SELECT [Literal]
FROM cte
GROUP BY [Literal]
于 2013-06-11T18:10:55.600 回答