0

Below is an example of my pricing_supports table with more than 3 millions rows. There some rows having same values in the first_origin_id, delivery_id fields. If there are records with same first_origin_id, delivery_id but different source, I only want to select those records where source = 0

ID code first_origin_id delivery_id source
1 A 10 20 0
2 B 10 20 1
3 C 11 21 1
4 D 12 22 0
5 E 12 22 1

I would like result like that:

ID code first_origin_id delivery_id source
1 A 10 20 0
3 C 11 21 1
4 D 12 22 0

How can I do for good performance?

tinlyx
3,84014 gold badges50 silver badges79 bronze badges
asked Sep 20, 2018 at 4:24

1 Answer 1

1

By using ROW_NUMBER() and PARTITION BY first_origin_id, delivery_id then ORDER BY source, you can achieve your expected result.

So the query will be:

SELECT ID, code, first_origin_id, delivery_id, source FROM (
 SELECT ID, code, first_origin_id, delivery_id, source, 
 ROW_NUMBER() OVER (PARTITION BY first_origin_id, delivery_id ORDER BY source, ID) AS RowNumber
 FROM pricing_supports
) Q
WHERE Q.RowNumber = 1

Please find the Live Demo

answered Sep 20, 2018 at 6:21
1
  • It's work, but not best way for performance. I think. thanks @Arulkumar Commented Sep 20, 2018 at 9:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.