SQL:如何处理NULL和PARTITION BY?

发布于 2025-01-11 09:41:55 字数 2381 浏览 0 评论 0原文

我有一个问题,如果你不介意的话。 假设我这里有这样一个表格 – 产品(2000 年按季度销售的金额,只有同一产品和季度(具有不同日期)有多个条目):

产品季度销售金额
牛仔裤120
牛仔裤240
牛仔裤360
牛仔裤45
裙子110
裙子25
裙子330
衬衫115
衬衫240
衬衫360
Blouse415

一下,如下:

产品quarter1quarter2quarter3quarter4Jeans不同5
重新2040605Skirt
401030介绍Null
Blouse1560我想15

我决定用分区来做(因为它不那么简单,有 相同产品的同一季度的行,但销售量不同,这就是为什么sum(amount_sold),但我希望你明白了):

WITH quater_sales as(
SELECT DISTINCT pro.product, pro.quarter, to_char (sum(pro.amount_sold) OVER (PARTITION BY pro.product, pro.quarter)) AS quater
FROM products pro
ORDER BY pro.pro.product)
SELECT quater_sales.prod_product, quater_sales.quater AS "Q1", qu2.quater AS "Q2", qu3.quater AS "Q3", qu4.quater AS "Q4"
FROM quater_sales
 JOIN quater_sales qu2 ON quater_sales.prod_subcategory=qu2.prod_subcategory
 JOIN quater_sales qu3 ON quater_sales.prod_subcategory=qu3.prod_subcategory
 JOIN quater_sales qu4 ON quater_sales.prod_subcategory=qu4.prod_subcategory
WHERE quater_sales.calendar_quarter_number=1 and qu2.calendar_quarter_number=2 and qu3.calendar_quarter_number=3 and qu4.calendar_quarter_number=4

问题在于分区(或者可能是选择的条件),即在所有 4 个季度中未售出的产品被丢弃。我最终得到的基本上是这样的:

如何15出现
60裙子
那么产品quarter1quarter2quarter3quarter4Jeans 20 40 60 5 Blouse 15 40那里

?我有点坚持这个。

I've got a question, if you don't mind terribly.
So suppose I have this kind of a table here – Products (amount sold by quarter in 2000, only there are multiple entries for the same product and quarter (with different dates)):

productquarteramount sold
Jeans120
Jeans240
Jeans360
Jeans45
Skirt110
Skirt25
Skirt330
Blouse115
Blouse240
Blouse360
Blouse415

I want to reintroduce it as follows:

productquarter1quarter2quarter3quarter4
Jeans2040605
Skirt10530Null
Blouse15406015

I decided to do it with partition (cause it's not exactly that simple, there are different rows with the same quarter for the same product, but different amount sold, that's why it's sum(amount_sold), but you get the idea, I hope):

WITH quater_sales as(
SELECT DISTINCT pro.product, pro.quarter, to_char (sum(pro.amount_sold) OVER (PARTITION BY pro.product, pro.quarter)) AS quater
FROM products pro
ORDER BY pro.pro.product)
SELECT quater_sales.prod_product, quater_sales.quater AS "Q1", qu2.quater AS "Q2", qu3.quater AS "Q3", qu4.quater AS "Q4"
FROM quater_sales
 JOIN quater_sales qu2 ON quater_sales.prod_subcategory=qu2.prod_subcategory
 JOIN quater_sales qu3 ON quater_sales.prod_subcategory=qu3.prod_subcategory
 JOIN quater_sales qu4 ON quater_sales.prod_subcategory=qu4.prod_subcategory
WHERE quater_sales.calendar_quarter_number=1 and qu2.calendar_quarter_number=2 and qu3.calendar_quarter_number=3 and qu4.calendar_quarter_number=4

The problem is with partition (or maybe it's the condition of select) that the product that was not sold in all the 4 quarters is just discarded. What I basically get in the end is this:

productquarter1quarter2quarter3quarter4
Jeans2040605
Blouse15406015

So how do I make "skirts" appear there too? I am a bit stuck with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春风十里 2025-01-18 09:41:55

您是否考虑过使用 PIVOT 语句?

WITH
    quarter_sales (product, quarter, amount_sold)
    AS
        (SELECT 'Jeans', 1, 20 FROM DUAL
         UNION ALL
         SELECT 'Jeans', 2, 40 FROM DUAL
         UNION ALL
         SELECT 'Jeans', 3, 60 FROM DUAL
         UNION ALL
         SELECT 'Jeans', 4, 5 FROM DUAL
         UNION ALL
         SELECT 'Skirt', 1, 10 FROM DUAL
         UNION ALL
         SELECT 'Skirt', 2, 5 FROM DUAL
         UNION ALL
         SELECT 'Skirt', 3, 30 FROM DUAL
         UNION ALL
         SELECT 'Blouse', 1, 15 FROM DUAL
         UNION ALL
         SELECT 'Blouse', 2, 40 FROM DUAL
         UNION ALL
         SELECT 'Blouse', 3, 60 FROM DUAL
         UNION ALL
         SELECT 'Blouse', 4, 15 FROM DUAL)
SELECT *
  FROM (SELECT *
          FROM quarter_sales qs)
       PIVOT (SUM (amount_sold)
             FOR quarter
             IN (1 AS quarter1, 2 AS quarter2, 3 AS quarter3, 4 AS quarter4));


   PRODUCT    QUARTER1    QUARTER2    QUARTER3    QUARTER4
__________ ___________ ___________ ___________ ___________
Blouse              15          40          60          15
Jeans               20          40          60           5
Skirt               10           5          30

Have you considered using a PIVOT statement?

WITH
    quarter_sales (product, quarter, amount_sold)
    AS
        (SELECT 'Jeans', 1, 20 FROM DUAL
         UNION ALL
         SELECT 'Jeans', 2, 40 FROM DUAL
         UNION ALL
         SELECT 'Jeans', 3, 60 FROM DUAL
         UNION ALL
         SELECT 'Jeans', 4, 5 FROM DUAL
         UNION ALL
         SELECT 'Skirt', 1, 10 FROM DUAL
         UNION ALL
         SELECT 'Skirt', 2, 5 FROM DUAL
         UNION ALL
         SELECT 'Skirt', 3, 30 FROM DUAL
         UNION ALL
         SELECT 'Blouse', 1, 15 FROM DUAL
         UNION ALL
         SELECT 'Blouse', 2, 40 FROM DUAL
         UNION ALL
         SELECT 'Blouse', 3, 60 FROM DUAL
         UNION ALL
         SELECT 'Blouse', 4, 15 FROM DUAL)
SELECT *
  FROM (SELECT *
          FROM quarter_sales qs)
       PIVOT (SUM (amount_sold)
             FOR quarter
             IN (1 AS quarter1, 2 AS quarter2, 3 AS quarter3, 4 AS quarter4));


   PRODUCT    QUARTER1    QUARTER2    QUARTER3    QUARTER4
__________ ___________ ___________ ___________ ___________
Blouse              15          40          60          15
Jeans               20          40          60           5
Skirt               10           5          30
只有一腔孤勇 2025-01-18 09:41:55

尝试枢轴。这就是您在 tsql

declare @tmp as table(product varchar(20),quarter int,[amount sold] int);

insert into @tmp values
('Jeans',   1,  20)
,('Jeans',  2,  40)
,('Jeans',  3,  60)
,('Jeans',  4,  5)
,('Skirt',  1,  10)
,('Skirt',  2,  5)
,('Skirt',  3,  30)
,('Blouse', 1,  15)
,('Blouse', 2,  40)
,('Blouse', 3,  60)
,('Blouse', 4,  15)


    select  product, [1] as quarter1,[2] as quarter2,[3] as quarter3,[4] as quarter4
from
(
select product,quarter,[amount sold] from @tmp)p
pivot
(
sum([amount sold])
for quarter in([1],[2],[3],[4])
) as pvt

输出中进行透视的方式:

product quarter1    quarter2    quarter3    quarter4
Blouse  15          40          60          15
Jeans   20          40          60          5
Skirt   10          5           30          NULL

try pivot. this is how you would pivot in tsql

declare @tmp as table(product varchar(20),quarter int,[amount sold] int);

insert into @tmp values
('Jeans',   1,  20)
,('Jeans',  2,  40)
,('Jeans',  3,  60)
,('Jeans',  4,  5)
,('Skirt',  1,  10)
,('Skirt',  2,  5)
,('Skirt',  3,  30)
,('Blouse', 1,  15)
,('Blouse', 2,  40)
,('Blouse', 3,  60)
,('Blouse', 4,  15)


    select  product, [1] as quarter1,[2] as quarter2,[3] as quarter3,[4] as quarter4
from
(
select product,quarter,[amount sold] from @tmp)p
pivot
(
sum([amount sold])
for quarter in([1],[2],[3],[4])
) as pvt

output:

product quarter1    quarter2    quarter3    quarter4
Blouse  15          40          60          15
Jeans   20          40          60          5
Skirt   10          5           30          NULL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文