gpt4 book ai didi

mysql - 在 mysql 中使用函数创建列别名?

转载 作者:行者123 更新时间:2023-11-29 03:04:51 25 4
gpt4 key购买 nike

我有这张 table

sales(productid, sales_date)

我想获取过去 12 个月的月度销售额。

如何在列别名上生成月份名称(无需硬编码)?

我试过这个:-

http://sqlfiddle.com/#!2/ee777/9

但我想要这种格式的结果表

productid   Oct-12  Nov-12  Dec-12  Jan-13  Feb-13  Mar-13  Apr-13  May-13
1 0 0 3 4 2 8 2 11
2 0 5 6 8 2 0 0 0

我尝试使用函数 DATE_FORMAT(sales_date, '%M') 给列别名但它是语法错误。

select productid,sum(if(DATE_FORMAT(sales_date,'%Y%m')=
DATE_FORMAT(date_sub(curdate(),interval 12 month),'%Y%m'),1,0))
`12 Months Before`,
sum(if(DATE_FORMAT(sales_date,'%Y%m')=
DATE_FORMAT(date_sub(curdate(),interval 11 month),'%Y%m'),1,0))
`11 Months Before`,
sum(if(DATE_FORMAT(sales_date,'%Y%m')=
DATE_FORMAT(date_sub(curdate(),interval 10 month),'%Y%m'),1,0))
`10 Months Before`,
sum(if(DATE_FORMAT(sales_date,'%Y%m')=
DATE_FORMAT(date_sub(curdate(),interval 9 month),'%Y%m'),1,0))
`9 Months Before`,
sum(if(DATE_FORMAT(sales_date,'%Y%m')=
DATE_FORMAT(date_sub(curdate(),interval 8 month),'%Y%m'),1,0))
`8 Months Before`,
sum(if(DATE_FORMAT(sales_date,'%Y%m')=
DATE_FORMAT(date_sub(curdate(),interval 7 month),'%Y%m'),1,0))
`7 Months Before`,
sum(if(DATE_FORMAT(sales_date,'%Y%m')=
DATE_FORMAT(date_sub(curdate(),interval 6 month),'%Y%m'),1,0))
`6 Months Before`,
sum(if(DATE_FORMAT(sales_date,'%Y%m')=
DATE_FORMAT(date_sub(curdate(),interval 5 month),'%Y%m'),1,0))
`5 Months Before`,
sum(if(DATE_FORMAT(sales_date,'%Y%m')=
DATE_FORMAT(date_sub(curdate(),interval 4 month),'%Y%m'),1,0))
`4 Months Before`,
sum(if(DATE_FORMAT(sales_date,'%Y%m')=
DATE_FORMAT(date_sub(curdate(),interval 3 month),'%Y%m'),1,0))
`3 Months Before`,
sum(if(DATE_FORMAT(sales_date,'%Y%m')=
DATE_FORMAT(date_sub(curdate(),interval 2 month),'%Y%m'),1,0))
`2 Months Before`,
sum(if(DATE_FORMAT(sales_date,'%Y%m')=
DATE_FORMAT(date_sub(curdate(),interval 1 month),'%Y%m'),1,0))
`1 Months Before`
from sales
group by productid;

我怎样才能得到月份和年份作为别名(例如:- Oct-12)来代替 12 Months Before , 11 Months Before等等。

最佳答案

MySql 没有PIVOT 所以你必须使用条件SUM()

有两种选择:

  1. 如果周期数有限且事先已知,您可以手动执行此操作
  2. 您可以使用动态 SQL 动态生成查询,然后执行它

option1 的查询可能如下所示

SELECT productid
,SUM(CASE WHEN DATE_FORMAT(sales_date, '%Y%m') = '201210' THEN 1 ELSE 0 END) `Oct12`
,SUM(CASE WHEN DATE_FORMAT(sales_date, '%Y%m') = '201211' THEN 1 ELSE 0 END) `Nov12`
,SUM(CASE WHEN DATE_FORMAT(sales_date, '%Y%m') = '201212' THEN 1 ELSE 0 END) `Dec12`
,SUM(CASE WHEN DATE_FORMAT(sales_date, '%Y%m') = '201301' THEN 1 ELSE 0 END) `Jan13`
,SUM(CASE WHEN DATE_FORMAT(sales_date, '%Y%m') = '201302' THEN 1 ELSE 0 END) `Feb13`
,SUM(CASE WHEN DATE_FORMAT(sales_date, '%Y%m') = '201303' THEN 1 ELSE 0 END) `Mar13`
,SUM(CASE WHEN DATE_FORMAT(sales_date, '%Y%m') = '201304' THEN 1 ELSE 0 END) `Apr13`
,SUM(CASE WHEN DATE_FORMAT(sales_date, '%Y%m') = '201305' THEN 1 ELSE 0 END) `May13`
,SUM(CASE WHEN DATE_FORMAT(sales_date, '%Y%m') = '201306' THEN 1 ELSE 0 END) `Jun13`
FROM sales
WHERE sales_date BETWEEN '2012-10-01' AND '2013-06-30'
GROUP BY productid

示例输出:

| PRODUCTID | OCT12 | NOV12 | DEC12 | JAN13 | FEB13 | MAR13 | APR13 | MAY13 | JUN13 |-------------------------------------------------------------------------------------|         1 |     0 |     0 |     0 |     1 |     1 |     0 |     0 |     2 |     1 ||         2 |     1 |     1 |     1 |     0 |     0 |     1 |     1 |     1 |     2 |

Here is SQLFiddle demo


Now a version for option 2 using dynamic SQL ( PREPARE, EXECUTE)

SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'SUM(CASE WHEN DATE_FORMAT(sales_date, ''%Y%m'') = ''',
DATE_FORMAT(period, '%Y%m'), ''' THEN 1 ELSE 0 END) `', DATE_FORMAT(period, '%b%y'), '`'
)
) INTO @sql
FROM
(
SELECT DATE_FORMAT(sales_date, '%Y-%m-01') period
FROM sales
WHERE sales_date BETWEEN DATE_FORMAT(CURDATE() - INTERVAL 11 MONTH, '%Y-%m-01') AND CURDATE()
GROUP BY DATE_FORMAT(sales_date, '%Y-%m-01')
) s;

SET @sql = CONCAT
('SELECT productid, ', @sql, '
FROM sales
WHERE sales_date BETWEEN DATE_FORMAT(CURDATE() - INTERVAL 11 MONTH, ''%Y-%m-01'') AND CURDATE()
GROUP BY productid'
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

输出相同。

| PRODUCTID | JUL12 | AUG12 | SEP12 | OCT12 | NOV12 | DEC12 | JAN13 | FEB13 | MAR13 | APR13 | MAY13 | JUN13 |-------------------------------------------------------------------------------------------------------------|         1 |     1 |     2 |     1 |     0 |     0 |     0 |     1 |     1 |     0 |     0 |     2 |     1 ||         2 |     0 |     0 |     0 |     1 |     1 |     1 |     0 |     0 |     1 |     1 |     1 |     2 |

这是 SQLFiddle 演示

关于mysql - 在 mysql 中使用函数创建列别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17376528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com