[英]MSSQL aggregate (count) one column but show string based on different column from last matching row
I have a table such as below.我有一张如下表。 The
code
never changes for each type of item, but the description
may have been entered inconsistently.每种类型的商品的
code
永远不会改变,但可能输入的description
不一致。 There is no way to go back and fix old data due to regulatory requirements in our industry, but we have fixed our input form so users cannot enter incorrect descriptions moving forward.由于我们行业的监管要求,无法返回并修复旧数据,但我们已经修复了我们的输入表单,因此用户无法输入错误的描述。 In other words, the most recent descriptions are the correct descriptions that should be used.
换句话说,最近的描述是应该使用的正确描述。
I want to count how many times each code
appears, but I want it to be shown as the most recent description
.我想计算每个
code
出现的次数,但我希望它显示为最近的description
。
id ![]() |
code![]() |
description![]() |
other stuff![]() |
---|---|---|---|
1 ![]() |
09G ![]() |
jacket![]() |
blah blah![]() |
2 ![]() |
270 ![]() |
pants![]() |
blah blah![]() |
3 ![]() |
13B ![]() |
t-shirt ![]() |
blah blah![]() |
4 ![]() |
09G ![]() |
coat![]() |
blah blah![]() |
5 ![]() |
09G ![]() |
sweater![]() |
blah blah![]() |
6 ![]() |
13B ![]() |
shirt![]() |
blah blah![]() |
Here is my current code:这是我当前的代码:
SELECT
TOP 10 code, count(*)
FROM
table
GROUP BY
code
ORDER BY
count(*) DESC
Which gives me:这给了我:
code![]() |
value![]() |
---|---|
09G ![]() |
3 ![]() |
13B ![]() |
2 ![]() |
270 ![]() |
1 ![]() |
This is correct, but instead of the code, I would like it to show the most recently entered description - such as:这是正确的,但我希望它显示最近输入的描述而不是代码 - 例如:
code![]() |
value![]() |
---|---|
sweater![]() |
3 ![]() |
shirt![]() |
2 ![]() |
pants![]() |
1 ![]() |
The following code will get me close, but it returns the string that is "last" in alphabetical order, not necessarily the last entered (aka, the matching description with the largest id
):以下代码将使我接近,但它返回按字母顺序排列的“last”字符串,不一定是最后输入的字符串(也就是具有最大
id
的匹配描述):
SELECT
MAX(description) as description, count(*)
FROM
table
GROUP BY
code
ORDER BY
count(*) DESC
Assuming "most recent" means "highest id
":假设“最近的”意味着“最高的
id
”:
;WITH cte AS
(
SELECT description,
value = COUNT(*) OVER (PARTITION BY code),
rn = ROW_NUMBER() OVER (PARTITION BY code ORDER BY id DESC)
FROM dbo.[table]
)
SELECT description, value
FROM cte
WHERE rn = 1
ORDER BY value DESC;
Results:结果:
description![]() |
value![]() |
---|---|
sweater![]() |
3 ![]() |
shirt![]() |
2 ![]() |
pants![]() |
1 ![]() |
Here is an approach using 2 subqueries joined together, but I like the common table expression approach better.这是一种使用 2 个连接在一起的子查询的方法,但我更喜欢公共表表达式方法。
DECLARE @Data AS TABLE
(
[id] INT NOT NULL,
[code] CHAR(3) NOT NULL,
[description] VARCHAR(10) NOT NULL
);
INSERT INTO @Data
(
[id],
[code],
[description]
)
VALUES
(1, '09G', 'jacket'),
(2, '270', 'pants'),
(3, '13B', 't-shirt'),
(4, '09G', 'coat'),
(5, '09G', 'sweater'),
(6, '13B', 'shirt');
SELECT [descriptions].[description], [counts].[count]
FROM
(
SELECT [code], COUNT(*) AS [count]
FROM @Data AS [d]
GROUP BY [code]
) AS [counts]
INNER JOIN
(
SELECT [ordered].[code], [ordered].[description]
FROM
(
SELECT [code],
[description],
ROW_NUMBER() OVER (PARTITION BY [code]
ORDER BY [id] DESC
) AS [rn]
FROM @Data
) AS [ordered]
WHERE [ordered].[rn] = 1
) AS [descriptions]
ON [descriptions].[code] = [counts].[code];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.