简体   繁体   中英

T-SQL Force Select results to have a Primary Key

I have a large set of imperfect data, from this data I reverse engineering a table for the coding used.

For this particular task, it is know that all records with a specific division code should all have the same group ID and plan ID (which are not included in the data) from another source I been able to add a close but imperfect (and incomplete) mapping of the group ID and plan ID. Now I want to work backwards and build a division mapping table. I have gotten data down to a format like this:

Division Year   Group   Plan    Cnt
52       2019   30      101    9031
52       2020   30      101    9562
54       2019   60      602    3510
54       2020   60      602    3385
56       2019   76      904    1113
56       2020   76      905    1125
56       2020   76      001    6

The Division and Year columns should from a primary key. As you can see 56, 2020 is not unique, but by looking at the cnt column it is easy to see that the record with a count of 6 is a bad record and should be dropped.

What I need is a method to return each division and year pair once with the group and plan IDs that have the largest count.

Thank You

I found the answer using the Rank() function and WHERE clause:

SELECT *
FROM (
SELECT Division, Year, Group, Plan_Cd
     , RANK() OVER (PARTITION BY Division, Year ORDER BY Cnt DESC ) AS 'rk'
FROM DivisionMap ) R
WHERE rk = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM