2017-10-30 17 views
0

私は以下の作業クエリを持っていますが、私のselectステートメントでこれらのエージェントの姓と名を選択する必要があります。結合なしで別のテーブルから選択する

他のテーブルはambition.ambition_usersで、first_name、last_name、およびextensionがあります。サブクエリを正しく引き出して、idとして扱います。しかし、私の主な選択では、first_nameとlast_nameも選択したいと思います。私は単純なサブクエリでこれを行う方法を見つけることができないようで、私が作成したidに参加することができない限り、テーブルに参加することはありません。ambition_users.extention

現在のクエリの集計に影響を与えずに姓と名義を引き出す最良の方法は何ですか?

はここで働いてクエリです:

select 
case 
when callingpartyno  in (select extension from ambition.ambition_users) 
then callingpartyno 
when finallycalledpartyno in (select extension from ambition.ambition_users) 
then finallycalledpartyno 
end as id 

-- this is where i want to select first_name and last_name from ambition.ambition_users 

, sum(duration) as total_talk_time_seconds 
, round(sum(duration)/60,2) as total_talk_time_minutes 
, sum(if(legtype1 = 1,1,0)) as total_outbound 
, sum(if(legtype1 = 2,1,0) and answered = 1) as total_inbound 
, sum(if(legtype1 = 2,1,0) and answered = 0) as total_missed 
, sum(if(legtype1 = 1, 1, 0)) +     -- outbound calls 
sum(if(legtype1 = 2, 1, 0)) as total_calls 
, now() as time_of_report 
, curdate() as date_of_report 
from 
    ambition.session a 
    join ambition.callsummary b 
    on a.notablecallid = b.notablecallid 
where 
date(a.ts) >= curdate() 
and (
callingpartyno in (select extension from ambition.ambition_users 
) 
or finallycalledpartyno in (select extension from ambition.ambition_users 
) 
) 
group by 
id; 
+0

「mytableから選択したくありませんか? – kabanus

+0

よく電話番号を集めたコール情報が含まれているため、メインテーブルから選択して別のテーブルに参加しています。これはすべて正常に動作しています。ただし、エージェントの名前を拡張子に基づいて選択したいだけです –

+0

以前の質問を知りたいCASE式を書き換えて結合に使用します。 –

答えて

1

あなたは非常に多くのサブクエリが必要な理由。左結合を使用して、他のすべてのサブクエリを取り出すことができます。

select 
    COALESCE(callingpartyno, finallycalledpartyno) as id 
    -- if extension is not equal to callingpartyno, it will return null because its a left join. If you use coalesce you can get the first non null value. 

    , max(c.firstname) 
    , max(c.lastname) 
    -- if you use max you need not put that in group by clause 

    , sum(duration) as total_talk_time_seconds 
    , round(sum(duration)/60,2) as total_talk_time_minutes 
    , sum(if(legtype1 = 1,1,0)) as total_outbound 
    , sum(if(legtype1 = 2,1,0) and answered = 1) as total_inbound 
    , sum(if(legtype1 = 2,1,0) and answered = 0) as total_missed 
    , sum(if(legtype1 = 1, 1, 0)) +     -- outbound calls 
    sum(if(legtype1 = 2, 1, 0)) as total_calls 
    , now() as time_of_report 
    , curdate() as date_of_report 
    from 
     ambition.session a 
     join ambition.callsummary b 
     on a.notablecallid = b.notablecallid 
---- adding a left join 
    left join 
    ambition.users c 
    on c.extension = callingpartyno or c.extension = finallycalledpartyno 
    where 
    date(a.ts) >= curdate() 
    group by id; 
+0

申し訳ありませんが、これはまだ少し不完全ですか? –

+0

@TomN。それは正しくフォーマットされていません..その完全な – Valli

1

私はあなたのテーブル間の関係を取得していないが、あなたはselect文の中にサブクエリで最初と最後の名前を取得するためのロジックを下回る

