gpt4 book ai didi

mysql - 如何生成三列计数表?

转载 作者:行者123 更新时间:2023-11-29 12:25:39 25 4
gpt4 key购买 nike

我有这张 table

 |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 表格。

enter image description here

我不知道如何才能生成 = 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;

我得到的数据比预期少。然后,我还需要向所有顾客展示,并且客户数量可能会有所不同。

请帮忙。谢谢。

最佳答案

使用 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,因此这将计算行数每个客户。

关于mysql - 如何生成三列计数表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28357640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com