gpt4 book ai didi

mysql - SQL,使用数学将行转换为列

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

我有这张表:

Player    Routes   Colors

GIN 2 RED
GIN 1 BLU
GIN 5 BLAS
GIN 1 TREN
GIN 2 RED
PON 1 TREN
PON 4 BLU

来自带有 union all 的查询,如下所示:

select *
from (
select DISTINCT Colors.Name [Colors]
, COUNT(routes.idRoutes) [Routes]
, Routes.cdRoutes [Doc]
from Routes
inner join Colors on Colors.Name blablabla
where blablabla
group by Colors.Name, Routes.cdRoutes

) dates

我需要一个像这样的表格:

Colors    GIN    PON

RED 4 0
BLU 1 4
BLAS 5 0
TREN 1 1

如何做?我想我可以不使用PIVOT函数来获取它,对吗?

最佳答案

你可以这样做(即使不使用 PIVOT):

select
Colors,
sum(case when Player = 'GIN' then Routes else 0 end) as GIN,
sum(case when Player = 'PON' then Routes else 0 end) as PON
from your_query_or_Table
group by Colors

关于mysql - SQL,使用数学将行转换为列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29299586/

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