0

下面的代码出现了一个奇怪的错误。No column was specified for column 2 of 'no1'. 据我所知,它应该可以工作。我只是想收集有关客户在收据上不止一次存在的客户的详细信息。

SELECT
    CM.ClientID,
    CPN.Birthdate,
    CM.ClientPassword
FROM 
    dbo.ClientMaster AS CM
    JOIN dbo.ClientPerson AS CPN ON (CM.ClientID = CPN.ClientID)
    JOIN dbo.ClientProduct AS CP ON (CPN.ClientID = CP.ClientID)
WHERE 
    CM.ClientID IN (
                    SELECT 
                        no1.ClientID 
                    FROM 
                        (
                        SELECT 
                            CM.ClientID,
                            COUNT(*)
                        FROM 
                            dbo.ClientMaster AS CM
                            JOIN dbo.ClientPerson AS CPN ON (CM.ClientID = CPN.ClientID)
                            JOIN dbo.ClientProduct AS CP ON (CPN.ClientID = CP.ClientID)
                        WHERE
                            CP.PolicyNo IN (SELECT PolicyNo FROM Receipts)
                            AND CM.ClientID IS NOT NULL
                            AND Birthdate IS NOT NULL
                            AND ClientPassword IS NOT NULL
                        GROUP BY
                            CM.ClientID
                        HAVING 
                            COUNT(*)>1
                        ) AS no1
                    )

解决方案

啊没有意识到你不需要Count(*). Select多谢你们!这就是我最终的结果。

SELECT DISTINCT
    CM.ClientID,
    CPN.Birthdate,
    CM.ClientPassword
FROM 
    dbo.ClientMaster AS CM
    JOIN dbo.ClientPerson AS CPN ON (CM.ClientID = CPN.ClientID)
    JOIN dbo.ClientProduct AS CP ON (CPN.ClientID = CP.ClientID)
WHERE 
    CM.ClientID IN (
                        SELECT 
                            ClientID 
                        FROM 
                            Receipts
                        GROUP BY
                            ClientID
                        HAVING 
                            COUNT(*)>1
                        )
    AND CM.ClientID IS NOT NULL
    AND Birthdate IS NOT NULL
    AND ClientPassword IS NOT NULL
4

2 回答 2

2

您的 COUNT(*) 列需要一个名称,例如:

 COUNT(*) AS RecordCount
于 2012-07-03T08:22:50.073 回答
1

您不需要为此嵌套两次相同的查询。你可以这样做:

SELECT
    CM.ClientID,
    CPN.Birthdate,
    CM.ClientPassword
FROM 
    dbo.ClientMaster AS CM
    JOIN dbo.ClientPerson AS CPN ON (CM.ClientID = CPN.ClientID)
    JOIN dbo.ClientProduct AS CP ON (CPN.ClientID = CP.ClientID)
GROUP BY CM.ClientID, CPN.Birthdate, CM.ClientPassword
HAVING COUNT(CM.ClientID) > 1
于 2012-07-03T08:31:14.547 回答