2017-09-26 24 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:great-n-per-group]へようこそ。他のRDBMSにはあなたを助けるユーティリティがあります。また、暗黙的な結合(カンマで区切られた 'FROM'句)の構文を削除し、代わりに明示的な' JOIN'とそれに伴う条件を使用してください。 –

+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

事前一般公開草稿 – Strawberry

関連する問題