2016-10-19 2 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 

번호 :

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 

가 어떻게 하루에 한 행으로 그들 모두를 요약 할 수 있습니까? CASE와

답변

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 

되었습니다 무엇 변경됨 :

처음으로 보기 및 클릭 모두를 필터링하기 위해 어디에서 열을 maded. EVENT = 뭔가 우리가 그룹으로, 그렇지 않으면 0

, 1을 추가 할 때

그런 다음 우리가 SUM하지만 케이스를 사용, 당신은 하나 개의 쿼리에서 일 모두에 의해 얻을 것이다.

+0

우수! 고마워! 더하기 쉽도록 총 7 개의 열이 더 있습니다. 코드 : \t SUM (사례 : [EventName] = 'DiscountView', 그 다음 1 else 0 end) 'Views', \t SUM (사례 : [이벤트 이름] = 'DiscountClick', 1 else 0 end) '클릭' 라운드에서 CTR이 필요합니다 (캐스팅 (클릭 수)/float)/캐스팅 (Views with float) * 100, 2) '평균 CTR' – Nerf

0

사용 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) 
관련 문제