我有2张桌子。一个是工作(主)表,另一个是分配(交易)表。对于每项工作,我需要打印一个月中每天的分配数量。
我需要以以下格式打印分配数量。
我在存储过程中使用while循环尝试了此操作。但这没有帮助。
BEGIN
SET @start_day = DATE('2018-11-01');
SET @end_day = DATE_ADD(@start_day, INTERVAL 30 DAY);
SELECT
job_id into @job_id
FROM
jobs
WHERE
job_post_date BETWEEN @start_day AND @end_day;
WHILE(@start_day < @end_day) DO
SELECT COUNT(allocation_id) FROM allocations WHERE allocations_job_id =
@job_id AND allocations_assigned_date = @start_day;
SET @start_day = DATE_ADD(@start_day, INTERVAL 1 DAY);
END WHILE;
END
答案 0 :(得分:0)
如果您可以通过循环讨论所讨论月份的所有天数来创建动态sql,则应该可以解决您的问题。 动态sql应该看起来像
select j.job_name,
sum(case when a.allocations_assigned_date = '2019-01-01'
then 1 else 0 end) as "1-JAN-2019",
sum(case when a.allocations_assigned_date = '2019-01-02'
then 1 else 0 end) as "2-JAN-2019",
sum(case when a.allocations_assigned_date = '2019-01-31'
then 1 else 0 end) as "31-JAN-2019"
from jobs j inner join allocations a
on j.job_id = a.allocations_job_id
group by j.job_name