2017-09-26 138 views
0

大家下午好,SQL查詢最近更新:

我一直在努力研究這一整天,我弄不明白。我在Excel中執行此查詢。

SELECT 
PRICE_BOOK.status, 
PRICE_BOOK.ms_code, 
PRICE_BOOK.location_code, 
SUPPLIER_LOC_HDR.name, 
PRICE_BOOK.mfg_code, 
PRICE_BOOK.group_code, 
PRICE_BOOK.stock_id, 
PRICE_BOOK.effective_date, 
PRICE_BOOK.price 
FROM dbo.MS_INFO MS_INFO, dbo.PRICE_BOOK PRICE_BOOK, dbo.SUPPLIER_LOC_HDR 
SUPPLIER_LOC_HDR 
WHERE MS_INFO.ms_code = PRICE_BOOK.ms_code AND 
SUPPLIER_LOC_HDR.location_code = PRICE_BOOK.location_code AND 
((PRICE_BOOK.mfg_code Not In ('Type1','Type2','Type3'))) 

這是導出每個位置,產品,有效時間和價格的價格。

但是,我只需要每個ms_code,location_code,name,mfg_code,group_code,stock_id的唯一組合的最新「PRICE_BOOK.effective_date」。

換句話說,我只需要導出每個產品的最新價格。

status ms_code location_code name mfg_code group_code stock_id effective_date price A Supplier1 Terminal 1 City #1 PartType1 Group1 61 7/5/17 0:01 1.09 A Supplier1 Terminal 1 City #1 PartType1 Group1 61 7/4/17 0:01 1.41 A Supplier1 Terminal 1 City #2 PartType1 Group1 61 7/3/17 0:01 1.76 A Supplier1 Terminal 1 City #2 PartType1 Group1 61 5/24/17 0:01 1.20 A Supplier1 Terminal 1 City #1 PartType1 Group1 62 7/5/17 0:01 1.67 A Supplier1 Terminal 1 City #1 PartType1 Group1 62 7/4/17 0:01 1.19 A Supplier1 Terminal 1 City #1 PartType1 Group1 62 7/3/17 0:01 1.14 A Supplier1 Terminal 1 City #1 PartType1 Group1 62 5/24/17 0:01 1.11 A Supplier1 Terminal 1 City #1 PartType1 Group1 63 7/5/17 0:01 1.33 A Supplier1 Terminal 1 City #1 PartType1 Group1 63 7/4/17 0:01 1.59 A Supplier1 Terminal 1 City #1 PartType1 Group1 63 7/3/17 0:01 1.61 A Supplier1 Terminal 1 City #1 PartType1 Group1 63 5/24/17 0:01 1.75 A Supplier1 Terminal 1 City #1 PartType1 Group1 64 7/5/17 0:01 1.75 A Supplier1 Terminal 1 City #1 PartType1 Group1 64 7/4/17 0:01 1.77 A Supplier2 Terminal 1 City #1 PartType1 Group1 64 7/3/17 0:01 1.45 A Supplier2 Terminal 1 City #1 PartType1 Group1 64 5/24/17 0:01 1.77

預期結果

status ms_code location_code name mfg_code group_code stock_id effective_date price A Supplier1 Terminal 1 City #1 PartType1 Group1 61 7/5/17 0:01 1.09 A Supplier1 Terminal 1 City #2 PartType1 Group1 61 7/3/17 0:01 1.76 A Supplier1 Terminal 1 City #1 PartType1 Group1 62 7/5/17 0:01 1.67 A Supplier1 Terminal 1 City #1 PartType1 Group1 63 7/5/17 0:01 1.33 A Supplier1 Terminal 1 City #1 PartType1 Group1 64 7/5/17 0:01 1.75 A Supplier2 Terminal 1 City #1 PartType1 Group1 64 7/3/17 0:01 1.45

編輯:每個時鐘的評論更改的查詢

SELECT 
PRICE_BOOK.status, 
PRICE_BOOK.ms_code, 
PRICE_BOOK.location_code, 
SUPPLIER_LOC_HDR.name, 
PRICE_BOOK.mfg_code, 
PRICE_BOOK.group_code, 
PRICE_BOOK.stock_id, 
PRICE_BOOK.effective_date, 
PRICE_BOOK.price 
FROM dbo.PRICE_BOOK PRICE_BOOK 
    INNER JOIN dbo.SUPPLIER_LOC_HDR SUPPLIER_LOC_HDR 
     ON SUPPLIER_LOC_HDR.location_code = PRICE_BOOK.location_code 
WHERE ((PRICE_BOOK.mfg_code Not In ('Type1','Type2','Type3'))) 

由於提前,

傑夫

+2

你可以提供你的架構的一些數據來編寫和測試查詢嗎? – Santosh

+0

[SQL只能選擇列上具有最大值的行]的可能重複(https://stackoverflow.com/questions/7745609/sql-select-only-rows-with-max-value-on-a-column)。歡迎來到[tag:most-n-per-group],其他RDBMS有實用程序可以幫助您。另外,刪除隱式連接(以逗號分隔的'FROM'子句)語法,並使用顯式的'JOIN's和伴隨條件代替。 –

+0

發條,抱歉,我不是程序員。不理解你說的話。感謝幫助。 – jeffpro

回答

0

您應該使用窗口功能。這樣的事情:

qualify row_number() over(partition by product, location order by effective_date desc) = 1 
+0

'ROW_NUMBER()'在MySQL上不存在。 –

+0

https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html – Alex

+0

Pre普遍可用性草案 – Strawberry