簡體   English   中英

基於其他選定行中最大值的一列的 SQL 值

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

我正在使用 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          |
+-----------+----------------------------------+------------+------------+

使用查詢

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

我拿到桌子

+------------+------------+-------+
| 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     |
+------------+------------+-------+

如您所見,供應商 1 生產 2 個類別 1 產品和 1 個類別 2 產品。 因此,查詢中的第一行應為

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

接下來應該是供應商 ID #2,它總共有 4 個類別 2 產品。 決賽桌應該是這樣的……

+------------+------------+-------+
| 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     |
+------------+------------+-------+ 

我知道很多供應商只為給定的類別生產一件商品,這不是一個很好的例子,只是想在這里學習。

謝謝

我認為您可以使用基於供應商分區的行號,然后使用聚合函數和行號進行排名。 然后只選擇給定供應商有更多行的那個。 我只是采用了您的示例數據的一部分並以這種方式進行了操作。

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 

輸出:

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

首先,您必須按供應商生成類別計數,然后您必須將它們從高到低排列,最后只選擇最高的。 在下面的查詢中,我通過使用嵌套查詢來做到這一點:

-- 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]

在 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