我正在尝试获取子查询,该子查询将计算在特定时间段内向个人收取的案件数量。相反,当前版本报告在该时间段内计算的每个费用,而不仅仅是个人。如何修改以下查询以输出与每个BookedLastName关联的唯一tblCsCases.CauseNumber的计数?我需要BookedLastNames选择受一般WHERE by ChargeDescription,Statute和ChargeLanguage的限制,但同样的限制不应该应用于CrimeCount,只应受日期限制。
System.out.printf("Hi %s. Let's practice addition!", name);
答案 0 :(得分:0)
您需要将COUNT链接到BookedLastName / BookedFirstName(或标识某人的其他唯一标识符)。为此,您需要使用表别名或两个别名。因此,请尝试以下查询:
SELECT
substring (Cases.FileNumber,4,6) AS [FileNumber]
,Charge.ChargeCode
,Cases.BookedLastName
,Cases.BookedFirstName
,(SELECT COUNT(c2.CauseNumber)
FROM tblCsCases c2
WHERE c2.IssuedDate >= @EndDate
AND c2.IssuedDate <= GETDATE()
AND c2.BookedLastName = Cases.BookedLastName
AND c2.BookedFirstName = Cases.BookedFirstName
) AS CrimeCount
FROM tblCsCases as Cases
INNER JOIN tblCsCharge as Charge
ON Cases.FileNumber = Charge.FileNumber
WHERE
Cases.IssuedDate >= @StartDate
AND Cases.IssuedDate <= @EndDate
AND Charge.Statute IN (N'571.030')
AND Charge.ChargeDescription LIKE '%sub%'
AND Charge.ChargeLanguage LIKE '%exhibit%'
ORDER By
Cases.BookedLastName