You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何合并myo与cbp两张表按周分组的注射次数统计结果?

Alright, let's figure out how to combine those weekly injection counts from your cbp and myo tables. Based on what you've already got, there are two common ways to approach this—depending on whether you want to see separate counts for each table per week, or a total combined count.

Option 1: Show separate counts for each table per week

If you want to break out how many injections came from cbp vs myo for each week (including weeks where only one table has data), use a full outer join between your two existing aggregated queries. We'll use COALESCE to turn any NULL values (for weeks where one table has no records) into 0, so you get a clean, complete weekly view:

SELECT
    COALESCE(c.week, m.week) AS week,
    COALESCE(c.count, 0) AS cbp_injections,
    COALESCE(m.count, 0) AS myo_injections
FROM
    (SELECT EXTRACT(week from cbp_date) as week, count(*) AS count
     FROM cbp 
     WHERE EXTRACT(year from cbp_date) = '2018' 
       AND cbp_injected_by = 'ABC' 
     GROUP BY week) c
FULL OUTER JOIN
    (SELECT EXTRACT(week from myo_date) as week, count(*) AS count
     FROM myo 
     WHERE EXTRACT(year from myo_date) = '2018' 
       AND myo_injected_by = 'ABC' 
     GROUP BY week) m
ON c.week = m.week
ORDER BY week ASC;

If your database (like MySQL) doesn't support FULL OUTER JOIN, use this workaround: first gather all unique weeks from both tables, then left join each aggregated query to that list:

WITH all_weeks AS (
    SELECT EXTRACT(week from cbp_date) as week FROM cbp 
    WHERE EXTRACT(year from cbp_date) = '2018' AND cbp_injected_by = 'ABC'
    UNION
    SELECT EXTRACT(week from myo_date) as week FROM myo 
    WHERE EXTRACT(year from myo_date) = '2018' AND myo_injected_by = 'ABC'
)
SELECT
    aw.week,
    COALESCE(c.count, 0) AS cbp_injections,
    COALESCE(m.count, 0) AS myo_injections
FROM all_weeks aw
LEFT JOIN (
    SELECT EXTRACT(week from cbp_date) as week, count(*) AS count
    FROM cbp 
    WHERE EXTRACT(year from cbp_date) = '2018' AND cbp_injected_by = 'ABC'
    GROUP BY week
) c ON aw.week = c.week
LEFT JOIN (
    SELECT EXTRACT(week from myo_date) as week, count(*) AS count
    FROM myo 
    WHERE EXTRACT(year from myo_date) = '2018' AND myo_injected_by = 'ABC'
    GROUP BY week
) m ON aw.week = m.week
ORDER BY aw.week ASC;

Option 2: Get a total combined count per week

If you just need the total number of injections across both tables for each week, use UNION ALL to stack the two aggregated results, then group and sum them together:

SELECT
    week,
    SUM(injection_count) AS total_injections
FROM
    (SELECT EXTRACT(week from cbp_date) as week, count(*) AS injection_count
     FROM cbp 
     WHERE EXTRACT(year from cbp_date) = '2018' 
       AND cbp_injected_by = 'ABC' 
     GROUP BY week
     UNION ALL
     SELECT EXTRACT(week from myo_date) as week, count(*) AS injection_count
     FROM myo 
     WHERE EXTRACT(year from myo_date) = '2018' 
       AND myo_injected_by = 'ABC' 
     GROUP BY week) combined_results
GROUP BY week
ORDER BY week ASC;

Stick with UNION ALL instead of plain UNION here—it preserves all rows from both tables, so we can correctly sum counts for weeks that have data in both cbp and myo.

内容的提问来源于stack exchange,提问作者moadeep

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:27:10