SELECT CASE WHEN grouping(fskgk) = 1 THEN '合计' ELSE fskgk END AS fskgk, SUM(fssjejn) AS fssjejn, SUM(fssjeqn) AS fssjeqn , SUM(fssjedqn) AS fssjedqn, SUM(fhsjn) AS fhsjn , SUM(fhsqn) AS fhsqn, SUM(fhsdqn) AS fhsdqn , round(decode(SUM(fssjeqn), 0, 0, (SUM(fssjejn) - SUM(fssjeqn)) / SUM(fssjeqn)) * 100, 2) AS fcsbjn , round(decode(SUM(fssjedqn), 0, 0, (SUM(fssjeqn) - SUM(fssjedqn)) / SUM(fssjedqn)) * 100, 2) AS fcsbqn , round(decode(SUM(fhsqn), 0, 0, (SUM(fhsjn) - SUM(fhsqn)) / SUM(fhsqn)) * 100, 2) AS hjbjn , round(decode(SUM(fhsdqn), 0, 0, (SUM(fhsqn) - SUM(fhsdqn)) / SUM(fhsdqn)) * 100, 2) AS hjbqn FROM ( SELECT fskgk , round(SUM(fssjejn) / 10000, 2) AS fssjejn , round(SUM(fssjeqn) / 10000, 2) AS fssjeqn , round(SUM(fssjedqn) / 10000, 2) AS fssjedqn , nvl(SUM(fhsjn), 0) AS fhsjn , nvl(SUM(fhsqn), 0) AS fhsqn , nvl(SUM(fhsdqn), 0) AS fhsdqn FROM ( SELECT substr(frkrq, 1, 4) AS fyears, fskgk , SUM(车船税) AS fssjejn, 0 AS fssjeqn, 0 AS fssjedqn FROM zhzs_srfx_ss_all WHERE substr(frkrq, 1, 4)='2023' GROUP BY fskgk, substr(frkrq, 1, 4) UNION ALL SELECT substr(frkrq, 1, 4)AS fyears, fskgk , 0 AS fssjejn, SUM(车船税) AS fssjeqn, 0 AS fssjedqn FROM zhzs_srfx_ss_all WHERE substr(frkrq, 1, 4)='2022' GROUP BY fskgk, substr(frkrq, 1, 4) UNION ALL SELECT substr(frkrq, 1, 4)AS fyears, fskgk , 0 AS fssjejn, 0 AS fssjeqn, SUM(车船税) AS fssjedqn FROM zhzs_srfx_ss_all WHERE substr(frkrq, 1, 4)='2021' GROUP BY fskgk, substr(frkrq, 1, 4) ) a left JOIN ( SELECT substr(fcldjsj, 1, 4) AS years, fssqx , fclspswl AS fhsjn, '0' AS fhsqn, '0' AS fhsdqn FROM zhzs_bill_clspdjxx WHERE substr(fcldjsj, 1, 4) = '2023' GROUP BY substr(fcldjsj, 1, 4), fssqx, fclspswl UNION ALL SELECT substr(fcldjsj, 1, 4) AS years, fssqx , '0' AS fhsjn, fclspswl AS fhsqn, '0' AS fhsdqn FROM zhzs_bill_clspdjxx WHERE substr(fcldjsj, 1, 4) = '2022' GROUP BY substr(fcldjsj, 1, 4), fssqx, fclspswl UNION ALL SELECT substr(fcldjsj, 1, 4) AS years, fssqx , '0' AS fhsjn, '0' AS fhsqn, fclspswl AS fhsdqn FROM zhzs_bill_clspdjxx WHERE substr(fcldjsj, 1, 4) = '2021' GROUP BY substr(fcldjsj, 1, 4), fssqx, fclspswl ) b ON a.fyears = b.years and a.fskgk = b.fssqx GROUP BY fskgk ) GROUP BY ROLLUP (fskgk) ORDER BY CASE WHEN fskgk = '合计' THEN 1 ELSE 2 END
时间: 2024-04-27 12:25:28 浏览: 118
这是一段 SQL 查询语句,作用是对一个数据表中的数据进行聚合计算,并且按照一定的规则排序后返回结果。该查询语句主要包括以下几个部分:
1. SELECT 子句:选择需要查询的列,以及对这些列进行聚合计算的函数。
2. FROM 子句:指定需要查询的数据表。
3. WHERE 子句:指定过滤数据的条件。
4. GROUP BY 子句:根据指定的列对数据进行分组。
5. UNION ALL:将多个 SELECT 语句的结果合并成一个结果集。
6. JOIN:将两个数据表进行连接。
7. ROLLUP:对 GROUP BY 的结果进行汇总。
8. ORDER BY:根据指定的规则对结果进行排序。
具体查询的意义需要根据数据表的结构和业务需求进行分析。
相关问题
优化成MYSQL 脚本SELECT 子品牌, 商品数字ID, COUNT(DISTINCT o.用户id) AS new_customer_num, SUM(o.商品实付金额) AS new_customer_商品实付金额 FROM 天猫官旗有效数据 o LEFT JOIN ( -- 找到22年10月30日前有购买行为的客户编号 SELECT 用户id FROM 天猫官旗有效数据 WHERE 支付日期 BETWEEN '2021-10-31' AND '2022-10-30' GROUP BY 用户id ) old_c ON o.用户id = old_c.用户id WHERE o.支付日期 >= '2022-10-31' AND o.支付日期 <= '2022-11-11' AND NOT EXISTS ( SELECT 1 FROM 天猫官旗有效数据 WHERE 用户id = o.用户id AND 支付日期 BETWEEN '2021-10-31' AND '2022-10-30') GROUP BY GROUPING SETS ((子品牌, 商品数字ID))
以下是优化后的MYSQL脚本:
SELECT
子品牌,
商品数字ID,
COUNT(DISTINCT CASE WHEN o.支付日期 >= '2022-10-31' AND o.支付日期 <= '2022-11-11' AND old_c.用户id IS NULL THEN o.用户id ELSE NULL END) AS new_customer_num,
SUM(CASE WHEN o.支付日期 >= '2022-10-31' AND o.支付日期 <= '2022-11-11' AND old_c.用户id IS NULL THEN o.商品实付金额 ELSE 0 END) AS new_customer_商品实付金额
FROM 天猫官旗有效数据 o
LEFT JOIN (
SELECT 用户id
FROM 天猫官旗有效数据
WHERE 支付日期 BETWEEN '2021-10-31' AND '2022-10-30'
GROUP BY 用户id
) old_c ON o.用户id = old_c.用户id
WHERE o.支付日期 >= '2022-10-31' AND o.支付日期 <= '2022-11-11'
GROUP BY 子品牌, 商品数字ID;
这里进行了以下优化:
1. CASE语句替代NOT EXISTS子查询,提高查询效率。
2. 将GROUPING SETS修改为普通的GROUP BY语句,简化查询语句。
3. 将条件判断移动到CASE语句中,减少不必要的计算。
mysql简单的行转列
MySQL中行转列操作可以使用PIVOT函数或者CASE WHEN语句来实现。具体实现方法如下:
1. PIVOT函数
PIVOT函数可以将一列数据转换成多列,使用方法如下:
```
SELECT
[Grouping Column],
[Pivot Column 1], [Pivot Column 2],..., [Pivot Column N]
FROM
[Source Table]
PIVOT
(
[Aggregation Function]([Value Column])
FOR [Pivot Column] IN ([Pivot Column 1], [Pivot Column 2],..., [Pivot Column N])
) AS [Pivot Table];
```
其中,[Grouping Column]是需要分组的列名,[Pivot Column 1], [Pivot Column 2],..., [Pivot Column N]是需要转换的列名,[Aggregation Function]是聚合函数(如SUM、AVG等),[Value Column]是需要聚合的值列名,[Source Table]是源数据表名,[Pivot Table]是输出的表名。
例如,将学生的成绩按科目进行转换:
```
SELECT
name,
MAX(CASE WHEN subject = 'Math' THEN score ELSE NULL END) AS Math,
MAX(CASE WHEN subject = 'English' THEN score ELSE NULL END) AS English,
MAX(CASE WHEN subject = 'Science' THEN score ELSE NULL END) AS Science
FROM
scores
GROUP BY
name;
```
2. CASE WHEN语句
使用CASE WHEN语句可以将多个行转换成一行,使用方法如下:
```
SELECT
[Grouping Column],
MAX(CASE WHEN [Condition 1] THEN [Value 1] ELSE NULL END) AS [Pivot Column 1],
MAX(CASE WHEN [Condition 2] THEN [Value 2] ELSE NULL END) AS [Pivot Column 2],
...,
MAX(CASE WHEN [Condition N] THEN [Value N] ELSE NULL END) AS [Pivot Column N]
FROM
[Source Table]
GROUP BY
[Grouping Column];
```
其中,[Grouping Column]是需要分组的列名,[Condition 1], [Condition 2],..., [Condition N]是条件语句,[Value 1], [Value 2],..., [Value N]是需要转换的列名,[Source Table]是源数据表名。
例如,将学生的成绩进行转换:
```
SELECT
name,
MAX(CASE WHEN subject = 'Math' THEN score ELSE NULL END) AS Math,
MAX(CASE WHEN subject = 'English' THEN score ELSE NULL END) AS English,
MAX(CASE WHEN subject = 'Science' THEN score ELSE NULL END) AS Science
FROM
scores
GROUP BY
name;
```
阅读全文