2

私はこれを機能させようとしてきましたが、何かが私をつまずかせています。ここで SO を検索しましたが、私の特定のケースを説明するトピックは見つかりませんでした。次のスキーマがあります

ここでフィドル: http://www.sqlfiddle.com/#!2/ac8162/6

テーブルrecords:

| ID | CONTRACT |   NAME   | STATUS  |
|----|----------|----------|---------|
|  1 | US       | FOO      | status1 |
|  2 | US       | BAR      | status1 |
|  3 | UK       | ABC Inc. | status3 |
|  4 | PL       | EFG LTD. | status2 |
|  5 | UK       | XXX Inc. | status2 |
|----|----------|----------|---------|

テーブルtransactions:

| ID | RECORD_ID | RESPONSE_DELAY | STATUS  |
|----|-----------|----------------|---------|
|  1 |         1 |           8889 | status1 |
|  2 |         1 |           8813 | status1 |
|  3 |         1 |           5908 | status2 |
|  4 |         1 |           4779 | status3 |
|  5 |         2 |            519 | status1 |
|  6 |         2 |           8804 | status1 |
|  7 |         3 |           2604 | status1 |
|  8 |         3 |           5054 | status2 |
|  9 |         3 |            385 | status3 |
| 10 |         4 |           8852 | status1 |
| 11 |         4 |           4330 | status1 |
| 12 |         4 |           4507 | status1 |
| 13 |         4 |           6282 | status2 |
| 14 |         4 |           5789 | status3 |
| 15 |         4 |           8685 | status4 |
| 16 |         5 |           8339 | status1 |
| 17 |         5 |           9543 | status1 |
| 18 |         1 |           7870 | status1 |
|----|-----------|----------------|---------|

recordsは と 1 対多の関係にありtransactionsます。契約ごとにグループ化されたレポートを取得しようとしています。次のクエリを使用しています。

SELECT 

t0.`contract` as contract,

COUNT(DISTINCT t0.`id`) as rec_num,
COUNT(t1.`id`) as tr_num,

AVG(t1.`response_delay`) as rd_avg,

SUM(CASE WHEN t0.`status`='status1' THEN 1 ELSE 0 END) as s1,
SUM(CASE WHEN t0.`status`='status2' THEN 1 ELSE 0 END) as s2,
SUM(CASE WHEN t0.`status`='status3' THEN 1 ELSE 0 END) as s3

FROM 
records as t0

LEFT JOIN transactions AS t1 
ON
(t0.id = t1.record_id)

GROUP BY contract ORDER BY t0.`id` DESC

結果:

| CONTRACT | REC_NUM | TR_NUM |  RD_AVG   | S1 | S2 | S3 |
|----------|---------|--------|-----------|----|----|----|
| PL       |       1 |      6 | 6407.5    |  0 |  6 |  0 |
| UK       |       2 |      5 | 5185      |  0 |  2 |  3 |
| US       |       2 |      7 | 6511.7143 |  7 |  0 |  0 |
|----------|---------|--------|-----------|----|----|----|

私は契約ごとに各ステ​​ータスタイプのカウントを取得することを目指しており、トランザクションステータスではなくレコードステータスのみをカウントしています。結果は次のとおりです。

| CONTRACT | REC_NUM | TR_NUM |  RD_AVG   | S1 | S2 | S3 |
|----------|---------|--------|-----------|----|----|----|
| PL       |       1 |      6 | 6407.5    |  0 |  1 |  0 |
| UK       |       2 |      5 | 5185      |  0 |  1 |  1 |
| US       |       2 |      7 | 6511.7143 |  2 |  0 |  0 |
|----------|---------|--------|-----------|----|----|----|

どんな助けでも大歓迎です

4

2 に答える 2

5

これを試して:

SELECT 

t0.`contract` as contract,

COUNT(DISTINCT t0.`id`) as rec_num,
COUNT(t1.`id`) as tr_num,

AVG(t1.`response_delay`) as rd_avg,

count(distinct CASE WHEN t0.`status`='status1' THEN t0.id  END) as s1,
count(distinct CASE WHEN t0.`status`='status2' THEN t0.id  END) as s2,
count(distinct CASE WHEN t0.`status`='status3' THEN t0.id  END) as s3


FROM 
records as t0

LEFT JOIN transactions AS t1 
ON
(t0.id = t1.record_id)


GROUP BY contract ORDER BY t0.`id` DESC
于 2013-10-07T05:18:47.400 に答える
0

これにより、探している結果が得られます(おそらくさらに整理できると思います):

SELECT a.`contract`
   , a.`rec_num`
   , b.`tr_num`
   , b.`rd_avg`
   , a.`s1`
   , a.`s2`
   , a.`s3`
FROM
   (SELECT `contract`
     , COUNT(`id`) AS rec_num
     , SUM(CASE WHEN `status`='status1' THEN 1 ELSE 0 END) AS s1
     , SUM(CASE WHEN `status`='status2' THEN 1 ELSE 0 END) AS s2
     , SUM(CASE WHEN `status`='status3' THEN 1 ELSE 0 END) AS s3
   FROM records
   GROUP BY
    contract ORDER BY `id` ASC) AS a
LEFT JOIN 
   (SELECT t0.`contract` AS contract
     , COUNT(t1.`id`) AS tr_num
     , AVG(t1.`response_delay`) AS rd_avg
    FROM
       records AS t0 
    LEFT JOIN
       transactions AS t1 ON (t0.id = t1.record_id)
    GROUP BY t0.`contract`) AS b
ON
   a.`contract`=b.`contract`
于 2013-10-07T05:24:20.810 に答える