2016-09-06 4 views
0

나는 두 개의 하위 쿼리에서 결과를 빼기 위해 노력하고 있지만 총 호출 수를보고 싶기 때문에 두 개 이상의 행을 반환하려고합니다. 특정 날짜 범위와 이들의 차이점에 대해 "하위 쿼리가 1 개 이상의 값을 반환했습니다. 하위 쿼리가 =,! =, <, < =,>,> = 또는 when 다음에 오는 경우 허용되지 않습니다. 부속 조회는 표현식으로 사용됩니다. 당신이 날 도와 줄 수 있길 바래. 이 저장 프로 시저의 일부임을 유의하십시오. 미리 감사드립니다.두 행을 반환하는 두 SQL 하위 쿼리의 결과를 뺍니다.

SELECT distinct 

c.reporting as 'Agent_ID' 
,count(a.pkey) as 'Total_Calls_Handled' 
,a.MidnightStartDate as 'Call_Start_Time' 
,datename(dw,a.midnightstartdate) as 'Week_day' 
,a.[queue] 

into #temp3 
FROM t1 a 
join t2 b 
on a.FKAgent = b.fkagent 
join t3 c 
on a.agent = c.reporting 
where a.agent in (

    '132568' 
,'116308' 
,'132083' 
,'113737' 

) 

and convert(date, midnightstartdate) BETWEEN '08/29/16' AND '08/30/19' 
group by c.Reporting,a.MidnightStartDate,a.[queue] 


SELECT distinct b.[Week_Day], a.[Queue],[Total ACD Calls], [Total ACD Calls Handled], count([total_calls_handled]) as 'Total ACD Calls Handledby Agent', 

(select ((select [total acd calls handled] from #temp2) - 
(select count([total_calls_handled]) from #temp3))) as 'OperatorsCalls' 

INTO #Temp4 
FROM #Temp2 a 
JOIN #Temp3 b 
ON a.[Queue] = b.[Queue] 
GROUP BY [Total ACD Calls], [Total ACD Calls Handled], b.[Week_Day] , a.[Queue],[total_calls_handled] 
+0

# temp2 테이블에 여러 행의 결과가 포함되어있을 수 있으며 SINGLE 값이있는 단일 레코드 만 반환 될 수있는 컨텍스트에서 select를 사용하고 있습니다. –

+0

예, 여러 행이 있습니다. 위의 코드는 기간이 1 일 동안 만 작동하는 경우 작동합니다. 그러나 나는 전체 주간의 차이를보고 싶다. 내가 할 수있는 방법이 있니? – Emarie

답변

0

작동중인 SQL 바이올린을 사용하지 않고서도 가장 좋은 결과를 얻었습니다. 본질적으로 데이터를로드하기 전에 임시 테이블을 만들고, 계산을 수행 한 다음 (여러 행 형식을 유지할 수 있음) 마지막에 # temp4 테이블에 마지막 삽입에 연결해야합니다.

SELECT distinct 

c.reporting as 'Agent_ID' 
,count(a.pkey) as 'Total_Calls_Handled' 
,a.MidnightStartDate as 'Call_Start_Time' 
,datename(dw,a.midnightstartdate) as 'Week_day' 
,a.[queue] 

into #temp3 
FROM t1 a 
join t2 b 
on a.FKAgent = b.fkagent 
join t3 c 
on a.agent = c.reporting 
where a.agent in (

'132568' 
,'116308' 
,'132083' 
,'113737' 

) 

and convert(date, midnightstartdate) BETWEEN '08/29/16' AND '08/30/19' 
group by c.Reporting,a.MidnightStartDate,a.[queue] 

--new temp table to load temp3 counts 
SELECT 
    Count(total_calls_handled) as total_calls_handled_count, 
    queue 
INTO #Temp3Counts 
FROM 
    #temp3 
GROUP BY queue, total_calls_handled 

-- new temp table to handled subquery calculations 
SELECT 
    T2.Queue, 
    (T2.[Total acd calls handled]-T3.total_calls_handled_count) as  'OperatorsCalls' 
INTO #temp2Calc 
FROM 
    #temp2 as T2 
INNER JOIN 
#temp3Counts as T3 
ON T2.Queue = T3.Queue 

GROUP BY T2.Queue,T2.[Total acd calls handled],T3.total_calls_handled_count 



SELECT distinct b.[Week_Day], a.[Queue],[Total ACD Calls], [Total ACD Calls  Handled], count([total_calls_handled]) as 'Total ACD Calls Handledby Agent',  T2C.OperatorsCalls 

INTO #Temp4 
FROM #Temp2 a 
JOIN #Temp3 b 
ON a.[Queue] = b.[Queue] 
INNER JOIN 
    #Temp2Calc as T2C 
ON T2C.queue = a.queue 
GROUP BY b.[Week_Day], a.queue,[Total ACD Calls],[Total ACD Calls Handled], [total_calls_handled],T2C.OperatorsCalls 
+0

감사합니다. 그것은 효과가 있었다. – Emarie

+0

대답으로 표시 할 수 있다면 잠재적으로 사람들의 향후 검색을 도울 수 있습니다. – Zi0n1

+0

방금했습니다. 나를 상기시켜 줘서 고마워. :) – Emarie

관련 문제