2016-09-06 4 views
0

私は2つのサブクエリから結果を減算しようとしていますが、総呼び出しを見たいので1行以上を返したい特定の日付範囲と下のコードの違いは、「サブクエリは1つ以上の値を返しました」というエラーが表示されます。サブクエリは、=、!=、<、< =、>、> =サブクエリは式 "として使用されます。あなたが私にこれを助けることを願っています。これはストアドプロシージャの一部であることに注意してください。前もって感謝します。2行以上を返す2つの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値の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

関連する問題