我正在尝试返回具有最新到期日的行,并且在到期日有匹配的情况下,我会返回价格最高的记录。但是,看起来我的代码中的分组不正确,我不确定问题是什么。
drawable.setColorFilter(0xffff0000, PorterDuff.Mode.SRC_ATOP);
SELECT
dap.CompanyID AS CompanyId,
MAX(dap.Price) AS Price,
dap.AssetClass,
dap.AsOfDate,
MAX(dap.MaturityDate) AS MaturityDate
FROM TA.DailyAssetPricing dap WITH(NOLOCK)
WHERE dap.AssetClass = 'Bond' AND dap.MaturityDate IS NOT NULL AND dap.AsOfDate = '2018-02-02' AND dap.CompanyID = 59303
GROUP BY dap.CompanyID, dap.AssetClass, dap.AsOfDate, dap.MaturityDate
答案 0 :(得分:2)
您通常GROUP BY
所选列,但设置函数的参数除外。
在这种情况下:
GROUP BY dap.CompanyID, dap.AssetClass, dap.AsOfDate
答案 1 :(得分:0)
如果您想要具有最长到期日的行,则不需要group by
。你可以这样做:
SELECT TOP (1) WITH TIES dap.*
FROM TA.DailyAssetPricing dap
WHERE dap.AssetClass = 'Bond' AND dap.MaturityDate IS NOT NULL AND
dap.AsOfDate = '2018-02-02' AND dap.CompanyID = 59303
ORDER BY dap.MaturityDate DESC;