我有一个查询需要为每个 ID 返回本月的最低余额。我遇到的问题是它返回多个余额而不是余额上的最小余额。我不断得到这样的结果:
ID Name Month Year TodayMonth TodayYear BalMin
1 A 4 12 4 2012 10,000.00
1 A 4 12 4 2012 20,000.00
当我需要它返回最低余额时:
ID Name Month Year TodayMonth TodayYear BalMin
1 A 4 12 4 2012 10,000.00
这是我到目前为止所拥有的:
SELECT DISTINCT
TOP (100) PERCENT History.ID, info.Name, DATEPART(mm, History.ReportDate) AS Month, DATEPART(yy,History.ReportDate) AS Year, DATEPART(mm, { fn CURDATE() }) AS TodayMonth, DATEPART(yy, { fn CURDATE() }) AS TodayYear, MIN(History.Balance) AS BalMin
FROM History LEFT OUTER JOIN Info ON History.ID = Info.ID
WHERE (DATEPART(yy, History.ReportDate) = DATEPART(yy, { fn CURDATE() })) AND (DATEPART(mm, History.ReportDate) = DATEPART(mm,
{ fn CURDATE() })) AND (History.Balance > 0)
GROUP BY History.ID, History.ReportDate, Info.Name, History.Balance
ORDER BY History.ID