我目前有一个存储过程,它返回数据并显示在我的报告查看器中,但我的问题是我检查学生是否上课,我有
COALESCE(A.Attended, 0)AS Attended
如果他们参加则返回1,如果不参加则返回0 - 在我的报告中,它只显示1或0,即使他们可以参加不止一次。如何将其转换为int以获得正确的总数。
感谢
整个查询:
SELECT
P.PartyId,
COUNT(COALESCE(A.Attended, 0))AS Attended,
COUNT(DISTINCT H.HearingId) AS Hearings,
O.OfficeName As OfficeName,
CO.Name,
P.FirstName AS FirstName,
P.LastName AS LastName,
P.BirthDate AS DOB
FROM Activity A
INNER JOIN ActivityType AT On A.ActivityTypeId = AT.ActivityTypeId
INNER JOIN ActivityEntry AE ON A.ActivityEntryId = AE.ActivityEntryId
INNER JOIN HearingEntry HE ON CAE.HearingEntryId = HE.HearingEntryId
INNER JOIN Hearing H ON HE.HearingEntryId = H.HearingEntryId
INNER JOIN [Case] C ON H.CaseId = C.CaseId
INNER JOIN CaseOffice CO ON C.CaseId = CO.CaseId AND AE.OfficeId = CO.OfficeId
INNER JOIN Office O ON CO.OfficeId = O.OfficeId
INNER JOIN Attended A ON H.HearingId = A.HearingId
INNER JOIN Party P ON A.PartyId = P.PartyId
WHERE HP.PartyId = P.PartyId AND AE.OfficeId = @OfficeId AND(H.HearingDate >= @BeginDate AND (H.HearingDate <= @EndDate OR H.HearingDate IS NULL)) AND HE.HearingEntryId = CAE.HearingEntryId
GROUP BY P.PartyId, A.Attended, O.OfficeName,CO.Name,P.FirstName, P.LastName,P.BirthDate
答案 0 :(得分:2)
我猜你的意思是A.Attented有点,你希望它是一个int,以便你以后可以聚合?
你可以像这样强制转换为int:
CAST(A.Attented AS INT)
或者在这种情况下:
COALESCE(CAST(A.Attended AS INT), 0) AS Attended
答案 1 :(得分:1)
您需要按参加人数进行汇总。
SELECT
P.PartyId,
(SUM(COALESCE(A.Attended, 0)))AS Attended,
COUNT(DISTINCT H.HearingId) AS Hearings,
O.OfficeName As OfficeName,
CO.Name,
P.FirstName AS FirstName,
P.LastName AS LastName,
P.BirthDate AS DOB
FROM Activity A
INNER JOIN ActivityType AT On A.ActivityTypeId = AT.ActivityTypeId
INNER JOIN ActivityEntry AE ON A.ActivityEntryId = AE.ActivityEntryId
INNER JOIN HearingEntry HE ON CAE.HearingEntryId = HE.HearingEntryId
INNER JOIN Hearing H ON HE.HearingEntryId = H.HearingEntryId
INNER JOIN [Case] C ON H.CaseId = C.CaseId
INNER JOIN CaseOffice CO ON C.CaseId = CO.CaseId AND AE.OfficeId = CO.OfficeId
INNER JOIN Office O ON CO.OfficeId = O.OfficeId
INNER JOIN Attended A ON H.HearingId = A.HearingId
INNER JOIN Party P ON A.PartyId = P.PartyId
WHERE HP.PartyId = P.PartyId AND AE.OfficeId = @OfficeId AND(H.HearingDate >= @BeginDate AND (H.HearingDate <= @EndDate OR H.HearingDate IS NULL)) AND HE.HearingEntryId = CAE.HearingEntryId
GROUP BY P.PartyId, O.OfficeName,CO.Name,P.FirstName, P.LastName,P.BirthDate