The column "dbo.User.FB_UserId" is not valid in the select list because it is not contained in the aggregate function or in the GROUP BY clause

I have seen this error message many times here, but I am not getting a solution to my specific problem (perhaps because I am not an sql expert), so please forgive me for sending the question to the same error.

This is the query I'm trying to execute:

SELECT DISTINCT U.FB_UserId
 , U.Id AS GameUserID
 , U.FbLocale
 , U.FbGender
 , U.FbBirthday
 , U.RegistredAt
 , U.LoginCount
 , U.PlayCount
 , U.MarketGroupId

    , (SELECT COUNT(C.PriceFbCredits)
    WHERE UserID = U.Id) AS Payments

    , (SELECT SUM(CASE WHEN C.PriceFbCredits = 13 THEN 1 END)
    WHERE UserID = U.Id) AS P13

    , (SELECT SUM(CASE WHEN C.PriceFbCredits = 52 THEN 1 END)
    WHERE UserID = U.Id) AS P52

    , (SELECT SUM(CASE WHEN C.PriceFbCredits = 130 THEN 1 END)
    WHERE UserID = U.Id) AS P130

FROM [dbo].[User] AS U WITH (NOLOCK) INNER JOIN [dbo].[FbCreditsCallback] AS C WITH (NOLOCK) ON C.UserId = U.Id

If yes, I get an error. Well, I know what this means, and I understand what to do, but I think that if I do this, it will not give me the result that I want ... I need some data for a certain one userid. some data must be summarized, some of them must be calculated, and each identifier should appear only once in the list of results.

( ). SELECT-Queries, , . , :

, (SELECT COUNT(PriceFbCredits)
FROM [dbo].[FbCreditsCallback]
WHERE UserID = U.Id) AS Payments

... , , .

+2
2

SQL Server .

WHERE UserID = U.Id , INNER JOIN.

, :

SELECT DISTINCT U.FB_UserId
 , U.Id AS GameUserID
 , U.FbLocale
 , U.FbGender
 , U.FbBirthday
 , U.RegistredAt
 , U.LoginCount
 , U.PlayCount
 , U.MarketGroupId
 , COUNT(*)  AS Payments
 , SUM(CASE WHEN C.PriceFbCredits = 13 THEN 1 ELSE 0 END) AS P13
 , SUM(CASE WHEN C.PriceFbCredits = 52 THEN 1 ELSE 0 END) AS P52
 , SUM(CASE WHEN C.PriceFbCredits = 130 THEN 1 ELSE 0 END) AS P130

FROM [dbo].[User] AS U WITH (NOLOCK) 
INNER JOIN [dbo].[FbCreditsCallback] AS C WITH (NOLOCK) ON C.UserId = U.Id
GROUP BY U.FB_UserId
 , U.Id 
 , U.FbLocale
 , U.FbGender
 , U.FbBirthday
 , U.RegistredAt
 , U.LoginCount
 , U.PlayCount
 , U.MarketGroupId

sql-expert

WITH (NOLOCK), , SELECT [data] FROM [TABLE] WITH(I really, really don't care if it is accurate or not)

, SQL, , .

0

GROUP BY, select

, ()

0

All Articles