简体   繁体   English

基于其他选定行中最大值的一列的 SQL 值

[英]SQL value of one column based on max values in other selected rows

I am using the Northwind sample database table and I would like to find the top categoryId for each supplierId...我正在使用 Northwind 示例数据库表,我想找到每个供应商 ID 的顶级 categoryId...

+-----------+----------------------------------+------------+------------+
| ProductID | ProductName                      | SupplierID | CategoryID |
+-----------+----------------------------------+------------+------------+
| 1         | Chai                             | 1          | 1          |
+-----------+----------------------------------+------------+------------+
| 2         | Chang                            | 1          | 1          |
+-----------+----------------------------------+------------+------------+
| 3         | Aniseed Syrup                    | 1          | 2          |
+-----------+----------------------------------+------------+------------+
| 4         | Chef Anton's Cajun Seasoning     | 2          | 2          |
+-----------+----------------------------------+------------+------------+
| 5         | Chef Anton's Gumbo Mix           | 2          | 2          |
+-----------+----------------------------------+------------+------------+
| 6         | Grandma's Boysenberry Spread     | 3          | 2          |
+-----------+----------------------------------+------------+------------+
| 7         | Uncle Bob's Organic Dried Pears  | 3          | 7          |
+-----------+----------------------------------+------------+------------+
| 8         | Northwoods Cranberry Sauce       | 3          | 2          |
+-----------+----------------------------------+------------+------------+
| 9         | Mishi Kobe Niku                  | 4          | 6          |
+-----------+----------------------------------+------------+------------+
| 10        | Ikura                            | 4          | 8          |
+-----------+----------------------------------+------------+------------+
| 11        | Queso Cabrales                   | 5          | 4          |
+-----------+----------------------------------+------------+------------+
| 12        | Queso Manchego La Pastora        | 5          | 4          |
+-----------+----------------------------------+------------+------------+
| 13        | Konbu                            | 6          | 8          |
+-----------+----------------------------------+------------+------------+
| 14        | Tofu                             | 6          | 7          |
+-----------+----------------------------------+------------+------------+
| 15        | Genen Shouyu                     | 6          | 2          |
+-----------+----------------------------------+------------+------------+
| 16        | Pavlova                          | 7          | 3          |
+-----------+----------------------------------+------------+------------+
| 17        | Alice Mutton                     | 7          | 6          |
+-----------+----------------------------------+------------+------------+
| 18        | Carnarvon Tigers                 | 7          | 8          |
+-----------+----------------------------------+------------+------------+
| 19        | Teatime Chocolate Biscuits       | 8          | 3          |
+-----------+----------------------------------+------------+------------+
| 20        | Sir Rodney's Marmalade           | 8          | 3          |
+-----------+----------------------------------+------------+------------+
| 21        | Sir Rodney's Scones              | 8          | 3          |
+-----------+----------------------------------+------------+------------+
| 22        | Gustaf's Knäckebröd              | 9          | 5          |
+-----------+----------------------------------+------------+------------+
| 23        | Tunnbröd                         | 9          | 5          |
+-----------+----------------------------------+------------+------------+
| 24        | Guaraná Fantástica               | 10         | 1          |
+-----------+----------------------------------+------------+------------+
| 25        | NuNuCa Nuß-Nougat-Creme          | 11         | 3          |
+-----------+----------------------------------+------------+------------+
| 26        | Gumbär Gummibärchen              | 11         | 3          |
+-----------+----------------------------------+------------+------------+
| 27        | Schoggi Schokolade               | 11         | 3          |
+-----------+----------------------------------+------------+------------+
| 28        | Rössle Sauerkraut                | 12         | 7          |
+-----------+----------------------------------+------------+------------+
| 29        | Thüringer Rostbratwurst          | 12         | 6          |
+-----------+----------------------------------+------------+------------+
| 30        | Nord-Ost Matjeshering            | 13         | 8          |
+-----------+----------------------------------+------------+------------+
| 31        | Gorgonzola Telino                | 14         | 4          |
+-----------+----------------------------------+------------+------------+
| 32        | Mascarpone Fabioli               | 14         | 4          |
+-----------+----------------------------------+------------+------------+
| 33        | Geitost                          | 15         | 4          |
+-----------+----------------------------------+------------+------------+
| 34        | Sasquatch Ale                    | 16         | 1          |
+-----------+----------------------------------+------------+------------+
| 35        | Steeleye Stout                   | 16         | 1          |
+-----------+----------------------------------+------------+------------+
| 36        | Inlagd Sill                      | 17         | 8          |
+-----------+----------------------------------+------------+------------+
| 37        | Gravad lax                       | 17         | 8          |
+-----------+----------------------------------+------------+------------+
| 38        | Côte de Blaye                    | 18         | 1          |
+-----------+----------------------------------+------------+------------+
| 39        | Chartreuse verte                 | 18         | 1          |
+-----------+----------------------------------+------------+------------+
| 40        | Boston Crab Meat                 | 19         | 8          |
+-----------+----------------------------------+------------+------------+
| 41        | Jack's New England Clam Chowder  | 19         | 8          |
+-----------+----------------------------------+------------+------------+
| 42        | Singaporean Hokkien Fried Mee    | 20         | 5          |
+-----------+----------------------------------+------------+------------+
| 43        | Ipoh Coffee                      | 20         | 1          |
+-----------+----------------------------------+------------+------------+
| 44        | Gula Malacca                     | 20         | 2          |
+-----------+----------------------------------+------------+------------+
| 45        | Rogede sild                      | 21         | 8          |
+-----------+----------------------------------+------------+------------+
| 46        | Spegesild                        | 21         | 8          |
+-----------+----------------------------------+------------+------------+
| 47        | Zaanse koeken                    | 22         | 3          |
+-----------+----------------------------------+------------+------------+
| 48        | Chocolade                        | 22         | 3          |
+-----------+----------------------------------+------------+------------+
| 49        | Maxilaku                         | 23         | 3          |
+-----------+----------------------------------+------------+------------+
| 50        | Valkoinen suklaa                 | 23         | 3          |
+-----------+----------------------------------+------------+------------+
| 51        | Manjimup Dried Apples            | 24         | 7          |
+-----------+----------------------------------+------------+------------+
| 52        | Filo Mix                         | 24         | 5          |
+-----------+----------------------------------+------------+------------+
| 53        | Perth Pasties                    | 24         | 6          |
+-----------+----------------------------------+------------+------------+
| 54        | Tourtière                        | 25         | 6          |
+-----------+----------------------------------+------------+------------+
| 55        | Pâté chinois                     | 25         | 6          |
+-----------+----------------------------------+------------+------------+
| 56        | Gnocchi di nonna Alice           | 26         | 5          |
+-----------+----------------------------------+------------+------------+
| 57        | Ravioli Angelo                   | 26         | 5          |
+-----------+----------------------------------+------------+------------+
| 58        | Escargots de Bourgogne           | 27         | 8          |
+-----------+----------------------------------+------------+------------+
| 59        | Raclette Courdavault             | 28         | 4          |
+-----------+----------------------------------+------------+------------+
| 60        | Camembert Pierrot                | 28         | 4          |
+-----------+----------------------------------+------------+------------+
| 61        | Sirop d'érable                   | 29         | 2          |
+-----------+----------------------------------+------------+------------+
| 62        | Tarte au sucre                   | 29         | 3          |
+-----------+----------------------------------+------------+------------+
| 63        | Vegie-spread                     | 7          | 2          |
+-----------+----------------------------------+------------+------------+
| 64        | Wimmers gute Semmelknödel        | 12         | 5          |
+-----------+----------------------------------+------------+------------+
| 65        | Louisiana Fiery Hot Pepper Sauce | 2          | 2          |
+-----------+----------------------------------+------------+------------+
| 66        | Louisiana Hot Spiced Okra        | 2          | 2          |
+-----------+----------------------------------+------------+------------+
| 67        | Laughing Lumberjack Lager        | 16         | 1          |
+-----------+----------------------------------+------------+------------+
| 68        | Scottish Longbreads              | 8          | 3          |
+-----------+----------------------------------+------------+------------+
| 69        | Gudbrandsdalsost                 | 15         | 4          |
+-----------+----------------------------------+------------+------------+
| 70        | Outback Lager                    | 7          | 1          |
+-----------+----------------------------------+------------+------------+
| 71        | Flotemysost                      | 15         | 4          |
+-----------+----------------------------------+------------+------------+
| 72        | Mozzarella di Giovanni           | 14         | 4          |
+-----------+----------------------------------+------------+------------+
| 73        | Röd Kaviar                       | 17         | 8          |
+-----------+----------------------------------+------------+------------+
| 74        | Longlife Tofu                    | 4          | 7          |
+-----------+----------------------------------+------------+------------+
| 75        | Rhönbräu Klosterbier             | 12         | 1          |
+-----------+----------------------------------+------------+------------+
| 76        | Lakkalikööri                     | 23         | 1          |
+-----------+----------------------------------+------------+------------+
| 77        | Original Frankfurter grüne Soße  | 12         | 2          |
+-----------+----------------------------------+------------+------------+

