2016-10-19 20 views
0

に選択します。SQLは、CONCAT、いくつかの私がデータを持つテーブルを持っている一つの大きなテーブル

  1. YYYY/MM/DD HH:MM:SS - ビュー数、クリック数、など。
  2. YYYY/MM/DD HH:MM:SS - ...

これらの列をすべて合計して合計値にする必要があります。 ように: 1. YYYY/MM/DD - ビューの合計、クリック

私がやっていること

の合計: 1.クエリのforeach列。 ビューの場合。

select 
    cast([EventTime] as date) as 'Date', 
    Count([Id]) as 'Views' 
from [TelemetryData] 
where [DiscountId] = '8773fd1b-f0c0-41fd-b0a0-ab8238f227f5' 
    and [EventName]  = 'DiscountView' 
group by cast([EventTime] as date) 
order by cast([EventTime] as date) asc 

日あたりのクリック数:私は日ごとに1つの行にそれらのすべてを合計するにはどうすればよい

select 
    cast([EventTime] as date) as 'Date', 
    Count([Id]) as 'Clicks' 
from [TelemetryData] 
where [DiscountId] = '8773fd1b-f0c0-41fd-b0a0-ab8238f227f5' 
    and [EventName] =  'DiscountClick' 
group by cast([EventTime] as date) 
order by cast([EventTime] as date) asc 

答えて

1

チェックし、合計でそれを試してみてください。

てきた何
SELECT cast([EventTime] as date) as 'Date', SUM(case when [EventName]='DiscountClick' then 1 else 0 end) as 'Clicks', SUM(case when [EventName]='DiscountView' then 1 else 0 end) as 'Views' 
from [TelemetryData] 
where [DiscountId] = '8773fd1b-f0c0-41fd-b0a0-ab8238f227f5' 
and ([EventName] = 'DiscountView' or [EventName] = 'DiscountClick') 
group by cast([EventTime] as date) 
order by cast([EventTime] as date) asc 

変更:

最初にビューとクリックの両方をフィルタリングする列がどこにあるかを確認してください。

その後、我々はときEVENT =我々は1を追加しているSOMETHING、あなたのグループとそうでない場合は0

SUMを使用しますがCASEでは、1つのクエリで日によって両方を取得します。

+0

優秀!どうも!合計する列が7つ増えますので、追加が簡単です。コード:\t SUM([EventName] = 'DiscountView'、次に1 else 0 end) 'Views'、 \t SUM([EventName] = 'DiscountClick'、次に1 else 0 end)を 'Clicks'とすると、ラウンドからのクリック率が必要です(キャスト(クリック)をフロートとして)/キャスト(ビューをフロート)* 100,2)を '平均クリック率' – Nerf

0

CASEで使用するSUM ..

sum(case when [DiscountId] = '8773fd1b-f0c0-41fd-b0a0-ab8238f227f5' 
    and [EventName] =  'DiscountClick' then noofclicks column else 0 end 
) as clicks 

sum(case when [DiscountId] = '8773fd1b-f0c0-41fd-b0a0-ab8238f227f5' 
    and [EventName] =  'DiscountView' then view column else 0 end 
) as views 

など他のために....

from 
table 
group by cast([EventTime] as date) 
order by cast([EventTime] as date) 
関連する問題