2
我在表中有一個日期列。根據選擇的日期,它應該計算過去6周的訂單數量爲Week1,Week2 ... Week6(這不是其中的簡單序列號)。例如,如果用戶選擇了日期2017年12月10日(日/月/年),那麼它應該計算訂單數爲週日期爲以每週爲單位顯示數據
有人可以請讓我知道這是否在SQL中可能嗎?
我在表中有一個日期列。根據選擇的日期,它應該計算過去6周的訂單數量爲Week1,Week2 ... Week6(這不是其中的簡單序列號)。例如,如果用戶選擇了日期2017年12月10日(日/月/年),那麼它應該計算訂單數爲週日期爲以每週爲單位顯示數據
有人可以請讓我知道這是否在SQL中可能嗎?
這是可能的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
這裏是另一種方式來達到同樣的效果 -
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))
此數據不neccessarily屬於到數據庫。只是在飛行計算,而不是...順便說一句,擺脫存儲' - '分離的數據分貝... –
我該如何計算。我試着用下面的代碼'Select SUM(當CMTOrder.OrderPickUpTimestamp CMTOrder.CreateTimestamp和Dateadd(dd,-7,CMTOrder.CreateTimestamp)then 1 else 0 end)沒有爲我工作。上面的截圖我只是舉了一個例子。我的數據庫沒有保存像tat這樣的數據。 –