Using the query使用查询

SELECT SupplierID, CategoryID, COUNT(CategoryID) AS Total FROM [dbo].[Products] GROUP BY CategoryID, SupplierID

I get the table我拿到桌子

+------------+------------+-------+
| SupplierID | CategoryID | Total |
+------------+------------+-------+
| 1          | 1          | 2     |
+------------+------------+-------+
| 1          | 2          | 1     |
+------------+------------+-------+
| 2          | 2          | 4     |
+------------+------------+-------+
| 3          | 2          | 2     |
+------------+------------+-------+
| 3          | 7          | 1     |
+------------+------------+-------+
| 4          | 6          | 1     |
+------------+------------+-------+
| 4          | 7          | 1     |
+------------+------------+-------+
| 4          | 8          | 1     |
+------------+------------+-------+
| 5          | 4          | 2     |
+------------+------------+-------+
| 6          | 2          | 1     |
+------------+------------+-------+
| 6          | 7          | 1     |
+------------+------------+-------+
| 6          | 8          | 1     |
+------------+------------+-------+
| 7          | 1          | 1     |
+------------+------------+-------+
| 7          | 2          | 1     |
+------------+------------+-------+
| 7          | 3          | 1     |
+------------+------------+-------+
| 7          | 6          | 1     |
+------------+------------+-------+
| 7          | 8          | 1     |
+------------+------------+-------+
| 8          | 3          | 4     |
+------------+------------+-------+
| 9          | 5          | 2     |
+------------+------------+-------+
| 10         | 1          | 1     |
+------------+------------+-------+
| 11         | 3          | 3     |
+------------+------------+-------+
| 12         | 1          | 1     |
+------------+------------+-------+
| 12         | 2          | 1     |
+------------+------------+-------+
| 12         | 5          | 1     |
+------------+------------+-------+
| 12         | 6          | 1     |
+------------+------------+-------+
| 12         | 7          | 1     |
+------------+------------+-------+
| 13         | 8          | 1     |
+------------+------------+-------+
| 14         | 4          | 3     |
+------------+------------+-------+
| 15         | 4          | 3     |
+------------+------------+-------+
| 16         | 1          | 3     |
+------------+------------+-------+
| 17         | 8          | 3     |
+------------+------------+-------+
| 18         | 1          | 2     |
+------------+------------+-------+
| 19         | 8          | 2     |
+------------+------------+-------+
| 20         | 1          | 1     |
+------------+------------+-------+
| 20         | 2          | 1     |
+------------+------------+-------+
| 20         | 5          | 1     |
+------------+------------+-------+
| 21         | 8          | 2     |
+------------+------------+-------+
| 22         | 3          | 2     |
+------------+------------+-------+
| 23         | 1          | 1     |
+------------+------------+-------+
| 23         | 3          | 2     |
+------------+------------+-------+
| 24         | 5          | 1     |
+------------+------------+-------+
| 24         | 6          | 1     |
+------------+------------+-------+
| 24         | 7          | 1     |
+------------+------------+-------+
| 25         | 6          | 2     |
+------------+------------+-------+
| 26         | 5          | 2     |
+------------+------------+-------+
| 27         | 8          | 1     |
+------------+------------+-------+
| 28         | 4          | 2     |
+------------+------------+-------+
| 29         | 2          | 1     |
+------------+------------+-------+
| 29         | 3          | 1     |
+------------+------------+-------+

