select t.*, ( case when t.networkType = 1 then '网点' when t.networkType = 2 then '集散' when t.networkType = 3 then '中心' end ) as networkTypeName from ( select h.networkName, h.networkType, h.suspectCnt, h.handlednt, h.noHandlednt, h.rate, row_number() over ( PARTITION by h.networkType order by h.noHandlednt desc ) as num from ( select wm.name as networkName, sn.duty_network_type as networkType, sum(sn.suspectedCount) as suspectCnt, sum(sn.endCount) as handlednt, sum(sn.suspectedCount - sn.endCount) as noHandlednt, round( if( sum(sn.suspectedCount) = 0, 0, sum(sn.endCount) / sum(sn.suspectedCount) ) * 100, 1 ) as rate from jms_dws.dws_suspected_loss_sum_new sn join ( select code, virt_code, virt_name, agent_code, agent_name, city_id, city_desc, name, business_principal, zone_code, zone_name, zone_id, zone_staff_name from jms_dwm.dwm_network_whole_massage where agent_code = '610000' ) wm on sn.duty_network_code = wm.code where sn.create_date between '2023-06-24' and '2023-06-27' AND sn.duty_network_type in (1, 2, 3) GROUP BY wm.name, sn.duty_network_type ) h ) t where t.num <= 100 order by noHandlednt DESC LIMIT 100
时间: 2024-04-18 11:30:38 浏览: 103
这段SQL查询语句的作用是从数据库中查询出符合条件的网络数据,并按照未处理数量从高到低排序,最多返回100条数据。
具体的查询逻辑如下:
1. 内部查询(子查询)首先从表dwm_network_whole_massage中选择出符合条件的网络数据,包括网络名字、网络类型等字段,并将其命名为wm。
2. 外部查询(主查询)中,将内部查询的结果作为表h,并对其进行聚合操作,计算出每个网络类型下的总疑似数量(suspectCnt)、已处理数量(handlednt)、未处理数量(noHandlednt)以及处理率(rate)等字段。
3. 利用窗口函数row_number()对每个网络类型进行分区,并按照未处理数量的降序进行排序,得到每个网络类型下的序号(num)。
4. 最外层查询通过选择序号小于等于100的数据,并按照未处理数量的降序进行排序,返回最多100条数据。
5. 在结果中,使用CASE语句将网络类型转换为相应的名称,并将其命名为networkTypeName。
请注意,以上解释仅针对给定的查询语句,具体的数据表结构和业务逻辑可能会有所不同。
相关问题
case when a.id='1' from
It looks like your SQL statement is incomplete. The `CASE` statement is used to conditionally return a value based on one or more conditions. However, in your example, you only have the beginning of the statement (`CASE WHEN a.id='1' FROM`).
Assuming you want to return a specific value when `a.id` is equal to `'1'`, the correct syntax would be:
```
SELECT CASE WHEN a.id = '1' THEN 'Value when ID is 1' ELSE 'Value when ID is not 1' END as my_column
FROM my_table a
```
In this example, the `CASE` statement checks if `a.id` is equal to `'1'`. If it is, the value `'Value when ID is 1'` is returned. If it is not, the value `'Value when ID is not 1'` is returned. The result is returned as a column named `my_column` from the table `my_table` (which you will need to replace with your actual table name).
请解释下sql中case when ...then 的用法,并举例
SQL 中的 `CASE WHEN... THEN` 是一个条件表达式,也称为 CASE 表达式或 CASE 结构,它用于根据给定的条件返回不同的值。这个结构的基本语法如下:
```sql
CASE
WHEN condition_1 THEN result_1
[WHEN condition_2 THEN result_2]
...
ELSE default_result
END
```
在这里,`condition_1`, `condition_2` 等是布尔表达式,如果它们成立,则返回相应的 `result_1`, `result_2`。如果有多个 `WHEN` 子句,会检查每个条件是否满足,第一个满足的就会执行对应的 `THEN` 部分。
例如,假设你想计算某个表中的员工薪资等级,基于他们的月工资:
```sql
SELECT
employee_id,
salary,
CASE
WHEN salary > 5000 THEN '高级'
WHEN salary > 3000 THEN '中级'
ELSE '初级'
END AS salary_level
FROM employees;
```
在这个例子中,如果员工的月工资超过 5000,他们会被标记为“高级”,在其他情况下,根据不同的工资范围,他们会分别被标记为“中级”或“初级”。
阅读全文
相关推荐
















