Pivot 的 PLSQL 结果 Table
PLSQL results to Pivot Table
我想在 PLSQL 中运行查询以获取过去六个月内数据table中的销售数量总和,并将其按月分配给列。
到目前为止,我将这段代码 select 我想要的数据
SELECT
*
FROM
(
select
CATALOG_NO as PartNo,
to_char(INVOICE_DATE, 'MM') as tday,
sum(INVOICED_QTY) as qty
from
db.INV_ITEM_JOIN
WHERE
upper(CATALOG_GROUP) = upper('SPAM')
AND INVOICE_DATE between add_months(
trunc(sysdate, 'mm'),
-6
)
and last_day(
add_months(
trunc(sysdate, 'mm'),
-1
)
)
group by
to_char(INVOICE_DATE, 'MM'),
CATALOG_NO
order by
CATALOG_NO
)
然后我得到了结果,为了创建 PIVOT,我在 select 查询
之后添加了这段代码
PIVOT (
SUM(qty) FOR TDAY IN (
01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11,
12
)
)
所以一旦我添加它 returns table as PartNo
, 01
,02
,03
,04
,05
,06
,07
,08
,09
,10
,11
,12
像个聪明人。
但是数据只去了10
11
12
个月而已,其他的没有填。我能得到解决这个问题的帮助吗?
TO_CHAR(INVOICE_DATE, 'MM')
输出一个零填充的两位数字符串,因此您要么想使用:
PIVOT (
SUM(qty) FOR TDAY IN (
'01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'
)
)
或者,使用 EXTRACT(MONTH FROM invoice_date)
而不是 TO_CHAR
然后你可以使用数值:
SELECT *
FROM (
select CATALOG_NO as PartNo,
EXTRACT(MONTH FROM INVOICE_DATE) as tday,
sum(INVOICED_QTY) as qty
from db.INV_ITEM_JOIN
WHERE upper(CATALOG_GROUP) = 'SPAM'
AND INVOICE_DATE >= add_months(trunc(sysdate, 'mm'), -6)
AND INVOICE_DATE < trunc(sysdate, 'mm')
GROUP BY
EXTRACT(MONTH FROM INVOICE_DATE),
CATALOG_NO
order by
CATALOG_NO
)
PIVOT (
SUM(qty) FOR TDAY IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
)
顺便说一句:
INVOICE_DATE between add_months(trunc(sysdate, 'mm'), -6)
and last_day(add_months(trunc(sysdate, 'mm'), -1))
将排除上个月最后一天时间从 00:00:01 到 23:59:59 的值。如果您想包括整个范围,请使用:
AND INVOICE_DATE >= add_months(trunc(sysdate, 'mm'), -6)
AND INVOICE_DATE < trunc(sysdate, 'mm')
db<>fiddle here
我想在 PLSQL 中运行查询以获取过去六个月内数据table中的销售数量总和,并将其按月分配给列。
到目前为止,我将这段代码 select 我想要的数据
SELECT
*
FROM
(
select
CATALOG_NO as PartNo,
to_char(INVOICE_DATE, 'MM') as tday,
sum(INVOICED_QTY) as qty
from
db.INV_ITEM_JOIN
WHERE
upper(CATALOG_GROUP) = upper('SPAM')
AND INVOICE_DATE between add_months(
trunc(sysdate, 'mm'),
-6
)
and last_day(
add_months(
trunc(sysdate, 'mm'),
-1
)
)
group by
to_char(INVOICE_DATE, 'MM'),
CATALOG_NO
order by
CATALOG_NO
)
然后我得到了结果,为了创建 PIVOT,我在 select 查询
之后添加了这段代码PIVOT (
SUM(qty) FOR TDAY IN (
01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11,
12
)
)
所以一旦我添加它 returns table as PartNo
, 01
,02
,03
,04
,05
,06
,07
,08
,09
,10
,11
,12
像个聪明人。
但是数据只去了10
11
12
个月而已,其他的没有填。我能得到解决这个问题的帮助吗?
TO_CHAR(INVOICE_DATE, 'MM')
输出一个零填充的两位数字符串,因此您要么想使用:
PIVOT (
SUM(qty) FOR TDAY IN (
'01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'
)
)
或者,使用 EXTRACT(MONTH FROM invoice_date)
而不是 TO_CHAR
然后你可以使用数值:
SELECT *
FROM (
select CATALOG_NO as PartNo,
EXTRACT(MONTH FROM INVOICE_DATE) as tday,
sum(INVOICED_QTY) as qty
from db.INV_ITEM_JOIN
WHERE upper(CATALOG_GROUP) = 'SPAM'
AND INVOICE_DATE >= add_months(trunc(sysdate, 'mm'), -6)
AND INVOICE_DATE < trunc(sysdate, 'mm')
GROUP BY
EXTRACT(MONTH FROM INVOICE_DATE),
CATALOG_NO
order by
CATALOG_NO
)
PIVOT (
SUM(qty) FOR TDAY IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
)
顺便说一句:
INVOICE_DATE between add_months(trunc(sysdate, 'mm'), -6)
and last_day(add_months(trunc(sysdate, 'mm'), -1))
将排除上个月最后一天时间从 00:00:01 到 23:59:59 的值。如果您想包括整个范围,请使用:
AND INVOICE_DATE >= add_months(trunc(sysdate, 'mm'), -6)
AND INVOICE_DATE < trunc(sysdate, 'mm')
db<>fiddle here