简体   繁体   English

MSSQL 聚合(计数)一列,但根据最后匹配行的不同列显示字符串

[英]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 ID code代码 description描述 other stuff其他的东西
1 1 09G 09G jacket夹克 blah blah呸呸
2 2 270 270 pants裤子 blah blah呸呸
3 3 13B 13B t-shirt T恤 blah blah呸呸
4 4 09G 09G coat外套 blah blah呸呸
5 5 09G 09G sweater毛衣 blah blah呸呸
6 6 13B 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 09G 3 3
13B 13B 2 2
270 270 1 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 3
shirt衬衫 2 2
pants裤子 1 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 3
shirt衬衫 2 2
pants裤子 1 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.

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