As you can see supplier 1 makes 2 category 1 products and 1 catergory 2 product.如您所见,供应商 1 生产 2 个类别 1 产品和 1 个类别 2 产品。 Therefore the first line in the query should read因此,查询中的第一行应为

+------------+------------+-------+
| SupplierID | CategoryID | Total |
+------------+------------+-------+
| 1          | 1          | 2     |
+------------+------------+-------+

Next should be supplierId #2 which makes a total of 4 category 2 products.接下来应该是供应商 ID #2,它总共有 4 个类别 2 产品。 The final table should look like this...决赛桌应该是这样的……

+------------+------------+-------+
| SupplierID | CategoryID | Total |
+------------+------------+-------+
| 1          | 1          | 2     |
+------------+------------+-------+
| 2          | 2          | 4     |
+------------+------------+-------+
| 3          | 2          | 2     |
+------------+------------+-------+
| 4          | 6          | 1     |
+------------+------------+-------+
| 5          | 4          | 2     |
+------------+------------+-------+
| 6          | 2          | 1     |
+------------+------------+-------+
| 7          | 1          | 1     |
+------------+------------+-------+
| 8          | 3          | 4     |
+------------+------------+-------+
| 9          | 5          | 2     |
+------------+------------+-------+
| 11         | 3          | 3     |
+------------+------------+-------+
| 12         | 1          | 1     |
+------------+------------+-------+
| 13         | 8          | 1     |
+------------+------------+-------+
| 14         | 4          | 3     |
+------------+------------+-------+
| 15         | 4          | 3     |
+------------+------------+-------+
| 16         | 1          | 3     |
+------------+------------+-------+
| 17         | 8          | 3     |
+------------+------------+-------+
| 18         | 1          | 2     |
+------------+------------+-------+
| 19         | 8          | 2     |
+------------+------------+-------+
| 20         | 1          | 1     |
+------------+------------+-------+
| 21         | 8          | 2     |
+------------+------------+-------+
| 22         | 3          | 2     |
+------------+------------+-------+
| 23         | 3          | 2     |
+------------+------------+-------+
| 24         | 5          | 1     |
+------------+------------+-------+
| 25         | 6          | 2     |
+------------+------------+-------+
| 26         | 5          | 2     |
+------------+------------+-------+
| 27         | 8          | 1     |
+------------+------------+-------+
| 28         | 4          | 2     |
+------------+------------+-------+
| 29         | 2          | 1     |
+------------+------------+-------+
| 29         | 3          | 1     |
+------------+------------+-------+ 

