Mysql 基于值从一行中创建单独的列

Mysql 基于值从一行中创建单独的列,mysql,sql,Mysql,Sql,我有一个名为Source的列,它有两个值“1”和“2”。 我想在两列中显示它,比如Source_1,它将只显示值“1”,Source_2,它将显示值“2”。 鉴于我的疑问,我不确定如何实现这一目标: Select b.[path], count(*) as "No of Calls", count(a.Source) as "Source_1 calls", count(a.Source) as "Source_2 calls", a.TimeD

我有一个名为Source的列,它有两个值“1”和“2”。 我想在两列中显示它,比如Source_1,它将只显示值“1”,Source_2,它将显示值“2”。 鉴于我的疑问,我不确定如何实现这一目标:

    Select 
    b.[path],
    count(*) as "No of Calls",
    count(a.Source) as "Source_1 calls",
    count(a.Source) as "Source_2 calls",
    a.TimeDataRetrieval as "DB Retrieval time",
    a.TimeProcessing as "Processing time",
    a.TimeRendering as "Rendering Time"

    FROM LogStorage a inner join Catalog b on a.[ReportID] = b.[ItemID]

    where b.[path] = ('/../some_path')  and a.[Source]=2
    group by b.[path]
这就是我被困的地方。在where子句中我应该做什么,因为我不能有一个[Source]=1和2


谢谢。

使用
条件聚合

Select 
b.[path],
count(*) as "No of Calls",
count(case when a.Source=1 then 1 end) as "Source_1 calls",
count(case when a.Source=2 then 1 end) as "Source_2 calls",
a.TimeDataRetrieval as "DB Retrieval time",
a.TimeProcessing as "Processing time",
a.TimeRendering as "Rendering Time"
FROM LogStorage a inner join Catalog b on a.[ReportID] = b.[ItemID]

where b.[path] = ('/../some_path')  and a.[Source]=2
group by b.[path]
或者干脆

sum(a.Source=1) as "Source_1 calls",
sum(a.Source=2) as "Source_2 calls",

使用条件聚合

Select 
b.[path],
count(*) as "No of Calls",
count(case when a.Source=1 then 1 end) as "Source_1 calls",
count(case when a.Source=2 then 1 end) as "Source_2 calls",
a.TimeDataRetrieval as "DB Retrieval time",
a.TimeProcessing as "Processing time",
a.TimeRendering as "Rendering Time"
FROM LogStorage a inner join Catalog b on a.[ReportID] = b.[ItemID]

where b.[path] = ('/../some_path')  and a.[Source]=2
group by b.[path]
或者干脆

sum(a.Source=1) as "Source_1 calls",
sum(a.Source=2) as "Source_2 calls",

非常感谢你。成功了。我没想过,非常感谢。成功了。我没想过。