当使用带有group by的子选择时,没有为'T1'的第1列指定列

时间:2014-07-15 19:04:07

标签: sql sql-server-2000 windows-server-2003

我有一个有效的查询:

SELECT 
    COUNT(*), ACCOUNT_ID 
FROM 
    CDS_PLAYER 
GROUP BY 
    ACCOUNT_ID 
HAVING 
    COUNT(*) > 1`

输出

No column name   Account_ID
----------------------------
    '2'          '12345'

我正在尝试为这些帐户添加名称(所有这些都来自同一张表),但没有运气。让我接近的唯一问题是:

SELECT 
    LASTNAME, FIRSTNAME, COUNT(ACCOUNT_ID) AS NUMBER
FROM 
    (SELECT 
         COUNT(*), ACCOUNT_ID 
     FROM 
         CDS_PLAYER 
     GROUP BY 
         ACCOUNT_ID 
     HAVING 
         COUNT(*) > 1) AS T1
GROUP BY 
    LASTNAME, FIRSTNAME, PLAYER_ID

但是我收到了一个错误:

  

没有为'T1'的第1列指定列

像我说的那样非常新。我4个月的老板想让我学习,所以我自学(书籍和谷歌)。任何帮助,让我到达我需要的地方将不胜感激!

(我正在使用Windows Server 2003和SQL Server 2000)

3 个答案:

答案 0 :(得分:0)

错误消息可以解决如下

SELECT LASTNAME, FIRSTNAME, COUNT(ACCOUNT_ID) AS NUMBER
 FROM 
     (SELECT COUNT(*)  AS Total, ACCOUNT_ID FROM CDS_PLAYER GROUP BY ACCOUNT_ID HAVING 
     COUNT(*) > 1) AS T1
GROUP BY LASTNAME, FIRSTNAME, PLAYER_ID`

as TOTAL

之后添加count(*)

答案 1 :(得分:0)

这样做你想要的吗?

SELECT COUNT(*), ACCOUNT_ID, LASTNAME, FIRSTNAME, PLAYER_ID
FROM CDS_PLAYER 
GROUP BY ACCOUNT_ID, LASTNAME, FIRSTNAME, PLAYER_ID
HAVING COUNT(*) > 1;

您还应该更新您的SQL Server版本。这已经过时了15年,并且多年来一直没有得到支持。您可以从Microsoft下载免费版本的SQL Server Express。

答案 2 :(得分:0)

您想要选择LASTNAMEFIRSTNAME,但是在您的子选择中没有选择它。您只能访问结果集中的字段。

解决方案:将其添加到您的GROUP BY子句中。 即:

SELECT 
    LASTNAME, FIRSTNAME, COUNT(ACCOUNT_ID) AS NUMBER
FROM 
    (SELECT COUNT(*), LASTNAME, FIRSTNAME, ACCOUNT_ID 
     FROM CDS_PLAYER 
     GROUP BY ACCOUNT_ID, LASTNAME, FIRSTNAME 
     HAVING COUNT(*) > 1) AS T1
GROUP BY 
    LASTNAME, FIRSTNAME, PLAYER_ID