我有一個查詢應該計算用戶根據唯一會話ID登錄到兩個不同版本的軟件的次數。然而,在我的外部選擇陳述中,我的計數數量太多了。例如,我爲一個用戶獲得了31000個會話,這是不正確的。它應該是更像40的東西。爲什麼會發生這種情況?爲什麼我的SQL Count語句計算次數太多
SELECT X.FirstName, X.LastName, X.CompanyName, X.AQ8Sessions, AQ360Sessions = COUNT(RRUI.SessionId)
FROM(
SELECT RRUI.UserId, RRUI.FirstName, RRUI.LastName, RRUI.CompanyName, COUNT(distinct RRUI.SessionId) AQ8Sessions
FROM Authentication.dbo.RegReportUserInfo RRUI
INNER JOIN Authentication.dbo.RegReportSessions RRS
ON RRUI.SessionId = RRS.SessionId
INNER JOIN WebCatalog.Published.People P
ON P.PKey = RRUI.UserId
WHERE RRUI.ClientType = 'aq8' AND RRS.ExpiresAt <= '2013-11-24 23:59:59.999'
AND RRS.ExpiresAt >= '2013-11-18 00:00:00.000' AND RRUI.CompanyName NOT LIKE 'AutoQuotes%'
AND P.EMail NOT LIKE '%@aqnet.com'
GROUP BY RRUI.FirstName, RRUI.LastName, RRUI.CompanyName, RRUI.UserId
) X
INNER JOIN Authentication.dbo.RegReportSessions RRS
ON RRS.UserId = X.UserId
AND RRS.ExpiresAt <= '2013-11-24 23:59:59.999'
AND RRS.ExpiresAt >= '2013-11-18 00:00:00.000'
LEFT OUTER JOIN Authentication.dbo.RegReportUserInfo RRUI
ON X.UserId = RRUI.UserId AND RRUI.ClientType = 'aq360'
GROUP BY X.FirstName, X.LastName, X.CompanyName, X.AQ8Sessions
ORDER BY X.AQ8Sessions DESC, COUNT(RRUI.SessionId) DESC
是否意味着一定的聯繫RegReportUserInfo和RegReportUserSessions?因爲目前,來自其中一個匹配行的每個匹配行將與另一個匹配行匹配。 –
請向我們展示表格定義,包括關鍵字。 – RBarryYoung
INNER JOIN Authentication.dbo.RegReportSessions RRS ON RRUI.SessionId = RRS.SessionId和RRUI.UserId = RRS.UserId – user2615302