select 
    case 
    when callingpartyno  in (select extension from ambition.ambition_users) 
    then callingpartyno 
    when finallycalledpartyno in (select extension from ambition.ambition_users) 
    then finallycalledpartyno 
    end as id 

    -- this is where i want to select first_name and last_name from ambition.ambition_users 

Max((select firstname from ambition.ambition_users as t1 where t1. Extension=b.callingpartyno)), 
Max((select lastname from ambition.ambition_users as t1 where t1. Extension=b.callingpartyno)), 

    , sum(duration) as total_talk_time_seconds 
    , round(sum(duration)/60,2) as total_talk_time_minutes 
    , sum(if(legtype1 = 1,1,0)) as total_outbound 
    , sum(if(legtype1 = 2,1,0) and answered = 1) as total_inbound 
    , sum(if(legtype1 = 2,1,0) and answered = 0) as total_missed 
    , sum(if(legtype1 = 1, 1, 0)) +     -- outbound calls 
    sum(if(legtype1 = 2, 1, 0)) as total_calls 
    , now() as time_of_report 
    , curdate() as date_of_report 
    from 
     ambition.session a 
     join ambition.callsummary b 
     on a.notablecallid = b.notablecallid 
    where 
    date(a.ts) >= curdate() 
    and (
    callingpartyno in (select extension from ambition.ambition_users 
    ) 
    or finallycalledpartyno in (select extension from ambition.ambition_users 
    ) 
    ) 
    group by 
    id; 
+0

これは、最初と最後の3名がnullを返し、残りの7名が正しく返されたことを除いて、動作するようです。あなたはその理由を知っていますか? MAXなしでそれを行う方法はありますか? –

+0

サブクエリ内に 'or'条件を入れてみてください。where句はうまくいきます。他のカラムでグループ化しているので、maxはそこにあるはずです。 – Rams

+0

実際はサブクエリ –

0
を使用することができます

もう一度同じサブクエリを使用するのではなく、&を再度使用して、サブクエリの結果を変数に格納することをお勧めします。

また、あなたはcallingpartynofinallycalledpartynoのためのさまざまなクエリを作成し、後で次のようにUNIONで結果をマージすることができます

select @ext := Group_concat(distinct extension separator ',') from ambition.ambition_users; 

select tmp.*, 
(select firstn from ambition.ambition_users where tmp.id = extension) as First_Name, 
(select lastn from ambition.ambition_users where tmp.id = extension) as Last_Name 
From 
(select 
callingpartyno as id 
, sum(duration) as total_talk_time_seconds 
, round(sum(duration)/60,2) as total_talk_time_minutes 
, sum(if(legtype1 = 1,1,0)) as total_outbound 
, sum(if(legtype1 = 2,1,0) and answered = 1) as total_inbound 
, sum(if(legtype1 = 2,1,0) and answered = 0) as total_missed 
, sum(if(legtype1 = 1, 1, 0)) +     -- outbound calls 
sum(if(legtype1 = 2, 1, 0)) as total_calls 
, now() as time_of_report 
, curdate() as date_of_report 
from 
    ambition.session a 
    join ambition.callsummary b 
    on a.notablecallid = b.notablecallid 
where 
date(a.ts) >= curdate() 
and 
callingpartyno in (@ext) 
group by callingpartyno) 

     UNION 

(select 
finallycalledpartyno as id 
, sum(duration) as total_talk_time_seconds 
, round(sum(duration)/60,2) as total_talk_time_minutes 
, sum(if(legtype1 = 1,1,0)) as total_outbound 
, sum(if(legtype1 = 2,1,0) and answered = 1) as total_inbound 
, sum(if(legtype1 = 2,1,0) and answered = 0) as total_missed 
, sum(if(legtype1 = 1, 1, 0)) +     -- outbound calls 
sum(if(legtype1 = 2, 1, 0)) as total_calls 
, now() as time_of_report 
, curdate() as date_of_report 
from 
    ambition.session a 
    join ambition.callsummary b 
    on a.notablecallid = b.notablecallid 
where 
date(a.ts) >= curdate() 
and 
finallycalledpartyno in (@ext) 
group by finallycalledpartyno) 
) tmp; 
関連する問題