我遇到了根据mtb.mid从以下查询中选择数据的问题。 我想要的是在最高的mtb.mid上选择下面的查询基础,但我无法弄清楚我需要把这个原因放在我的where子句或其他地方....
这是我的精细且有效的查询,它给我的结果,我需要添加一个我上面谈过的过滤器。
SELECT DISTINCT TOP (100) PERCENT wTB.Week, wTB.Description, wTB.wid, mTB.mid
FROM yTB INNER JOIN
mTB ON yTB.yid = mTB.yid INNER JOIN
wTB ON mTB.mid = wTB.mid INNER JOIN
dTB ON wTB.wid = dTB.wid
WHERE (dTB.dateEn <= CAST(GETDATE() AS DATE))
ORDER BY wTB.Week
提前致谢。
答案 0 :(得分:1)
这样可行。
SELECT DISTINCT TOP (100) PERCENT wTB.Week, wTB.Description, wTB.wid, mTB.mid
FROM yTB INNER JOIN
mTB ON yTB.yid = mTB.yid INNER JOIN
wTB ON mTB.mid = wTB.mid INNER JOIN
dTB ON wTB.wid = dTB.wid
WHERE (dTB.dateEn <= CAST(GETDATE() AS DATE)) AND
mTB.mid = (SELECT MAX(mid)
FROM mTB)
ORDER BY wTB.Week
将该条件放在WHERE子句中,并使用Subquery从mTB表中获取最高的mTB.mid值。