MySQL count field values before LEFT JOIN -


i've been trying work, tripping me up. search here on haven't found topic describing specific case. have following schema

fiddle here: http://www.sqlfiddle.com/#!2/ac8162/6

table records:

| id | contract |   name   | status  | |----|----------|----------|---------| |  1 |       | foo      | status1 | |  2 |       | bar      | status1 | |  3 | uk       | abc inc. | status3 | |  4 | pl       | efg ltd. | status2 | |  5 | uk       | xxx inc. | status2 | |----|----------|----------|---------| 

table 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 has 1 many relationship transactions. trying report out of grouped contract. using following query:

select   t0.`contract` contract,  count(distinct t0.`id`) rec_num, count(t1.`id`) tr_num,  avg(t1.`response_delay`) rd_avg,  sum(case when t0.`status`='status1' 1 else 0 end) s1, sum(case when t0.`status`='status2' 1 else 0 end) s2, sum(case when t0.`status`='status3' 1 else 0 end) s3   records t0  left join transactions t1  on (t0.id = t1.record_id)  group contract order t0.`id` desc 

result:

| 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 | |       |       2 |      7 | 6511.7143 |  7 |  0 |  0 | |----------|---------|--------|-----------|----|----|----| 

i aiming count of each status type per contract, counting record status , not transaction status, result after:

| 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 | |       |       2 |      7 | 6511.7143 |  2 |  0 |  0 | |----------|---------|--------|-----------|----|----|----| 

any appreciated

try this:

select   t0.`contract` contract,  count(distinct t0.`id`) rec_num, count(t1.`id`) tr_num,  avg(t1.`response_delay`) rd_avg,  count(distinct case when t0.`status`='status1' t0.id  end) s1, count(distinct case when t0.`status`='status2' t0.id  end) s2, count(distinct case when t0.`status`='status3' t0.id  end) s3    records t0  left join transactions t1  on (t0.id = t1.record_id)   group contract order t0.`id` desc 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -