查询为court = MA返回128行,因此MAFiledDaily
计数应为128。
结果不正确。
/ ******来自SSMS ****** /
的SelectTopNRows命令的脚本SELECT
dbo.tblCourtRec.CourtLtrsCurr as [Court],
dbo.tblSentences.CaseNo as [CaseNo],
dbo.tblSentences.DispDate as [DispDate],
dbo.tblSentences.DispDate as [DispMethod],
-- Get totals by Court
(SELECT count(caseno)
FROM dbo.tblCaseFiling
WHERE filedate = CASE
WHEN datepart(weekday, getdate()) = 2
THEN datediff(day, 3, getdate())
ELSE datediff(day, 1, getdate())
END
AND tblcourtrec.CourtLtrsCurr = 'MA') AS MAFiledDaily
FROM
[MF_COURT].[dbo].[tblSentences]
INNER JOIN
dbo.tblCourtRec ON dbo.tblCourtRec.CaseNo = dbo.tblSentences.CaseNo
WHERE
dbo.tblCourtRec.CourtLtrsCurr = 'MA'
AND YEAR(dbo.tblSentences.DispDate) = YEAR(getdate())
AND MONTH(dbo.tblSentences.DispDate) = MONTH(getdate())
ORDER BY
dbo.tblCourtRec.CourtLtrsCurr
示例数据:
Court CaseNo DispDate DispMethod MAFiledDaily
MA M1004861 01/05/2015 DISM 269
MA M1354445 01/05/2015 REVW 269
MA M1354445 01/05/2015 DISM 269
MA M1363238 01/05/2015 DISM 269
MA M1365908 01/05/2015 NAOG 269
答案 0 :(得分:0)
更新的答案
这是你的子查询(重新格式化了一点,只是为了帮助我更好地理解它):
-- Get totals by Court
(select count(caseno)
from dbo.tblCaseFiling
where filedate = CASE
WHEN datepart(weekday,getdate())= 2
THEN datediff(day,3,getdate())
ELSE datediff(day,1,getdate())
END
and tblcourtrec.CourtLtrsCurr = 'MA'
)
假设您的filedate
比较是正确的,我认为问题仍然是您引用子查询之外的表,但不是限制您在子查询中检索的记录。
您的外部查询引用表tblCourtRec
并限制返回dbo.tblCourtRec.CourtLtrsCurr ='MA'
的记录。
您的子查询也引用了tblCourtRec
- 但它引用了外部查询中的值。您返回的记录与外部查询中的特定记录之间的子查询中没有链接。您可以将引用到外部查询中的记录,但您无法以任何方式将其连接到tblCaseFiling
。
假设tblCaseFiling
中有一些数据表明提交的状态,您需要这样的条款:
where [file date comparison]
and tblCaseFiling.CourtFiledIn = 'MA'
...或者像这样(这就是所谓的相关子查询):
where [file date comparison]
and tblCaseFiling.CourtFiledIn = tblCourtRec.CourtLtrsCurr