如何找到所有具有最新日期的站点?
How can I find all Sites with latest date?
我正在尝试添加将显示所有最新定价的所有数据区域。因为特定位置有两个最新日期,所以只有 5/6 显示日期为 5/27。我需要显示一个地点,并且它的最晚日期是 5 月 26 日。
下面的例子:
ITEMNUMBER PRICETYPE FROMDATE SITE PRICECREATEDDATE PRICE PRICEQUANTITY
12345 cost 2022-05-26 A 2022-05-26 1.23 1
12345 cost 2022-05-27 G 2022-05-27 1.23 1
12345 cost 2022-05-27 M 2022-05-27 1.23 1
12345 cost 2022-05-27 N 2022-05-27 1.23 1
12345 cost 2022-05-27 P 2022-05-27 1.23 1
12345 cost 2022-05-27 U 2022-05-27 1.23 1
我目前有这个查询:
SELECT T.ITEMNUMBER, T.PRICETYPE, T.FROMDATE, T.SITE, T.PRICECREATEDDATE, T.PRICE, T.PRICEQUANTITY
FROM DBO.[D365 STRD Costs] T
INNER JOIN (
SELECT ITEMNUMBER, MAX(FROMDATE) AS MAXDATE
FROM DBO.[D365 STRD Costs]
GROUP BY ITEMNUMBER
) TM ON T.ITEMNUMBER = TM.ITEMNUMBER AND T.FROMDATE = TM.MAXDATE
结果:
ITEMNUMBER PRICETYPE FROMDATE SITE PRICECREATEDDATE PRICE PRICEQUANTITY
12345 cost 2022-05-27 G 2022-05-27 1.23 1
12345 cost 2022-05-27 M 2022-05-27 1.23 1
12345 cost 2022-05-27 N 2022-05-27 1.23 1
12345 cost 2022-05-27 P 2022-05-27 1.23 1
12345 cost 2022-05-27 U 2022-05-27 1.23 1
我也需要 SITE "A" 拉起来,因为那是最新价格。前一天刚导入。
您似乎正在使用 SQL 服务器(或 Sybase)而不是 MySQL。
如果需要,请尝试将 SITE
添加到 GROUP BY
:
SELECT T.ITEMNUMBER, T.PRICETYPE, T.FROMDATE, T.SITE, T.PRICECREATEDDATE, T.PRICE, T.PRICEQUANTITY
FROM DBO.[D365 STRD Costs] t
INNER JOIN (
SELECT ITEMNUMBER, SITE, MAX(FROMDATE) AS MAXDATE
FROM DBO.[D365 STRD Costs]
GROUP BY ITEMNUMBER, SITE
) TM ON T.ITEMNUMBER = TM.ITEMNUMBER AND T.FROMDATE = TM.MAXDATE
AND T.SITE = TM.SITE
我正在尝试添加将显示所有最新定价的所有数据区域。因为特定位置有两个最新日期,所以只有 5/6 显示日期为 5/27。我需要显示一个地点,并且它的最晚日期是 5 月 26 日。 下面的例子:
ITEMNUMBER PRICETYPE FROMDATE SITE PRICECREATEDDATE PRICE PRICEQUANTITY
12345 cost 2022-05-26 A 2022-05-26 1.23 1
12345 cost 2022-05-27 G 2022-05-27 1.23 1
12345 cost 2022-05-27 M 2022-05-27 1.23 1
12345 cost 2022-05-27 N 2022-05-27 1.23 1
12345 cost 2022-05-27 P 2022-05-27 1.23 1
12345 cost 2022-05-27 U 2022-05-27 1.23 1
我目前有这个查询:
SELECT T.ITEMNUMBER, T.PRICETYPE, T.FROMDATE, T.SITE, T.PRICECREATEDDATE, T.PRICE, T.PRICEQUANTITY
FROM DBO.[D365 STRD Costs] T
INNER JOIN (
SELECT ITEMNUMBER, MAX(FROMDATE) AS MAXDATE
FROM DBO.[D365 STRD Costs]
GROUP BY ITEMNUMBER
) TM ON T.ITEMNUMBER = TM.ITEMNUMBER AND T.FROMDATE = TM.MAXDATE
结果:
ITEMNUMBER PRICETYPE FROMDATE SITE PRICECREATEDDATE PRICE PRICEQUANTITY
12345 cost 2022-05-27 G 2022-05-27 1.23 1
12345 cost 2022-05-27 M 2022-05-27 1.23 1
12345 cost 2022-05-27 N 2022-05-27 1.23 1
12345 cost 2022-05-27 P 2022-05-27 1.23 1
12345 cost 2022-05-27 U 2022-05-27 1.23 1
我也需要 SITE "A" 拉起来,因为那是最新价格。前一天刚导入。
您似乎正在使用 SQL 服务器(或 Sybase)而不是 MySQL。
如果需要,请尝试将 SITE
添加到 GROUP BY
:
SELECT T.ITEMNUMBER, T.PRICETYPE, T.FROMDATE, T.SITE, T.PRICECREATEDDATE, T.PRICE, T.PRICEQUANTITY
FROM DBO.[D365 STRD Costs] t
INNER JOIN (
SELECT ITEMNUMBER, SITE, MAX(FROMDATE) AS MAXDATE
FROM DBO.[D365 STRD Costs]
GROUP BY ITEMNUMBER, SITE
) TM ON T.ITEMNUMBER = TM.ITEMNUMBER AND T.FROMDATE = TM.MAXDATE
AND T.SITE = TM.SITE