每周显示数据

时间:2017-10-12 12:05:18

标签: mysql sql

我在表格中有一个日期列。根据日期的选择,它应该计算过去6周的订单数量,如第1周,第2周......第6周(这不是周数,它是简单的序列号)。例如,如果用户选择日期为12/10/2017(年/月/日),则应计算周日期的订单计数

below image:

有人可以告诉我这是否可以在SQL中使用?

2 个答案:

答案 0 :(得分:1)

这可以使用SQL代码。

<强>查询

SELECT  
   CONCAT(week1.first_day, '-', week1.second_day) AS Week1
 , CONCAT(week2.first_day, '-', week2.second_day) AS Week2
 , CONCAT(week3.first_day, '-', week3.second_day) AS Week3
 , CONCAT(week4.first_day, '-', week4.second_day) AS Week4
 , CONCAT(week5.first_day, '-', week5.second_day) AS Week5
 , CONCAT(week6.first_day, '-', week6.second_day) AS Week6
FROM (
 SELECT 
     DATE_FORMAT(@first_date_of_week, '%d/%m/%Y') AS first_day
   , DATE_FORMAT(@date, '%d/%m/%Y') AS second_day 
)  
 week1 
CROSS JOIN (
  SELECT 
      DATE_FORMAT(@first_date_of_week - INTERVAL 1 WEEK, '%d/%m/%Y') AS first_day
    , DATE_FORMAT(@first_date_of_week - INTERVAL 1 WEEK + INTERVAL 1 WEEK - INTERVAL 1 DAY, '%d/%m/%Y') AS second_day
) 
 week2
CROSS JOIN (
  SELECT 
      DATE_FORMAT(@first_date_of_week - INTERVAL 2 WEEK, '%d/%m/%Y') AS first_day
    , DATE_FORMAT(@first_date_of_week - INTERVAL 2 WEEK + INTERVAL 1 WEEK - INTERVAL 1 DAY, '%d/%m/%Y') AS second_day
) 
 week3 
CROSS JOIN (
  SELECT 
      DATE_FORMAT(@first_date_of_week - INTERVAL 3 WEEK, '%d/%m/%Y') AS first_day
    , DATE_FORMAT(@first_date_of_week - INTERVAL 3 WEEK + INTERVAL 1 WEEK - INTERVAL 1 DAY, '%d/%m/%Y') AS second_day
) 
 week4  
CROSS JOIN (
  SELECT 
      DATE_FORMAT(@first_date_of_week - INTERVAL 4 WEEK, '%d/%m/%Y') AS first_day
    , DATE_FORMAT(@first_date_of_week - INTERVAL 4 WEEK + INTERVAL 1 WEEK - INTERVAL 1 DAY, '%d/%m/%Y') AS second_day
) 
 week5  
CROSS JOIN (
  SELECT 
      DATE_FORMAT(@first_date_of_week - INTERVAL 5 WEEK, '%d/%m/%Y') AS first_day
    , DATE_FORMAT(@first_date_of_week - INTERVAL 5 WEEK + INTERVAL 1 WEEK - INTERVAL 1 DAY, '%d/%m/%Y') AS second_day
) 
 week6   

CROSS JOIN (
  SELECT
      @date := STR_TO_DATE('12/10/2017', '%d/%m/%Y') AS DATE
    , @first_date_of_week := @date - INTERVAL (DAYOFWEEK(@date) - 1) DAY AS first_date_of_the_week
) init_user_params

<强>结果

Week1                  Week2                  Week3                  Week4                  Week5                  Week6                  
---------------------  ---------------------  ---------------------  ---------------------  ---------------------  -----------------------
08/10/2017-12/10/2017  01/10/2017-07/10/2017  24/09/2017-30/09/2017  17/09/2017-23/09/2017  10/09/2017-16/09/2017  03/09/2017-09/09/2017  

答案 1 :(得分:0)

这是实现相同结果的另一种方法 -

select CONCAT(subdate(CURDATE(), INTERVAL (weekday(CURDATE())+1) DAY), ' - ', CURDATE())
      ,CONCAT(subdate(date_sub(CURDATE(), interval 7 day), INTERVAL (weekday(CURDATE())+1) DAY), ' - ', subdate(CURDATE(), INTERVAL (weekday(CURDATE())+2) DAY))
      ,CONCAT(subdate(date_sub(CURDATE(), interval 14 day), INTERVAL (weekday(CURDATE())+1) DAY), ' - ', subdate(date_sub(CURDATE(), interval 7 day), INTERVAL (weekday(CURDATE())+2) DAY))
      ,CONCAT(subdate(date_sub(CURDATE(), interval 21 day), INTERVAL (weekday(CURDATE())+1) DAY), ' - ', subdate(date_sub(CURDATE(), interval 14 day), INTERVAL (weekday(CURDATE())+2) DAY))
      ,CONCAT(subdate(date_sub(CURDATE(), interval 28 day), INTERVAL (weekday(CURDATE())+1) DAY), ' - ', subdate(date_sub(CURDATE(), interval 21 day), INTERVAL (weekday(CURDATE())+2) DAY))
      ,CONCAT(subdate(date_sub(CURDATE(), interval 35 day), INTERVAL (weekday(CURDATE())+1) DAY), ' - ', subdate(date_sub(CURDATE(), interval 28 day), INTERVAL (weekday(CURDATE())+2) DAY))