简体   繁体   English

SQL查询-选择表的最高列值

[英]SQL Query - Select highest column value for table

I have the following table: 我有下表:

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

I need to run a query that gets the data for this table such that EACH GroupID is retrieved exactly once, and the top-most ProductID is chosen. 我需要运行一个查询来获取此表的数据,以便每个EACH GroupID都被精确检索一次,并选择最上面的ProductID。 Example is shown below 示例如下所示

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

Any thoughts on the best approach for this? 对最佳方法有什么想法? The goal being that each time this query is run, it always gets the latest ProductID for each particular GroupID. 目标是每次运行此查询时,它始终会为每个特定的GroupID获取最新的ProductID。 There are a lot more fields in this table, but I'm simplifying it to this example which essentially illustrates the main problem I'm trying to solve 该表中有更多字段,但是我将其简化为此示例,该示例实质上说明了我要解决的主要问题

Thanks! 谢谢!

You can try this ( not tested ) : 您可以尝试以下方法( 未经测试 ):

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 SQL小提琴演示

You're going to want to use the OVER clause to partition the table by GroupId. 您将要使用OVER子句按GroupId对表进行分区。 This will get you a table with two columns, the productId and a rowNum. 这将为您提供一个包含两列的表,即productId和rowNum。 rowNum will be 1 for the highest ProductID in each GroupID. 对于每个GroupID中最高的ProductID,rowNum将为1。 Next, you simply inner join to that table and get ProductIDs that have a rowNum of 1. More info on the OVER clause can be found here 接下来,您只需对表进行内部联接,并获得rowNum为1的ProductID。有关OVER子句的更多信息,请参见此处。

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

For tasks such as this one, I always love using Ranking : 对于这样的任务,我总是喜欢使用Rank

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

In general, the inner query just gives ranks for each row in the context of it's GroupID 通常,内部查询只是在其GroupID的上下文中给出每一行的排名
(this is done by RANK() OVER (PARTITION BY t.GroupID ORDER BY t.ProductID DESC ). (这是通过RANK() OVER (PARTITION BY t.GroupID ORDER BY t.ProductID DESC通过RANK() OVER (PARTITION BY t.GroupID ORDER BY t.ProductID DESC )完成的。

The wrapping query is just for filtering rows which have a rank of 1 ie where the productID is highest in the context of the specific GroupID . 包装查询仅用于过滤等级为1的行,即在特定GroupID的上下文中productID最高的行。

You can view the results in this Fiddle demo 您可以在此Fiddle演示中查看结果

Try this query 试试这个查询

select * from products; 从产品中选择*;

+-----------+---------+-------------+
| 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从产品group中选择MAX(productId))按groupId ASC排序;

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

Sorry first i cann't understand your question,so i am down Voted. 抱歉,我无法理解您的问题,所以我被拒绝了。 Following is the edited correct and tested query. 以下是经过编辑的正确和经过测试的查询。

your records: 您的记录:

select * from dbo.[Products]

在此处输入图片说明

Showing Distinct GroupID with top ProductID 显示具有最高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

Your required result executed here: 您所需的结果在此处执行:

在此处输入图片说明

In PostgreSQL, this works. 在PostgreSQL中,这可行。 Please check in SQL Server - 请检入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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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