2014-10-01 57 views
0

我有正確打印以下:如何從GROUP BY中選擇?

MonthNumber 
Users 
UserHits 

我也需要從dbo.ReportAudienceOverviews

如何更改SQL印刷從Coefficient列?

SELECT DATEPART(mm, S.DateProcessStatus) as MonthNumber, 
    --S.DeviceId, 
    SUM(A.Users) as Users, 
    SUM(A.UsersHits) as UserHits 
    FROM dbo.ReportAudienceOverviews A 
    LEFT JOIN dbo.ReportProcessStatuses S 
    ON S.StatusId = A.StatusId 
    AND S.DateProcessStatus >= '2014' AND S.DateProcessStatus < '2015' 
    --WHERE S.DeviceId = 1 AND A.Coefficient = 20 
    GROUP BY 
    DATEPART(mm, S.DateProcessStatus), S.DeviceId 
    ORDER BY MonthNumber ASC, S.DeviceId ASC 
+5

有可能,一般情況下是Coefficient'的'*多*值,它集合了行從這個查詢中形成單個輸出行 - 那麼應該如何選擇特定的「係數」值?或者他們應該是加在一起? – 2014-10-01 08:27:43

+1

您有幾個選項,例如'Coefficient = MIN(A.Coefficient)'或'..MAX(Coefficient)'或'SUM(Coefficient)'或'AVG(Coefficient)'。如果你想要一個特定行的「係數」,你必須解釋你的邏輯。 – 2014-10-01 08:29:15

+0

我對MIN(A.Coefficient) – GibboK 2014-10-01 08:30:05

回答

2

你只需要包括與MIN(Coefficient)列(如在評論中提及):

SELECT DATEPART(mm, S.DateProcessStatus) as MonthNumber, 
--S.DeviceId, 
SUM(A.Users) as Users, 
SUM(A.UsersHits) as UserHits, 
MIN(A.Coefficient) as Coefficient 
FROM dbo.ReportAudienceOverviews A 
LEFT JOIN dbo.ReportProcessStatuses S 
ON S.StatusId = A.StatusId 
AND S.DateProcessStatus >= '2014' AND S.DateProcessStatus < '2015' 
--WHERE S.DeviceId = 1 AND A.Coefficient = 20 
GROUP BY 
DATEPART(mm, S.DateProcessStatus), S.DeviceId 
ORDER BY MonthNumber ASC, S.DeviceId ASC