我有这张桌子
|model size color customer|
|ip4g 8 black Boy |
|ip4g 8 white Boy |
|ip4g 16 white Girl |
|ip4g 16 black Girl |
我知道如何通过
查询计数Select model, size, color, count(*) from table group by model, size, color;
我需要的是生成一个看起来像这样的excel表。
我不知道如何生成= 0和每位客户的计数。
我制作了一张包含所有可能组合的表格。 然后做了这个查询:
select x.model, x.size, x.color, sum(y.customer), count(y.*)
from table x left join table y on x.model = y.model
and x.size = y.size and x.color = y.color group by
x.modelname, x.size, x.color;
我的数据少于预期。然后,我还需要向所有客户展示, 客户数量可能会有所不同。
请帮忙。感谢。
答案 0 :(得分:1)
使用SUM()
计算每个客户的行数:
Select model, size, color,
SUM(customer = 'Customer 1') AS Customer1,
SUM(customer = 'Customer 2') AS Customer2,
SUM(customer = 'Customer 3') AS Customer3,
SUM(customer = 'Customer 4') AS Customer4,
SUM(customer = 'Customer 5') AS Customer5,
count(*) AS Total
from table
group by model, size, color;
如果客户匹配,则 customer = 'Customer N'
为1
,如果不匹配则为0
,因此这将计算每个客户的行数。