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
);
什么是正确的语法?
答案 0 :(得分:1)
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) 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
);