sql - 检索这些记录的查询是什么?

标签 sql sql-server database

输入表:销售数据

+---------------+---------------+---------- +-----------+
| Child         | Parent        | Level     | Sales     |
+---------------+---------------+---------- +-----------+
| All Region    | All Region    | 1         |  1000000  |
| Africa Region | All Region    | 2         |   159816  |
| America       | All Region    | 2         |   114054  |
| Asia          | All Region    | 2         |    74028  |
| Europe Region | All Region    | 2         |   116766  |
| Africa        | Africa Region | 3         |    81954  |
| Asia Pacific  | Asia          | 3         |   144306  |
| Europe        | Europe Region | 3         |     1440  |
| North America | America       | 3         |     8185  |
| South America | America       | 3         |     8440  | 
| Argentina     | South America | 4         |      470  |
| Australia     | Asia Pacific  | 4         |     9040  |
| Pakistan      | Asia Pacific  | 4         |      705  | 
| South Africa  | Africa        | 4         |       45  |
| Tunisia       | Africa        | 4         |      385  | 
| Uruguay       | South America | 4         |      420  |
+-------------------------------------------------------+ 

等级

  • 1 = 所有地区
  • 2 = 地区
  • 3 = 次区域
  • 4 = 国家

我需要输出的记录有两列,一列是CHILD,另一列是MAX_Sale,在MAX_Sale列中只有 sibling 中的最大销售额相应的子数据。

例子:

+-----------+------------+
| Childs    | Max_sales  |
+-----------+------------+
| Austrlia  |   9040     |
| Pakistan  |   9040     |
| S.Africa  |    385     |
| Tunisia   |    385     |
+------------------------+

由于澳大利亚和巴基斯坦同属 parent ,澳大利亚的销售额最大

最佳答案

您可以使用窗口函数 (row_number) 来获得此结果:

select child, MAX_Sale
from  (
        select child, 
               sales as MAX_Sale,
               row_number() over (partition by parent order by sales desc) as rn
        from   t
      ) sub
where rn = 1

...假设您的表名为t。根据需要更换。

输出是:

 Child         | MAX_Sale
---------------+---------
 Tunisia       |     385
 Africa        |   81954
 All Region    | 1000000
 South America |    8440
 Asia Pacific  |  144306
 Australia     |    9040
 Europe        |    1440
 Argentina     |     470

根据您的需要,您可能仍需要按级别过滤,并应用特定的排序顺序。

列出所有地区

但是,如果您需要列出所有区域,以及销量最好的 sibling 的销量,则:

select child, 
       first_value(sales) over (partition by parent order by sales desc) as MAX_Sale
from   t

输出:

Child         | MAX_Sale
--------------+----------
Tunisia       |     385
South Africa  |     385
Africa        |   81954
All Region    | 1000000
Africa Region | 1000000
Europe Region | 1000000
America       | 1000000
Asia          | 1000000
South America |    8440
North America |    8440
Asia Pacific  |  144306
Australia     |    9040
Pakistan      |    9040
Europe        |    1440
Argentina     |     470
Uruguay       |     470

first_value是一个 "analytic function" , 在“窗口函数”的上下文中使用:over clause定义应用窗口函数的窗口。

关于sql - 检索这些记录的查询是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38716435/

相关文章:

c# - 如何选择轻版本数据库系统

c# - 如何找到特定行的行号?

mysql - 如何在 customer 下创建一个引用 employer 表中的 "id"的列

mysql - MySQL存储高精度小数的数据类型

sql-server - 问题 : Writing more data than expected in debian packages

sql-server - 将 SQL Server CLR UDT 编写为 `struct` 或 `class` 的相对优点是什么?

sql-server - 存储过程中的动态sql,以列名作为输入参数

java - 在Spring Data JPA中获取列的所有记录,即使我们将其指定为参数

php - 使用 php PDO 和 MySQL 返回行值

android - 通过从数据库填充的 ListView 进行搜索