如何从数据库中获取最近 13 个月的数据

How do get last 13 months data from database

我有以下内容table我想根据我的客户订单和月份获取最近 13 个月的数据:

create table dynamicorders 
(
    order_date date,
    order_total number(8),
    name varchar2(100)
);


insert into dynamicorders values( '2021-01-05', 705, 'jhone'); 
insert into dynamicorders values( '2021-01-15', 715, 'steve'); 
insert into dynamicorders values( '2021-01-17', 50, 'jhone'); 
insert into dynamicorders values( '2021-02-09', 802, 'mular'); 
insert into dynamicorders values( '2021-03-12', 812, 'steve'); 
insert into dynamicorders values( '2021-03-29', 829, 'steve'); 
insert into dynamicorders values( '2021-04-03', 903, 'jhone'); 
insert into dynamicorders values( '2021-04-29', 150, 'steve'); 
insert into dynamicorders values( '2021-05-19', 100, 'steve'); 
insert into dynamicorders values( '2022-01-10', 300, 'steve');
insert into dynamicorders values( '2022-01-18', 400, 'mular');

我需要的结果集截图如下:

SELECT * FROM dynamicorders WHERE order_date >= DATEADD(MONTH, -13, CAST(GETDATE() AS DATE))

使用DATEADD函数,需要三个参数DATEADD(interval, number, date)

第一个参数是 interval(YEAR/MONTH/DAY)
第二个参数是number(你想根据间隔增加日期的次数)。
第三个参数是date你要修改的日期

查看此答案以获取更多信息 Grab data from the last 13 months
另请查看 Microsoft 官方页面上 DATEADDdocumentation 以了解有关此功能的详细信息。

您可以使用:

SELECT name,
       SUM(CASE TRUNC(order_date, 'MM') WHEN DATE '2021-01-01' THEN order_total END) AS "21-Jan",
       SUM(CASE TRUNC(order_date, 'MM') WHEN DATE '2021-02-01' THEN order_total END) AS "21-Feb",
       SUM(CASE TRUNC(order_date, 'MM') WHEN DATE '2021-03-01' THEN order_total END) AS "21-Mar",
       SUM(CASE TRUNC(order_date, 'MM') WHEN DATE '2021-04-01' THEN order_total END) AS "21-Apr",
       SUM(CASE TRUNC(order_date, 'MM') WHEN DATE '2021-05-01' THEN order_total END) AS "21-May",
       SUM(CASE TRUNC(order_date, 'MM') WHEN DATE '2021-06-01' THEN order_total END) AS "21-Jun",
       SUM(CASE TRUNC(order_date, 'MM') WHEN DATE '2021-07-01' THEN order_total END) AS "21-Jul",
       SUM(CASE TRUNC(order_date, 'MM') WHEN DATE '2021-08-01' THEN order_total END) AS "21-Aug",
       SUM(CASE TRUNC(order_date, 'MM') WHEN DATE '2021-09-01' THEN order_total END) AS "21-Sep",
       SUM(CASE TRUNC(order_date, 'MM') WHEN DATE '2021-10-01' THEN order_total END) AS "21-Oct",
       SUM(CASE TRUNC(order_date, 'MM') WHEN DATE '2021-11-01' THEN order_total END) AS "21-Nov",
       SUM(CASE TRUNC(order_date, 'MM') WHEN DATE '2021-12-01' THEN order_total END) AS "21-Dec",
       SUM(CASE TRUNC(order_date, 'YY') WHEN DATE '2021-01-01' THEN order_total END) AS TOTAL,
       SUM(CASE TRUNC(order_date, 'MM') WHEN DATE '2022-01-01' THEN order_total END) AS "22-Jan"
FROM   dynamicorders d
GROUP BY name

对于您的示例数据,输出:

NAME 21-Jan 21-Feb 21-Mar 21-Apr 21-May 21-Jun 21-Jul 21-Aug 21-Sep 21-Oct 21-Nov 21-Dec TOTAL 22-Jan
jhone 755 null null 903 null null null null null null null null 1658 null
steve 715 null 1641 150 100 null null null null null null null 2606 300
mular null 802 null null null null null null null null null null 802 400

如果您想要过去 13 个月而不是特定月份,那么您 不能 提供与月份相关的动态列名称(除非您使用动态 SQL)并且可以使用:

