我希望有人可以帮助我,我一整天都在努力。
我需要获取最大值,以及特定日期范围之间最大值关联的日期和ID。
这是我的代码,我尝试过很多不同的版本,但它仍然会返回多个ID和日期
SELECT distinct bw_s.id, avs.carProd, cd_s.RecordDate,
cd_s.milkProduction as MilkProd,
cd_s.WaterProduction as WaterProd
FROM tblTest bw_s
INNER JOIN tblTestCp cd_s WITH(NOLOCK)
ON bw_s.id=cd_s.id
AND cd_s.recorddate BETWEEN '08/06/2014' AND '10/05/2014'
Inner Join
( select id, max(CarVol) as carProd
from tblTestCp
where recorddate BETWEEN '08/06/2014' AND '10/05/2014'
group by id ) avs
on avs.id = bw_s.id
order by id
我有这样的表
id RecordDate carProd MilkProd WaterProd
47790 2014-10-05 132155 0 225
47790 2014-10-01 13444 0 0
47790 2014-08-06 132111 10 100
47790 2014-09-05 10000 500 145
47790 2014-09-20 10000 800 500
47791 2014-09-20 10000 300 500
47791 2014-09-21 10001 400 500
47791 2014-08-21 20001 600 500
结果应该是(max carprod)
id RecordDate carProd MilkProd WaterProd
47790 2014-10-05 132155 0 225
47791 2014-08-21 20001 600 500
答案 0 :(得分:0)
我认为你的牌桌名称是"数据":
SELECT
*
FROM
Data
WHERE
Data.RecordDate BETWEEN '2014-08-21' AND '2014-10-01'
ORDER BY
Data.carProd DESC
LIMIT 1;
请务必更改日期以符合您的特定要求。