我原来的查询是这样的,它获取总计记录,然后按特定会计年度的相应年份和月份进行分组。
`SELECT s.field_source_target_id AS field_source_target_id, tfd.name AS name, tfdc.name AS tfdc_name,
tfdr.name AS tfdr_name, max(nfcd.field_creation_date_value) AS Monat,
COUNT(DISTINCT nfd.nid) AS IntpM, SUM(case WHEN doing.field_doings_target_id in(26) then 1 else 0 end) AS IGVB, SUM(case WHEN doing.field_doings_target_id in(27) then 1 else 0 end) AS IGDG, SUM(case WHEN doing.field_doings_target_id in(30) then 1 else 0 end) AS PTVB,SUM(case WHEN doing.field_doings_target_id in(31) then 1 else 0 end) AS PTDG,SUM(case WHEN doing.field_doings_target_id in(28) then 1 else 0 end) AS VGVB, SUM(case WHEN doing.field_doings_target_id in(29) then 1 else 0 end) AS VGDG,
COUNT(DISTINCT case WHEN st.field_status_target_id in(33,34,35) then nfd.nid else null end) AS Onboard
FROM
node_field_data nfd
INNER JOIN node__field_region r ON r.entity_id = nfd.nid
INNER JOIN node__field_source s ON s.entity_id = nfd.nid
left JOIN node__field_creation_date nfcd ON nfcd.entity_id = nfd.nid
INNER JOIN taxonomy_term_field_data tfd ON tfd.tid = s.field_source_target_id
INNER JOIN node__field_status st ON st.entity_id = nfd.nid
LEFT OUTER JOIN node__field_onboard_vpkn vpkn ON vpkn.entity_id = nfd.nid
INNER JOIN node__field_channel nfc ON nfc.entity_id = nfd.nid
INNER JOIN taxonomy_term_field_data tfdr ON tfdr.tid = r.field_region_target_id
INNER JOIN taxonomy_term_field_data tfdc ON tfdc.tid = nfc.field_channel_target_id
LEFT OUTER JOIN node__field_doings doing ON doing.entity_id = nfd.nid
WHERE
(type = 'candidate') AND (status = 1)
GROUP BY tfd.name, tfdc.name, tfdr.name, r.field_region_target_id, s.field_source_target_id,
YEAR(nfcd.field_creation_date_value), MONTH(nfcd.field_creation_date_value)
ORDER BY s.field_source_target_id ASC,tfdr_name ASC`
结果看起来像
83,SEA,KMU,R1,2018-08-07,1
83,SEA,KMU,R2,2018-05-23,2
83,SEA,KMU,R2,2018-06-20,1
83,SEA,KMU,R2,2018-07-17,2
现在,我想查询每个通道和每个区域每个月每个节点在node__field_creation_date表中与其他表结合的记录。到目前为止我尝试过的
SELECT allmonths.m,nfcd.field_creation_date_value,MONTH(nfcd.field_creation_date_value) as month
,s.field_source_target_id AS field_source_target_id, tfd.name AS name, tfdc.name AS tfdc_name,
tfdr.name AS tfdr_name, max(nfcd.field_creation_date_value) AS Monat,
COUNT(DISTINCT nfd.nid) AS IntpM, SUM(case WHEN doing.field_doings_target_id in(26) then 1 else 0 end) AS IGVB, SUM(case WHEN doing.field_doings_target_id in(27) then 1 else 0 end) AS IGDG, SUM(case WHEN doing.field_doings_target_id in(30) then 1 else 0 end) AS PTVB,SUM(case WHEN doing.field_doings_target_id in(31) then 1 else 0 end) AS PTDG,SUM(case WHEN doing.field_doings_target_id in(28) then 1 else 0 end) AS VGVB, SUM(case WHEN doing.field_doings_target_id in(29) then 1 else 0 end) AS VGDG,
COUNT(DISTINCT case WHEN st.field_status_target_id in(33,34,35) then nfd.nid else null end) AS Onboard
FROM (
SELECT 01 as m UNION SELECT 2 as m UNION SELECT 03 as m UNION SELECT 04 as m UNION SELECT 5 as m UNION SELECT 6 as m
UNION SELECT 7 as m UNION SELECT 8 as m UNION SELECT 09 as m UNION SELECT 10 as m UNION SELECT 11 as m UNION SELECT 12 as m
) as allmonths
left JOIN node__field_creation_date nfcd ON MONTH(nfcd.field_creation_date_value) = allmonths.m
LEFT JOIN node_field_data nfd ON nfd.nid = nfcd.entity_id
INNER JOIN node__field_region r ON r.entity_id = nfd.nid
INNER JOIN node__field_source s ON s.entity_id = nfd.nid
INNER JOIN taxonomy_term_field_data tfd ON tfd.tid = s.field_source_target_id
INNER JOIN node__field_status st ON st.entity_id = nfd.nid
LEFT OUTER JOIN node__field_onboard_vpkn vpkn ON vpkn.entity_id = nfd.nid
INNER JOIN node__field_channel nfc ON nfc.entity_id = nfd.nid
INNER JOIN taxonomy_term_field_data tfdr ON tfdr.tid = r.field_region_target_id
INNER JOIN taxonomy_term_field_data tfdc ON tfdc.tid = nfc.field_channel_target_id
LEFT OUTER JOIN node__field_doings doing ON doing.entity_id = nfd.nid
WHERE
(nfcd.field_creation_date_value BETWEEN '2018-04-01' AND '2019-03-31') AND
(type = 'candidate') AND (status = 1)
GROUP BY tfd.name, tfdc.name, tfdr.name, r.field_region_target_id, s.field_source_target_id,
allmonths.m
# ,YEAR(nfcd.field_creation_date_value), MONTH(nfcd.field_creation_date_value)
ORDER BY s.field_source_target_id ASC
,tfdc_name,tfdr_name,Monat ASC;
我想要类似
83,SEA,KMU,R1,2018-08-07,1
83,SEA,KMU,R2,2018-05-23,2
83,SEA,KMU,R2,2018-04-23,0 // missing month with 0 records
83,SEA,KMU,R2,2018-06-20,1
83,SEA,KMU,R2,2018-07-17,2
我得到的结果与上面的查询相同。有人想帮助我吗?
答案 0 :(得分:0)
将(nfcd.field_creation_date_value BETWEEN '2018-04-01' AND '2019-03-31')
从您的WHERE
子句转移到左联接条件(ON
子句)。
由于此条件位于何处,因此忽略了nfcd.field_creation_date_value
为null
的情况;因此您不会在缺少月份的情况下获得结果。
请执行以下操作:
SELECT allmonths.m,nfcd.field_creation_date_value,MONTH(nfcd.field_creation_date_value) as month
,s.field_source_target_id AS field_source_target_id, tfd.name AS name, tfdc.name AS tfdc_name,
tfdr.name AS tfdr_name, max(nfcd.field_creation_date_value) AS Monat,
COUNT(DISTINCT nfd.nid) AS IntpM, SUM(case WHEN doing.field_doings_target_id in(26) then 1 else 0 end) AS IGVB, SUM(case WHEN doing.field_doings_target_id in(27) then 1 else 0 end) AS IGDG, SUM(case WHEN doing.field_doings_target_id in(30) then 1 else 0 end) AS PTVB,SUM(case WHEN doing.field_doings_target_id in(31) then 1 else 0 end) AS PTDG,SUM(case WHEN doing.field_doings_target_id in(28) then 1 else 0 end) AS VGVB, SUM(case WHEN doing.field_doings_target_id in(29) then 1 else 0 end) AS VGDG,
COUNT(DISTINCT case WHEN st.field_status_target_id in(33,34,35) then nfd.nid else null end) AS Onboard
FROM (
SELECT 01 as m UNION SELECT 2 as m UNION SELECT 03 as m UNION SELECT 04 as m UNION SELECT 5 as m UNION SELECT 6 as m
UNION SELECT 7 as m UNION SELECT 8 as m UNION SELECT 09 as m UNION SELECT 10 as m UNION SELECT 11 as m UNION SELECT 12 as m
) as allmonths
left JOIN node__field_creation_date nfcd ON MONTH(nfcd.field_creation_date_value) = allmonths.m AND (nfcd.field_creation_date_value BETWEEN '2018-04-01' AND '2019-03-31')
LEFT JOIN node_field_data nfd ON nfd.nid = nfcd.entity_id
INNER JOIN node__field_region r ON r.entity_id = nfd.nid
INNER JOIN node__field_source s ON s.entity_id = nfd.nid
INNER JOIN taxonomy_term_field_data tfd ON tfd.tid = s.field_source_target_id
INNER JOIN node__field_status st ON st.entity_id = nfd.nid
LEFT OUTER JOIN node__field_onboard_vpkn vpkn ON vpkn.entity_id = nfd.nid
INNER JOIN node__field_channel nfc ON nfc.entity_id = nfd.nid
INNER JOIN taxonomy_term_field_data tfdr ON tfdr.tid = r.field_region_target_id
INNER JOIN taxonomy_term_field_data tfdc ON tfdc.tid = nfc.field_channel_target_id
LEFT OUTER JOIN node__field_doings doing ON doing.entity_id = nfd.nid
WHERE
(type = 'candidate') AND (status = 1)
GROUP BY tfd.name, tfdc.name, tfdr.name, r.field_region_target_id, s.field_source_target_id,
allmonths.m
# ,YEAR(nfcd.field_creation_date_value), MONTH(nfcd.field_creation_date_value)
ORDER BY s.field_source_target_id ASC
,tfdc_name,tfdr_name,Monat ASC;