Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...