MYSQL日期和车间拼接查询!

跪求各位大佬这样的语句如何拼接,日期时间 按周自动更新的

1648780829(1).jpg

最终查询语句如下

    SELECT

        t1.timeDay as 日期,

        t2.频次,

        t1.车间     

    FROM

        ( select  a.timeDAy,b.车间 from 

(

SELECT 

        date_format(lastWeek.`timeDay`,'%Y-%m-%d') as 'timeDay' 

    FROM (

        select DATE_SUB(NOW(),interval 13 day) as 'timeDay'

        UNION ALL 

        select DATE_SUB(NOW(),interval 12 day) as 'timeDay'

        UNION ALL

        select DATE_SUB(NOW(),interval 11 day) as 'timeDay'

        UNION ALL 

        select DATE_SUB(NOW(),interval 10 day) as 'timeDay'

        UNION ALL 

        select DATE_SUB(NOW(),interval 9 day) as 'timeDay'

        UNION ALL 

        select DATE_SUB(NOW(),interval 8 day) as 'timeDay'

        UNION ALL 

        select DATE_SUB(NOW(),interval 7 day) as 'timeDay'

        UNION ALL 

        select DATE_SUB(NOW(),interval 6 day) as 'timeDay'

        UNION ALL 

        select DATE_SUB(NOW(),interval 5 day) as 'timeDay'

        UNION ALL 

        select DATE_SUB(NOW(),interval 4 day) as 'timeDay'

        UNION ALL 

        select DATE_SUB(NOW(),interval 3 day) as 'timeDay'

        UNION ALL 

        select DATE_SUB(NOW(),interval 2 day) as 'timeDay'

        UNION ALL 

        select DATE_SUB(NOW(),interval 1 day) as 'timeDay'

        ) as lastWeek

        WHERE YEARWEEK(lastWeek.`timeDay`,1) = YEARWEEK(NOW(),1)-1

        ) a,

(SELECT

substring_index(substring_index( a.车间,',',b.help_topic_id + 1 ),',' ,- 1) AS 车间

FROM

(select '111车间,112车间,113车间,211车间,212车间' as 车间) a

JOIN mysql.help_topic b ON b.help_topic_id <

(length(a.车间) - length( replace(a.车间, ',', '') ) + 1)) b       

) as t1  

LEFT JOIN

(

SELECT 日期,SUM(频次) AS 频次,车间,部门,公司

FROM (

    SELECT

        DATE_FORMAT(检验日期,

        '%Y-%m-%d') AS 日期,

        SUM(抽检不合格数量) as 频次,

        车间,

        部门,

        公司          

    FROM

        报检台账           

    WHERE

        YEARWEEK(检验日期,1) = YEARWEEK(NOW(),1)-1          

        AND 车间!='外协车间'          

        AND 公司='${=GETUSERDEPARTMENTS(1)}'          

    GROUP BY

        DATE_FORMAT(检验日期,

        '%Y-%m-%d'),

        车间     

    UNION

    ALL         SELECT

        DATE_FORMAT(反馈日期,

        '%Y-%m-%d') AS 日期,

        SUM(不良数量) as 频次,

        责任车间 AS 车间,

        部门责任 AS 部门,

        责任公司 AS 公司          

    FROM

        反馈台账           

    WHERE

        YEARWEEK(反馈日期,1) = YEARWEEK(NOW(),1)-1          

        AND 责任车间!='外协车间'          

        AND 责任公司='${=GETUSERDEPARTMENTS(1)}'          

    GROUP BY

        DATE_FORMAT(反馈日期,

        '%Y-%m-%d'),

        车间

        ) A

     GROUP BY

     日期,

     车间 

) as t2      

    ON t1.timeDay = t2.日期  AND t1.车间 =t2.车间

ORDER BY

t1.timeDay,t1.车间

FineReport 用户3aCDb8859785 发布于 2022-4-1 10:42 (编辑于 2022-4-1 11:35)
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共1回答
最佳回答
0
CD20160914Lv8专家互助
发布于2022-4-1 10:47(编辑于 2022-4-1 10:50)

select  a.*,b.* from 

(

select '2021-01-01' as date_code 

union all

select '2021-01-02' as date_code) a,

(SELECT

substring_index(substring_index( a.rn,',',b.help_topic_id + 1 ),',' ,- 1) AS rn

FROM

(select '1车间,2车间,3车间' as rn) a

JOIN mysql.help_topic b ON b.help_topic_id <

(length(a.rn) - length( replace(a.rn, ',', '') ) + 1)) b

我只是模拟。。你自己加你的日期表就行了。。

image.png

  • 2关注人数
  • 267浏览人数
  • 最后回答于:2022-4-1 11:35
    请选择关闭问题的原因
    确定 取消
    返回顶部