0

我目前正在为我们的一位客户撰写报告。该报告可以很好地报告每个课程每个区域的用户数量,但是客户还希望显示具有零用户结果的课程。下面是一个例子:

SELECT
lc.code as course_code, count(rc1.region_id) as sc1data, count(rc2.region_id) as
sc2data, count(rc3.region_id) as sc3data, count(rc4.region_id) as sc4data,
count(rc5.region_id) as sc5data, count(rc6.region_id) as sc6data,
count(rc7.region_id) as sc7data, count(rc8.region_id) as sc8data,
count(rc9.region_id) as sc9data, count(rc10.region_id) as sc10data,
count(rc11.region_id) as sc11data, count(rc12.region_id) as sc_total_data

FROM learning_polltrack lpt
LEFT JOIN learning_organization lo ON lo.idOrg = lpt.id_reference
LEFT JOIN learning_course lc ON lc.idCourse = lo.idCourse
LEFT JOIN core_field_userentry cfue ON lpt.id_user = cfue.id_user AND cfue.id_common IN (26)
LEFT JOIN core_field cf ON cf.idField = cfue.id_common
LEFT JOIN core_field_son cfs ON cfs.idField = cfue.id_common AND cfue.user_entry = cfs.idSon
LEFT JOIN provider_type_by_title ptbt ON cfs.translation = ptbt.title
LEFT JOIN core_field_userentry cfue2 ON cfue.id_user = cfue2.id_user AND cfue2.id_common = '15'
LEFT JOIN core_field_son cfs2 ON cfs2.idField = '15' AND cfs2.id_common_son = cfue2.user_entry
LEFT JOIN region_by_county rc1 ON rc1.county = cfs2.translation AND rc1.region_id = 1
LEFT JOIN region_by_county rc2 ON rc2.county = cfs2.translation AND rc2.region_id = 2
LEFT JOIN region_by_county rc3 ON rc3.county = cfs2.translation AND rc3.region_id = 3
LEFT JOIN region_by_county rc4 ON rc4.county = cfs2.translation AND rc4.region_id = 4
LEFT JOIN region_by_county rc5 ON rc5.county = cfs2.translation AND rc5.region_id = 5
LEFT JOIN region_by_county rc6 ON rc6.county = cfs2.translation AND rc6.region_id = 6
LEFT JOIN region_by_county rc7 ON rc7.county = cfs2.translation AND rc7.region_id = 7
LEFT JOIN region_by_county rc8 ON rc8.county = cfs2.translation AND rc8.region_id = 8
LEFT JOIN region_by_county rc9 ON rc9.county = cfs2.translation AND rc9.region_id = 9
LEFT JOIN region_by_county rc10 ON rc10.county = cfs2.translation AND rc10.region_id = 10
LEFT JOIN region_by_county rc11 ON rc11.county = cfs2.translation AND rc11.region_id = 11
LEFT JOIN region_by_county rc12 ON rc12.county = cfs2.translation
WHERE ptbt.provider_type = $P{provider_type} AND lc.code NOT LIKE '6 Month%' AND lc.code NOT LIKE 'F2F%' AND lc.code NOT LIKE 'Beta%' AND lpt.date_attempt BETWEEN $P{From} AND $P{To}

GROUP BY course_code ORDER BY course_code

那是我的代码,结果如下所示:

course_code | region 1 | region 2 | region 3 | ...... | region 11 | TOTAL
course 1    |     5    |     0    |    1     |        |     1     |   7
course 2    |     2    |     1    |    0     |        |     1     |   4
course 4    |     3    |     0    |    1     |        |     0     |   4
course 8    |     1    |     0    |    0     |        |     0     |   1

但我需要它看起来像:

course_code | region 1 | region 2 | region 3 | ...... | region 11 | TOTAL
course 1    |     5    |     0    |    1     |        |     1     |   7
course 2    |     2    |     1    |    0     |        |     1     |   4
course 3    |     0    |     0    |    0     |        |     0     |   0
course 4    |     3    |     0    |    1     |        |     0     |   4
course 5    |     0    |     0    |    0     |        |     0     |   0
course 6    |     0    |     0    |    0     |        |     0     |   0
course 7    |     0    |     0    |    0     |        |     0     |   0
course 8    |     1    |     0    |    0     |        |     0     |   1
4

2 回答 2

1

如果您想要所有课程,则从left outer join课程列表开始链。我认为这是具有别名的表lc。而不是这个:

FROM learning_polltrack lpt
LEFT JOIN learning_organization lo ON lo.idOrg = lpt.id_reference
LEFT JOIN learning_course lc ON lc.idCourse = lo.idCourse

尝试这个:

from learning_course lc left outer join
     learning_organizatino lo
     on lc.idCourse = lo.idCourse left outer join
     learning_polltrack lpt
     on lo.idOrg = lpt.id_reference

等等。我最好的猜测是没有用户的课程没有“polltrack”(无论是什么;-)。因此,它们最终会从整体连接结果中丢失。

另外,我希望您明白,当您在子句中放入任何表格时,where它可能会将.left joininner join

于 2013-05-07T20:07:16.710 回答
0

计算与主表连接的子查询中的所有行。sum(rc.region_id = N)您可以在主查询中使用旋转区域 ID 。

SELECT
    lc.code as course_code,
    ifnull(sum(rc.region_id = 1), 0) sc1data,
    ifnull(sum(rc.region_id = 2), 0) sc2data,
    ...,
    ifnull(sc_total_data, 0) sc_total_data
FROM learning_polltrack lpt
LEFT JOIN learning_organization lo ON lo.idOrg = lpt.id_reference
LEFT JOIN learning_course lc ON lc.idCourse = lo.idCourse
LEFT JOIN core_field_userentry cfue ON lpt.id_user = cfue.id_user AND cfue.id_common IN (26)
LEFT JOIN core_field cf ON cf.idField = cfue.id_common
LEFT JOIN core_field_son cfs ON cfs.idField = cfue.id_common AND cfue.user_entry = cfs.idSon
LEFT JOIN provider_type_by_title ptbt ON cfs.translation = ptbt.title
LEFT JOIN core_field_userentry cfue2 ON cfue.id_user = cfue2.id_user AND cfue2.id_common = '15'
LEFT JOIN core_field_son cfs2 ON cfs2.idField = '15' AND cfs2.id_common_son = cfue2.user_entry
LEFT JOIN region_by_county rc on rc.county = cfs2.translation
LEFT JOIN (select county, count(*) sc_total_data from region_by_county group by region) rcall on rcall.county = cfs2.translation
WHERE ptbt.provider_type = $P{provider_type} AND lc.code NOT LIKE '6 Month%' AND lc.code NOT LIKE 'F2F%' AND lc.code NOT LIKE 'Beta%' AND lpt.date_attempt BETWEEN $P{From} AND $P{To}
GROUP BY course_code ORDER BY course_code
于 2013-05-07T18:56:06.703 回答