I know a lot of suppliers only make one item for a given category and this isn't a great example but just trying to learn here.我知道很多供应商只为给定的类别生产一件商品,这不是一个很好的例子,只是想在这里学习。

Thanks谢谢

I think you can make use of row number based on partition by supplier and then use aggregate function along with row number for ranking.我认为您可以使用基于供应商分区的行号,然后使用聚合函数和行号进行排名。 Then only select the one where you have more rows for a given supplier.然后只选择给定供应商有更多行的那个。 I just took some part of your sample data and did it in this way.我只是采用了您的示例数据的一部分并以这种方式进行了操作。

with cte as (

 select  1 as ProductID, 'Chai'                            as ProductNmae,  1 as SupplierID,  1   as CategoryID   union all                               
 select  2 as ProductID, 'Chang'                           as ProductNmae, 1  as SupplierID, 1    as CategoryID   union all                               
 select  3 as ProductID, 'Aniseed Syrup'                   as ProductNmae, 1  as SupplierID, 2    as CategoryID   union all                               
 select  4 as ProductID, 'Chef Anton''s Cajun Seasoning'   as ProductNmae,  2 as SupplierID,  2   as CategoryID   union all                               
 select  5 as ProductID, 'Chef Anton''s Gumbo Mix'          as ProductNmae, 2  as SupplierID, 2    as CategoryID union all  
 select  6  as ProductID,  'Grandma''s Boysenberry Spread'     as Product_name , 3  as SupplierID,  2  as CategoryID union all         
 select   7 as ProductID,   'Uncle Bob''s Organic Dried Pears' as Product_name ,  3 as SupplierID,  7  as CategoryID union all         
 select   8 as ProductID,   'Northwoods Cranberry Sauce'       as Product_name ,  3 as SupplierID,  2  as CategoryID     )
 select t.SupplierID, t.CategoryID, t.total from ( 
 select supplierID, CategoryID , ROW_NUMBER() over (partition by supplierID order by count(1) desc) rownum, count(1) total    from cte     
 group by supplierID, CategoryID  ) t
 where t.rownum = 1 

Output:输出:

SupplierID  CategoryID  total
1               1        2
2               2        2
3               2        2

First you have to generate the category counts by supplier, then you have to rank them from highest to lowest, and finally select only the highest.首先,您必须按供应商生成类别计数,然后您必须将它们从高到低排列,最后只选择最高的。 In the following query, I've done that by using nested queries:在下面的查询中,我通过使用嵌套查询来做到这一点:

-- Select only the top category counts by supplier
SELECT
    [SupplierID],
    [CategoryID],
    [Total]
FROM (
    -- Rank category counts by supplier
    SELECT
        *,
        RANK() OVER (PARTITION BY [SupplierID] ORDER BY [Total] DESC) AS [Rank]
    FROM (
        -- Generate category counts by supplier
        SELECT
            [SupplierID],
            [CategoryID],
            COUNT(*) AS [Total]
        FROM [Products]
        GROUP BY
            [SupplierID],
            [CategoryID]
    ) AS SupplierCategoryCounts
) AS RankedSupplierCategoryCounts
WHERE [Rank] = 1
ORDER BY [SupplierID]

In Sql server you can write a query as:在 Sql 服务器中,您可以将查询编写为:

select SupplierID , 
        CategoryID ,
        Total 
from (
select 
        SupplierID , 
        CategoryID ,
        Total ,
        ROW_NUMBER() over (partition by SupplierID order by  Total desc) as rownum
from (
        SELECT SupplierID
            , CategoryID
            , COUNT(CategoryID) AS Total 
        FROM [dbo].[Products]
        GROUP BY CategoryID, SupplierID
        ) as Innertable

) as Outertable
where rownum = 1
order by SupplierID

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

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