【问题标题】:SQL Query - Select highest column value for tableSQL 查询 - 选择表的最高列值
【发布时间】:2014-02-09 04:02:30
【问题描述】:

我有下表:

ProductID, GroupID, Description
1          100      Blah blah
2          200      Blah blah
3          100      Blah blah
4          200      Blah blah
5          200      Blah blah
6          100      Blah blah
7          300      Blah blah
9          300      Blah blah
10         100      Blah blah

我需要运行一个查询来获取该表的数据,以便每个 GroupID 只检索一次,并选择最顶层的 ProductID。示例如下所示

ProductID, GroupID, Description
10         100      Blah blah
5          200      Blah blah
9          300      Blah blah

对此有最佳方法的任何想法?目标是每次运行此查询时,它总是获取每个特定 GroupID 的最新 ProductID。此表中有更多字段,但我将其简化为这个示例,它基本上说明了我要解决的主要问题

谢谢!

【问题讨论】:

  • 请看下面的答案,首先我被否决了,因为我无法理解你的问题。现在回答已编辑并有意义

标签: sql sql-server sql-server-2008 select join


【解决方案1】:

您将要使用 OVER 子句按 GroupId 对表进行分区。这将为您提供一个包含两列的表,productId 和 rowNum。对于每个 GroupID 中的最高 ProductID,rowNum 将为 1。接下来,您只需对该表进行内部连接并获取 rowNum 为 1 的 ProductID。有关 OVER 子句的更多信息可以是found here

SELECT yt1.*  
FROM yourTable yt1 
INNER JOIN  ( 
    SELECT ROW_NUMBER() OVER(PARTITION BY GroupID ORDER BY ProductId DESC) as rowNum
    , ProductID FROM yourTable 
    )yt2 
ON yt1.ProductID = yt2.ProductID 
WHERE yt2.rowNum = 1

【讨论】:

    【解决方案2】:

    你可以试试这个(未测试):

    SELECT t.ProductID, t.GroupID, t.Description
    FROM MyTableName t
        INNER JOIN
            (SELECT MAX(ProductID) As ProductID, GroupID
            FROM MyTableName
            GROUP BY GroupID) as maxPerGroup
        ON maxPerGroup.ProductID = t.ProductID
    

    SQL fiddle demo

    【讨论】:

    • 解释否决票。根据我对问题的理解,该查询运行良好。
    • 是的,我已经测试过了。它工作正常。如果有人反对它。那就不好了。要对任何事情投反对票,您必须有正当理由。
    【解决方案3】:

    对于像这样的任务,我总是喜欢使用Ranking

    SELECT ProductID, GroupID, Description FROM
    (
        SELECT
            t.ProductID
            ,t.GroupID
            ,t.Description    
            ,RANK() OVER (PARTITION BY t.GroupID ORDER BY t.ProductID DESC) [Rank]
        FROM MyTableName t
    
    ) RawData
    WHERE RANK = 1
    

    一般来说,内部查询只是在GroupID的上下文中为每一行提供排名
    (这是由RANK() OVER (PARTITION BY t.GroupID ORDER BY t.ProductID DESC 完成的)。

    包装查询仅用于过滤排名为 1 的行,即在特定 GroupID 的上下文中 productID 最高的行。

    你可以在这个Fiddle demo查看结果

    【讨论】:

      【解决方案4】:

      试试这个查询

      从产品中选择 *;

      +-----------+---------+-------------+
      | productId | groupId | description |
      +-----------+---------+-------------+
      |         1 |     100 | hello       |
      |         2 |     200 | hello       |
      |         3 |     100 | hello       |
      |         4 |     200 | hello       |
      |         5 |     200 | hello       |
      |         6 |     100 | hello       |
      |         7 |     300 | hello       |
      |         8 |     300 | hello       |
      |         9 |     100 | hello       |
      |        10 |     200 | hello       |
      +-----------+---------+-------------+
      

      select * from products where productId in (select MAX(productId) from products group by groupId) order by groupId ASC;

      +-----------+---------+-------------+
      | productId | groupId | description |
      +-----------+---------+-------------+
      |         9 |     100 | hello       |
      |        10 |     200 | hello       |
      |         8 |     300 | hello       |
      +-----------+---------+-------------+
      

      【讨论】:

        【解决方案5】:

        在 PostgreSQL 中,这是可行的。请检查 SQL Server -

         select t.productid, t.groupid, t.description from t, (select max(productid) mp , groupid from t  group by groupid) x where t.productid=x.mp;
        ;
         productid | groupid | description 
        -----------+---------+-------------
                 5 |     200 | BLAH BLAH
                 9 |     300 | BLAH BLAH
                10 |     100 | BLAH BLAH
        (3 rows)
        

        【讨论】:

          【解决方案6】:

          抱歉,首先我无法理解您的问题,所以我被否决了。以下是经过编辑且经过测试的正确查询。

          您的记录:

          select * from dbo.[Products]
          

          显示具有顶级 ProductID 的不同 GroupID

          ;with cteProducts(ProductID , GroupID) AS
          (
           select max(ProductID) ProductID , GroupID
           FROM dbo.Products od
           Group by GroupID 
          )
          SELECT p1.ProductID,p1.GroupID,p1.[Description] from Products p1
          INNER JOIN cteProducts p2 on p1.ProductID=p2.ProductID
          order by p1.ProductID Desc
          

          您需要的结果在这里执行:

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-10-13
            • 1970-01-01
            • 2020-05-03
            • 1970-01-01
            • 2015-12-23
            • 1970-01-01
            • 2013-10-26
            • 2023-01-26
            相关资源
            最近更新 更多