将服装产品的行显示为列

Posted

技术标签:

【中文标题】将服装产品的行显示为列【英文标题】:display rows as columns for apparel products 【发布时间】:2014-10-23 01:55:12 【问题描述】:

我正在做电子商务网站。试图将服装尺寸保持为列

sn-p 没有“group by productid”子句

select d.id,
(case when s.sizeid = 1 then 1 else 0 end) as "S"  ,
(case when s.sizeid = 2 then 1 else 0 end) as "M",
(case when s.sizeid = 3 then 1 else 0 end) as "L"  ,
(case when s.sizeid = 4 then 1 else 0 end) as "XL",
(case when s.sizeid = 5 then 1 else 0 end) as "XXL"  
from datatest d
inner join stock s on d.id = s.productid

具有像这样的“按产品 ID 分组”结果

select d.id,
(case when s.sizeid = 1 then 1 else 0 end) as "S"  ,
(case when s.sizeid = 2 then 1 else 0 end) as "M",
(case when s.sizeid = 3 then 1 else 0 end) as "L"  ,
(case when s.sizeid = 4 then 1 else 0 end) as "XL",
(case when s.sizeid = 5 then 1 else 0 end) as "XXL"  
from datatest d
inner join stock s on d.id = s.productid
group by d.id

请帮帮我

【问题讨论】:

您需要什么帮助?期望的结果是什么? 【参考方案1】:

我猜你想要的是所有尺寸列的总和,共享相同的产品 ID。

SUM() 函数会在您使用 GROUP BY 时为您执行此操作

select d.id,
SUM( (case when s.sizeid = 1 then 1 else 0 end) ) as "S"  ,
SUM( (case when s.sizeid = 2 then 1 else 0 end) ) as "M",
SUM( (case when s.sizeid = 3 then 1 else 0 end) ) as "L"  ,
SUM( (case when s.sizeid = 4 then 1 else 0 end) ) as "XL",
SUM( (case when s.sizeid = 5 then 1 else 0 end) ) as "XXL"  
from datatest d
inner join stock s on d.id = s.productid
group by d.id

【讨论】:

以上是关于将服装产品的行显示为列的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server - Pivot 将行转换为列(带有额外的行数据)

RecyclerView的行项目视图隐藏/显示在Recyclerview的滚动上搞砸了

奥威Power-BI服装行业BI数据可视化分析--动销商品分析

将具有动态日期的行转换为列

如何将具有重复值的行转换为列?

Magento 中可配置产品的高级搜索