SQL:如何处理NULL和PARTITION BY?
我有一个问题,如果你不介意的话。 假设我这里有这样一个表格 – 产品(2000 年按季度销售的金额,只有同一产品和季度(具有不同日期)有多个条目):
产品 | 季度 | 销售金额 |
---|---|---|
牛仔裤 | 1 | 20 |
牛仔裤 | 2 | 40 |
牛仔裤 | 3 | 60 |
牛仔裤 | 4 | 5 |
裙子 | 1 | 10 |
裙子 | 2 | 5 |
裙子 | 3 | 30 |
衬衫 | 1 | 15 |
衬衫 | 2 | 40 |
衬衫 | 3 | 60 |
Blouse | 4 | 15 |
一下,如下:
产品 | quarter1quarter2quarter3quarter4Jeans | 不同 | 5 | 的 |
---|---|---|---|---|
重新 | 20 | 40 | 60 | 5Skirt |
40 | 10 | 30 | 介绍 | Null |
Blouse | 15 | 60 | 我想 | 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)):
product | quarter | amount sold |
---|---|---|
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 |
I want to reintroduce it as follows:
product | quarter1 | quarter2 | quarter3 | quarter4 |
---|---|---|---|---|
Jeans | 20 | 40 | 60 | 5 |
Skirt | 10 | 5 | 30 | Null |
Blouse | 15 | 40 | 60 | 15 |
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:
product | quarter1 | quarter2 | quarter3 | quarter4 |
---|---|---|---|---|
Jeans | 20 | 40 | 60 | 5 |
Blouse | 15 | 40 | 60 | 15 |
So how do I make "skirts" appear there too? I am a bit stuck with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否考虑过使用
PIVOT
语句?Have you considered using a
PIVOT
statement?尝试枢轴。这就是您在 tsql
输出中进行透视的方式:
try pivot. this is how you would pivot in tsql
output: