2016-10-07 7 views
0

2つの値「1」と「2」を持つSourceという列があります。 私はSource_1のような2列に表示したいだけで値 "1"、Source_2に値 "2"を表示します。 私はそれが私が持っているクエリを与え達成はどうすればよいかわからない:値に基づいて1つの行から別の列を作成する

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節で何をすればいいのですか。なぜなら私はaを持つことができないからです。[Source] = 1と2

ありがとう。

答えて

3

使用Conditional Aggregate

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", 
+0

ありがとうございました。それはうまくいった。それについて考えなかった。 – Jatin

関連する問題