str_to_date(date_format(sysdate(),'%​​d-%b-%Y'),'%d-%b-%Y')-max(ren_date)> = 7附近的语法错误

时间:2018-07-09 05:15:30

标签: mysql

CREATE OR REPLACE  VIEW HWP_LASTRENEWED (REGNO, WPNO, PERMITSTATUS, PROCESSSTATUS, LASTRENDATE, DAYS, HTYPE, SRNO) AS 
  (
select regno, wpno, permitstatus, processstatus, max(ren_date) as lastrendate,
str_to_date(date_format(sysdate(),'%d-%b-%Y'),'%d-%b-%Y')-max(ren_date) days , 'renw' as htype, '0' as srno
from hwp_renewal
having permitstatus='RENEWED'
and str_to_date(date_format(sysdate(),'%d-%b-%Y'),'%d-%b-%Y')-max(ren_date)>=7
group by wpno, permitstatus, regno, processstatus
union
select reg_no as regno, wp_no as wpno, permitstatus, processstatus, permitdate as lastrendate,
(str_to_date(date_format(sysdate(),'%d-%b-%Y'),'%d-%b-%Y')-permitdate) as days, 'req' as htype, srno
from hwp_hotwork where wp_no is null and reg_no is null and
( (permitstatus='REQUESTED' and processstatus='REQ') or (permitstatus='APPROVED' and processstatus='APPR') )
and str_to_date(date_format(sysdate(),'%d-%b-%Y'),'%d-%b-%Y')-permitdate >=7
);

什么是正确的语法?

3 个答案:

答案 0 :(得分:1)

  1. HAVING子句必须位于GROUP BY之后。就这么简单。
  2. 您在并集选择中缺少括号。

CREATE OR REPLACE VIEW HWP_LASTRENEWED ( REGNO, WPNO, PERMITSTATUS, PROCESSSTATUS, LASTRENDATE, DAYS, HTYPE, SRNO ) AS ( select regno, wpno, permitstatus, processstatus, max(ren_date) as lastrendate, str_to_date(date_format(sysdate(), '%d-%b-%Y'), '%d-%b-%Y') - max(ren_date) as days, 'renw' as htype, '0' as srno from hwp_renewal group by wpno, permitstatus, regno, processstatus having permitstatus = 'RENEWED' and days >= 7 ) union ( select reg_no as regno, wp_no as wpno, permitstatus, processstatus, permitdate as lastrendate, str_to_date(date_format(sysdate(), '%d-%b-%Y'), '%d-%b-%Y') - permitdate as days, 'req' as htype, srno from hwp_hotwork where wp_no is null and reg_no is null and ( ( permitstatus = 'REQUESTED' and processstatus = 'REQ' ) or ( permitstatus = 'APPROVED' and processstatus = 'APPR' ) ) and days >= 7 )

答案 1 :(得分:0)

我不知道您为什么将SYSDATE()转换为字符串,然后再次转换为日期。而是直接将其与DATEDIFF一起使用:

CREATE OR REPLACE VIEW HWP_LASTRENEWED AS (
    SELECT
        regno,
        wpno,
        permitstatus,
        processstatus,
        MAX(ren_date) AS lastrendate,
        DATEDIFF(MAX(ren_date), SYSDATE()) days,
        'renw' AS htype,
        '0' AS srno
    FROM hwp_renewal
    WHERE permitstatus = 'RENEWED'
    GROUP BY wpno, permitstatus, regno, processstatus
    HAVING DATEDIFF(SYSDATE(), MAX(ren_date)) >= 7

    UNION

    SELECT
        reg_no AS regno,
        wp_no as wpno,
        permitstatus,
        processstatus,
        permitdate AS lastrendate,
        DATEDIFF(SYSDATE(), permitdate) AS days
        'req' as htype,
        srno
    FROM hwp_hotwork
    WHERE wp_no IS NULL AND reg_no IS NULL AND
        ((permitstatus = 'REQUESTED' AND processstatus = 'REQ') OR
         (permitstatus = 'APPROVED' AND processstatus = 'APPR')) AND
        DATEDIFF(SYSDATE(), permitdate) >= 7
);

答案 2 :(得分:0)

顺序
1) FROM&JOINs 确定并过滤行
2)在行上在哪里更多过滤器
3) GROUP BY 将这些行组合为组
4)拥有过滤器组
5) ORDER BY 排列剩余的行/组
6) LIMIT 过滤剩余的行/组

HAVING子句在GROUP BY子句之后使用 修改后的查询

CREATE OR REPLACE  VIEW HWP_LASTRENEWED (REGNO, WPNO, PERMITSTATUS, PROCESSSTATUS, LASTRENDATE, DAYS, HTYPE, SRNO) AS 
  (
select regno, wpno, permitstatus, processstatus, max(ren_date) as lastrendate,
str_to_date(date_format(sysdate(),'%d-%b-%Y'),'%d-%b-%Y')-max(ren_date) days , 'renw' as htype, '0' as srno
from hwp_renewal
group by wpno, permitstatus, regno, processstatus
having permitstatus='RENEWED'
and str_to_date(date_format(sysdate(),'%d-%b-%Y'),'%d-%b-%Y')-max(ren_date)>=7
union
select reg_no as regno, wp_no as wpno, permitstatus, processstatus, permitdate as lastrendate,
(str_to_date(date_format(sysdate(),'%d-%b-%Y'),'%d-%b-%Y')-permitdate) as days, 'req' as htype, srno
from hwp_hotwork where wp_no is null and reg_no is null and
( (permitstatus='REQUESTED' and processstatus='REQ') or (permitstatus='APPROVED' and processstatus='APPR') )
and str_to_date(date_format(sysdate(),'%d-%b-%Y'),'%d-%b-%Y')-permitdate >=7
);