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
328 views
in Technique[技术] by (71.8m points)

sql - select unique rows based on column by least group count

I need to build a SQL query to select unique records from below table by least number of category count.

eg : simon is falling in to both Red and Green Category but red is having only one record since i should give preference to red.

Name Category
Simon Green
Simon red
James Green
Mathew Green

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

1 Reply

0 votes
by (71.8m points)

Using analytic functions make this problem tractable:

WITH cte1 AS (
    SELECT *, COUNT(*) OVER (PARTITION BY Category) cat_cnt
    FROM yourTable
),
cte2 AS (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY Name ORDER BY cat_cnt) rn
    FROM cte1
)

SELECT Name, Category
FROM cte2
WHERE rn = 1;

Demo

The first CTE finds the color count across the entire table for each color (regardless of name). The second CTE restricts to the first record per name having the lower color count.

Regarding your using Knex, we could try to rewrite the above without using analytic functions, but it would be very ugly. I might actually suggest just a raw query here.


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

...