Raw data:
| Category | Serial Number |
| ------------ | ------------ |
| Sports | 3462 |
| Sports | 4373 |
| Sports | 3453 |
| Auto | 7742 |
| Auto | 1247 |
| Auto | 3463 |
| Auto | 1242 |
| Auto | 7746 |
My goal with select query is to get that data looking like this:
Category | Serial Number Count | Example Serial Number |
---|---|---|
Sports | 3 | 3462 |
Auto | 4 | 7742 |
My current query:
SELECT
Category,
COUNT( [Serial Number] ) AS [Serial Number Count]
FROM
abcDB
GROUP BY
Category
The above query has everything aside from that last column where I just need ONE of the serial numbers from each category as an example. It doesn't matter which serial number it is as long as it falls into the correct category.
Can someone please edit my query so it includes a column like this? Thank you.
2条答案
按热度按时间pcrecxhr1#
Please try the following solution.
Window function
ROW_NUMBER()
is very handy for such scenario.SQL
Output
yvfmudvl2#
The following might work if you were to return a random row in a group
fiddle