SELECT name,
       SUM(CASE TRUNC(order_date, 'MM') WHEN ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -12) THEN order_total END) AS "CURRENT_MONTH-12",
       SUM(CASE TRUNC(order_date, 'MM') WHEN ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -11) THEN order_total END) AS "CURRENT_MONTH-11",
       SUM(CASE TRUNC(order_date, 'MM') WHEN ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -10) THEN order_total END) AS "CURRENT_MONTH-10",
       SUM(CASE TRUNC(order_date, 'MM') WHEN ADD_MONTHS(TRUNC(SYSDATE, 'MM'),  -9) THEN order_total END) AS "CURRENT_MONTH-9",
       SUM(CASE TRUNC(order_date, 'MM') WHEN ADD_MONTHS(TRUNC(SYSDATE, 'MM'),  -8) THEN order_total END) AS "CURRENT_MONTH-8",
       SUM(CASE TRUNC(order_date, 'MM') WHEN ADD_MONTHS(TRUNC(SYSDATE, 'MM'),  -7) THEN order_total END) AS "CURRENT_MONTH-7",
       SUM(CASE TRUNC(order_date, 'MM') WHEN ADD_MONTHS(TRUNC(SYSDATE, 'MM'),  -6) THEN order_total END) AS "CURRENT_MONTH-6",
       SUM(CASE TRUNC(order_date, 'MM') WHEN ADD_MONTHS(TRUNC(SYSDATE, 'MM'),  -5) THEN order_total END) AS "CURRENT_MONTH-5",
       SUM(CASE TRUNC(order_date, 'MM') WHEN ADD_MONTHS(TRUNC(SYSDATE, 'MM'),  -4) THEN order_total END) AS "CURRENT_MONTH-4",
       SUM(CASE TRUNC(order_date, 'MM') WHEN ADD_MONTHS(TRUNC(SYSDATE, 'MM'),  -3) THEN order_total END) AS "CURRENT_MONTH-3",
       SUM(CASE TRUNC(order_date, 'MM') WHEN ADD_MONTHS(TRUNC(SYSDATE, 'MM'),  -2) THEN order_total END) AS "CURRENT_MONTH-2",
       SUM(CASE TRUNC(order_date, 'MM') WHEN ADD_MONTHS(TRUNC(SYSDATE, 'MM'),  -1) THEN order_total END) AS "CURRENT_MONTH-1",
       SUM(CASE WHEN ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -12) <= order_date AND order_date < TRUNC(SYSDATE, 'MM') THEN order_total END) AS TOTAL,
       SUM(CASE TRUNC(order_date, 'MM') WHEN TRUNC(SYSDATE, 'MM') THEN order_total END) AS "CURRENT_MONTH"
FROM   dynamicorders d
GROUP BY name

输出:

NAME CURRENT_MONTH-12 CURRENT_MONTH-11 CURRENT_MONTH-10 CURRENT_MONTH-9 CURRENT_MONTH-8 CURRENT_MONTH-7 CURRENT_MONTH-6 CURRENT_MONTH-5 CURRENT_MONTH-4 CURRENT_MONTH-3 CURRENT_MONTH-2 CURRENT_MONTH-1 TOTAL CURRENT_MONTH
jhone 755 null null 903 null null null null null null null null 1658 null
steve 715 null 1641 150 100 null null null null null null null 2606 300
mular null 802 null null null null null null null null null null 802 400

db<>fiddle here

这是 PIVOT 函数的 典型 用例。

with mon as (
select 
trunc(ORDER_DATE,'MM') ORDER_MONTH, ORDER_TOTAL, NAME
from dynamicorders)
select * from mon
PIVOT (sum(ORDER_TOTAL) ORDER_TOTAL  for (ORDER_MONTH) in 
(date'2021-01-01' as "21-Jan",
 date'2021-02-01' as "21-Feb",
 date'2021-03-01' as "21-Mar",
 date'2021-04-01' as "21-Apr",
 date'2021-05-01' as "21-May",
 date'2021-06-01' as "21-Jun",
 date'2021-07-01' as "21-Jul",
 date'2021-08-01' as "21-Aug",
 date'2021-09-01' as "21-Sep",
 date'2021-10-01' as "21-Oct",
 date'2021-11-01' as "21-Nov",
 date'2021-12-01' as "21-Dec"
)
);

dynamic 最后 N 个月你将需要 dynamic SQL,因为 PIVOT 需要 静态表达式。