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