是否可以输出 table 按组排序且每个组有限?

Is it possible to output table ordered by group and limited per group?

我有一个包含 table 辆汽车的数据库,table 有许多不同的列。我需要输出每辆汽车品牌订购的 table 中的内容,每个品牌只需要输出三辆汽车以及每排汽车的总数。我还需要按降序排列输出,并附有一个名为 Ranking 的列,该列从 1 递增到将有多少个输出。

下面是我的数据库中的示例 table

|Timestamp     |Email                    |Name      |Year|Make    |Model    |Car_ID|Judge_ID|Judge_Name|Racer_Turbo|Racer_Supercharged|Racer_Performance|Racer_Horsepower|Car_Overall|Engine_Modifications|Engine_Performance|Engine_Chrome|Engine_Detailing|Engine_Cleanliness|Body_Frame_Undercarriage|Body_Frame_Suspension|Body_Frame_Chrome|Body_Frame_Detailing|Body_Frame_Cleanliness|Mods_Paint|Mods_Body|Mods_Wrap|Mods_Rims|Mods_Interior|Mods_Other|Mods_ICE|Mods_Aftermarket|Mods_WIP|Mods_Overall|
|--------------|-------------------------|----------|----|--------|---------|------|--------|----------|-----------|------------------|-----------------|----------------|-----------|--------------------|------------------|-------------|----------------|------------------|------------------------|---------------------|-----------------|--------------------|----------------------|----------|---------|---------|---------|-------------|----------|--------|----------------|--------|------------|
|8/5/2018 14:10|honoland13@japanpost.jp  |Hernando  |2015|Acura   |TLX      |48    |J04     |Bob       |0          |0                 |2                |2               |4          |4                   |0                 |2            |4               |4                 |2                       |4                    |2                |2                   |2                     |2         |2        |0        |4        |4            |4         |6       |2               |0       |4           |
|8/5/2018 15:11|nlighterness2q@umn.edu   |Noel      |2015|Jeep    |Wrangler |124   |J02     |Carl      |0          |6                 |4                |2               |4          |6                   |6                 |4            |4               |4                 |6                       |6                    |6                |6                   |6                     |4         |6        |6        |6        |6            |6         |4       |6               |4       |6           |
|8/5/2018 17:10|eguest47@microsoft.com   |Edan      |2015|Lexus   |Is250    |222   |J05     |Adrian    |0          |0                 |0                |0               |0          |0                   |0                 |0            |6               |6                 |6                       |0                    |0                |6                   |6                     |6         |0        |0        |0        |0            |0         |0       |0               |0       |4           |
|8/5/2018 17:34|hchilley40@fema.gov      |Hieronymus|1993|Honda   |Civic eG |207   |J06     |Aaron     |0          |0                 |2                |2               |2          |2                   |2                 |2            |0               |4                 |2                       |2                    |2                |2                   |2                     |2         |4        |2        |2        |0            |0         |0       |2               |2       |0           |
|8/5/2018 14:30|nnowick3d@tuttocitta.it  |Nickolas  |2016|Ford    |Mystang  |167   |J02     |Carl      |0          |0                 |2                |2               |0          |2                   |2                 |0            |0               |0                 |0                       |2                    |0                |2                   |2                     |2         |0        |0        |2        |0            |0         |0       |0               |0       |2           |
|8/5/2018 16:12|mdearl39@amazon.co.uk    |Martin    |2013|Hyundai |Gen coupe|159   |J04     |Bob       |0          |0                 |2                |0               |0          |0                   |2                 |0            |0               |0                 |0                       |2                    |0                |2                   |2                     |0         |2        |0        |2        |0            |0         |0       |0               |0       |0           |
|8/5/2018 17:00|alynamg@blogtalkradio.com|Aldridge  |2009|Infiniti|G37      |20    |J06     |Aaron     |2          |0                 |2                |2               |0          |0                   |2                 |0            |0               |2                 |2                       |2                    |2                |2                   |2                     |2         |2        |2        |4        |2            |2         |0       |2               |0       |2           |
|8/5/2018 16:11|abowton3k@spiegel.de     |Ambros    |2009|Honda   |Oddesy   |178   |J06     |Aaron     |2          |0                 |2                |2               |2          |2                   |2                 |0            |4               |4                 |2                       |2                    |2                |4                   |4                     |4         |2        |2        |         |6            |4         |4       |6               |4       |6           |
|8/5/2018 17:29|qesterbrookn@bandcamp.com|Quincy    |2012|Hyundai |Celoster |30    |J04     |Bob       |0          |0                 |2                |2               |2          |2                   |2                 |4            |6               |6                 |4                       |2                    |4                |4                   |6                     |6         |4        |0        |2        |0            |0         |0       |2               |2       |4           |

预期的输出如下所示

|Ranking |Car_ID|Year   |Make  |Model      |Total|
|--------|------|-------|------|-----------|-----|
|1       |48    |2015   |Acura |TLX        |89   |
|2       |66    |2012   |Acura |MDX        |75   |
|3       |101   |2022   |Acura |TLX        |70   |
|4       |22    |2011   |Chevy |Camaro     |112  |
|5       |40    |2015   |Chevy |Corvette   |99   |
|6       |205   |2022   |Chevy |Corvette   |66   |
|7       |111   |2006   |Ford  |Mustang    |94   |
|8       |97    |2003   |Ford  |GT         |88   |
|9       |71    |2008   |Ford  |Fiesta ST  |80   |

这是我能够组合起来的命令,它执行的操作与我需要的类似,但我无法弄清楚如何按总数降序排列排名列和顺序。

SELECT Car_ID, Year, Make, Model, Racer_Turbo + Racer_Supercharged + ... + Mods_Overall FROM Carstable order by Make limit 3;

这个查询命令只返回了三个结果,而不是全部,我也想不通应该把DESC关键字放在命令的什么地方,才能让它们根据总列降序排列或者怎么办排名栏也是如此。有什么想法吗?

使用 CTE,其中 returns 每一行的列 TotalROW_NUMBER() window 函数为每个 Make 选择前 3 行和创建列 Ranking:

WITH cte AS (
  SELECT *, 
       Racer_Turbo + Racer_Supercharged + Racer_Performance + Racer_Horsepower + 
       Car_Overall + 
       Engine_Modifications + Engine_Performance + Engine_Chrome + Engine_Detailing + Engine_Cleanliness + 
       Body_Frame_Undercarriage + Body_Frame_Suspension + Body_Frame_Chrome + Body_Frame_Detailing + Body_Frame_Cleanliness + 
       Mods_Paint + Mods_Body + Mods_Wrap + Mods_Rims + Mods_Interior + Mods_Other + Mods_ICE + Mods_Aftermarket + Mods_WIP + Mods_Overall Total
  FROM carstable
)
SELECT ROW_NUMBER() OVER (ORDER BY Make, Total DESC) Ranking,
       Car_ID, Year, Make, Model, Total
FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY Make ORDER BY Total) rn FROM cte) 
WHERE rn <= 3
ORDER BY Make, Total DESC;

参见demo