我有一个用Oracle编写的查询,想转换成MySQL

时间:2016-07-21 22:12:11

标签: mysql oracle

select * from 
(
select week_of, COUNT(DISTINCT CUSTOMER_ID) AS weekly_developer_count,        COUNT(DISTINCT APPLICATION_ID) as weekly_app_count from
(
select PE.EVENT_DAY_UTC, to_char(next_day(PE.EVENT_DAY_UTC - 1, 'SUNDAY'),   'YYYY-MM-DD') as week_of, PE.CUSTOMER_ID, PE.APPLICATION_ID
  from tablename PE
  where 
  PE.EVENT_DAY_UTC >= SYSDATE - 90
  and PE.EVENT_DAY_UTC < SYSDATE + 1
  and PE.EVENT_NAME NOT LIKE '%.%'
)
group by  week_of
)
order by week_of desc;

这是我在Oracle中拥有的代码,MySQL不支持NEXT_DAY。有没有想过要实现这个目标? 我希望输出像

Weekof ApplicationCount DeveloperCount
2016-17-07  50              10
2016-10-07  60              15

3 个答案:

答案 0 :(得分:0)

您可以实现自己的功能,如下所述:

MySQL implementation of Oracle's next_day

答案 1 :(得分:0)

您可以使用日期数学和DAYOFWEEK的适当组合替换next_day,例如:

mydate + INTERVAL (8-DAYOFWEEK(mydate)) DAY

应该适用于星期日

答案 2 :(得分:0)

这是一个MySQL存储函数,你可以定义它与Oracle的NEXT_DAY()做同样的事情:

CREATE FUNCTION next_day (date DATE, dow VARCHAR(10)) RETURNS DATE 
RETURN FROM_DAYS(
    TO_DAYS(date) - DAYOFWEEK(date)
    + IF(DAYOFWEEK(date) >= FIND_IN_SET(dow,  'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday'), 7, 0)
    + FIND_IN_SET(dow, 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday')
  );