0

以下のSQLステートメントがあります。列のラップアップ コードには、最大 4 つの値を指定できます。

営業、サービス、会議、その他

SQL を実行すると、列のラップアップ コードが 1 つの列として表示されます。私が解決したいのは、値に基づいて新しいラップアップコードを動的に作成することです。コードを追加または削除する場合があります。それ以外の場合は、値をケースします。

これは、MySQL データベースに基づいています。

データは現在次のようになっています。

user  wrapupcode  intialtime  endofwrapup  timediff
joe   Service     11:38       11:39        1
jenny Sales       11:35       11:36        1
joe   Service     11:41       11:42        1

しかし、MySQL データベース上の SQL 経由で可能かどうかを確認したいと思います:

user  Service  Sales     timediff
joe   2                  2
jenny          1         1

時間と合計の合計/平均を実行できます。異なるラップアップコードごとに新しい列を追加するだけです。

SELECT
     ( SELECT loginid FROM `axpuser` WHERE age.userid = axpuser.pkey ) as user,
     ( SELECT name FROM `breakcode` WHERE age.wrapupcode = pkey ) as wrapupcode,
     time(age.`instime`) as initialtime,
     age.`ENDOFWRAPUPTIME` AS endofwrapup,
     timediff(age.`ENDOFWRAPUPTIME`, time(age.`instime`)) as timediff
  FROM
     agentcallinformation age
  WHERE
     age.endofwrapuptime IS NOT null and ( SELECT name FROM `breakcode` WHERE age.wrapupcode =   pkey ) <> ''
4

1 に答える 1

2

基本的な構文は次のとおりです。

select user,
    sum(case when wrapupcode = 'Service' then 1 else 0 end) Service,
    sum(case when wrapupcode = 'Sales' then 1 else 0 end) Sales,
    sum(case when wrapupcode = 'Meeting' then 1 else 0 end) Meeting,
    sum(case when wrapupcode = 'Other' then 1 else 0 end) Other,
    count(timediff) timediff
from
(       
    <yourquery>
) src
group by user

ハードコーディングされた静的バージョンは、次のようになります。

select user,
    sum(case when wrapupcode = 'Service' then 1 else 0 end) Service,
    sum(case when wrapupcode = 'Sales' then 1 else 0 end) Sales,
    sum(case when wrapupcode = 'Meeting' then 1 else 0 end) Meeting,
    sum(case when wrapupcode = 'Other' then 1 else 0 end) Other,
    count(timediff) timediff
from
(       
    select u.loginid as user,
        b.name wrapupcode,
        time(age.`instime`) as initialtime,
        age.`ENDOFWRAPUPTIME` AS endofwrapup,
        count(timediff(age.`ENDOFWRAPUPTIME`,   time(age.`instime`))) as timediff
    from agentcallinformation age
    left join `axpuser` u
        on age.userid = u.pkey
    left join `breakcode` b
        on age.wrapupcode = b.pkey
        and age.wrapupcode <> ''
    WHERE age.endofwrapuptime IS NOT null 
) src
group by user

JOIN相関サブクエリの代わりに構文を使用するようにクエリを変更しました。

動的バージョンが必要な場合は、準備済みステートメントを使用できます。

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'sum(case when wrapupcode = ''',
      name,
      ''' then 1 else 0 end) AS ',
      name
    )
  ) INTO @sql
FROM breakcode;

SET @sql = CONCAT('SELECT user, ', @sql, ' 
                    , count(timediff) timediff
                  from
                  (     
                    select u.loginid as user,
                        b.name wrapupcode,
                        time(age.`instime`) as initialtime,
                        age.`ENDOFWRAPUPTIME` AS endofwrapup,
                        count(timediff(age.`ENDOFWRAPUPTIME`,   time(age.`instime`))) as timediff
                    from agentcallinformation age
                    left join `axpuser` u
                        on age.userid = u.pkey
                    left join `breakcode` b
                        on age.wrapupcode = b.pkey
                        and age.wrapupcode <> ''
                    WHERE age.endofwrapuptime IS NOT null 
                ) src
                GROUP BY user');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
于 2012-10-31T16:09:33.717 に答える