Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

sql - Grouping records from a table

Please see the data in this file: https://drive.google.com/file/d/1gewGZst9zwEzntK9LnwZT4j86rHMVICS/view?usp=sharing

This data represents the recipe for a particular product on a particular date (the product being identified in column pmd_ppm_pr_code and the ingredients in pmd_pi_code). What I need to do is get the most recent recipe for a particular product. Here is the SQL I put together to do this;

select
    pmd_ppm_sio_reference
    ,pmd_ppm_pr_code
    ,pmd_ppm_date_effective
    ,pmd_percentage
    ,pmd_pi_code
    ,row_number()
    over(partition by pmd_ppm_pr_code, pmd_pi_code
        order by pmd_ppm_pr_code, pmd_pi_code, trunc(pmd_ppm_date_effective) desc) as "row"
from ppm_details
where pmd_ppm_sio_reference = 1801
and pmd_ppm_pr_code = 'A24052'

The where clause is just to restrict the results so I can see the wood from the trees.

Here are the results; https://drive.google.com/file/d/1mH7n0Q5yQYukJvgR7sh94OsdUaD8sXY_/view?usp=sharing

The issue is with the last item in the group where row = 1. And that's because that ingredient wasn't used in the latest recipe, but it has given me the latest entry where it was used.

I have tried adding the date into the partition, but all that does is give me row = 1 for every line.

question from:https://stackoverflow.com/questions/66059661/grouping-records-from-a-table

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There exists a column in your data that represents the recipe -- let me just randomly guess that this is pmd_ppm_pr_code. I assume the effective date is the most recent date. Let me assume that all products in the recipe have the same effective date.

If so, you want to use dense_rank(), not row_number():

dense_rank() over (partition by pmd_ppm_pr_code order by pmd_ppm_date_effective desc) as seqnum

Your version returns the most recent date for each product separately. This returns the most recent by recipe.

Note that your order by repeats the partitioning columns. This is totally unnecessary. The order by is within a partition.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...