SQL Server 微软SQL服务器:分组依据错误

rjjhvcjd  于 2022-12-10  发布在  其他
关注(0)|答案(1)|浏览(149)

I'm new to Microsoft SQL Server 2014. I run this SQL code:

SELECT TOP(10) 'DBSG' as seek_entity, * 
FROM DBSG..PM00200

and get this result:

Next, I want to find out total line items for that entity with code below.

WITH vw_pm00200_all AS
(
    SELECT TOP(10) 
        'DBSG' as seek_entity, *    
    FROM 
        DBSG..PM00200
)
SELECT
    seek_entity,
    COUNT(*) AS total
FROM
    vw_pm00200_all
GROUP BY
    1

Sadly, I get this error. I have no idea why it failed.
Msg 164, Level 15, State 1, Line 9
Each GROUP BY expression must contain at least one column that is not an outer reference.
Lastly, please advise is Microsoft SQL Server based on Transact-SQL ?

rqqzpn5f

rqqzpn5f1#

It looks like you are running into this problem here: Each GROUP BY expression must contain at least one column that is not an outer reference
As the answer points out, grouping by a constant literal is pointless as it is the same for all results. Count() will return the same result as Count() with a GROUP BY.
If this is just test code and you plan on using a CASE statement (with different values) in place of the string literal, you may have better luck.
Yes, T-SQL is Microsoft SQL Server's flavor of SQL.

相关问题