2016-06-07 30 views
0

我寫了一個存儲過程來從一個日期得到一週,它也返回在一週的開始日期和週數和年份。MariaDB的開始到52

我知道了「周」的功能,但是這並沒有給我在一週的開始日期,我不知道這是否給出了一週,一年的函數。

的問題是:
我怎樣才能在給定的週數周的開始,「日期」?當週的開始傳過來的日指數,0 =星期日,1 =星期一等

我目前的功能並不總是工作,如果一週的第一天是星期一,那麼週日落入下週,而不是我希望的那一週結束。

+0

任何人都可以幫助解決這個問題嗎? – SPlatten

回答

0

解決了,我重新寫存儲過程:

exitProc:BEGIN 
    #-- 
    # Procedure: 
    # weekFromDate 
    # 
    # Parameters: 
    # vcCompKey, the key associated with the company 
    # dtDate, the date to translate 
    # dtOutSOW, returned start of week date 
    # siOutWeek, returned week number 
    # siOutYear, returned year 
    #-- 
     DECLARE siDIY   SMALLINT; #Day in year 
     DECLARE siFDOW   SMALLINT; #First day of week 
     DECLARE siGoBack  SMALLINT;  #Flag used to check for last year 
     DECLARE siRmonth  SMALLINT; #Reference Month 
     DECLARE siRyear  SMALLINT; #Reference Year 
     DECLARE dtSOY   DATE;  #Date of start of year 
     DECLARE vcFMDOY  VARCHAR(12);#First month and day of year 
     DECLARE vcFDOW   VARCHAR(12);#First day of the week 
     DECLARE vcDYSOW  VARCHAR(80);#Days of week 
    #Get the first day of the week for the specified company 
     SET vcFDOW = vcGetParamValue(vcCompKey, 'Var:First day of week'); 

    IF (vcFDOW IS NULL) THEN 
    #No entry found, abort! 
     LEAVE exitProc; 
    END IF; 
    #Get the first month and day of the year for the specified company 
    SET vcFMDOY = vcGetParamValue(vcCompKey, 'Var:First day of year'); 

    IF (vcFMDOY IS NULL) THEN 
    #No entry found, abort! 
     LEAVE exitProc; 
    END IF; 
    #Set-up days of week 
    SET vcDYSOW = 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday'; 
    #Get the first day of the week index base 1 
    SET siFDOW = FIND_IN_SET(LOWER(vcFDOW), LOWER(vcDYSOW)) - 1; 
    #Get the reference month and year 
    SET siRmonth = MONTH(dtDate); 
    SET siRyear = YEAR(dtDate); 
    SET dtSOY = DATE(CONCAT(siRyear, '/', vcFMDOY)); 
    #Calculate the start of week date 
    SET dtOutSOW = DATE_SUB(dtDate, INTERVAL (DAYOFWEEK(dtDate) - siFDOW) DAY) + 1; 
    #Calculate the day in year 
    SET siDIY = DATEDIFF(dtOutSOW, dtSOY); 
    #Do we need to go back to the end of the previous year? 
    SET siGoBack = YEAR(dtDate) - YEAR(dtOutSOW); 

    IF siGoBack < 0 Or siDIY < 0 Or dtDate < dtOutSOW THEN 
    #Yes 
     IF YEAR(dtOutSOW) = YEAR(dtDate) THEN 
      SET dtOutSOW = DATE_SUB(dtOutSOW, INTERVAL 7 DAY); 
     END IF; 
     SET dtSOY = DATE(CONCAT(YEAR(dtOutSOW), '/', vcFMDOY)); 
     SET siDIY = DATEDIFF(dtOutSOW, dtSOY); 
    END IF;  
    #Calculate the week no. and year 
    SET siOutWeek = (siDIY/7) + 1; 
    SET siOutYear = YEAR(dtOutSOW); 
END 

這個程序不使用我的數據庫中的其他表,並允許公司有不同的歲月開始。

0

使用Sequence引擎。您可以根據需要調整下面的例子:

MariaDB [_]> SHOW ENGINES\G 
. 
. 
. 
*************************** 3. row *************************** 
     Engine: SEQUENCE 
    Support: YES 
    Comment: Generated tables filled with sequential values 
Transactions: YES 
      XA: NO 
    Savepoints: YES 
. 
. 
. 

MariaDB [_]> SET @`year` := 2016, 
      ->  @`mode` := 1, 
      ->  @`week` := 23; 
Query OK, 0 rows affected (0.00 sec) 

MariaDB [_]> SELECT 
      -> `der`.`date`, 
      -> `der`.`week`, 
      -> `der`.`year` 
      -> FROM (
      -> SELECT 
      ->  `der`.`date`, 
      ->  WEEK(`der`.`date`, @`mode`) `week`, 
      ->  YEAR(`der`.`date`) `year` 
      -> FROM (
      ->  SELECT 
      ->  DATE_ADD(CONCAT(@`year`, '-01-01'), INTERVAL `s`.`seq` DAY) `date` 
      ->  FROM 
      ->  seq_0_to_365 `s` 
      -> ) `der` 
      ->) `der` 
      -> WHERE 
      -> `der`.`week` = @`week` AND 
      -> `der`.`year` = @`year`; 
+------------+------+------+ 
| date  | week | year | 
+------------+------+------+ 
| 2016-06-06 | 23 | 2016 | 
| 2016-06-07 | 23 | 2016 | 
| 2016-06-08 | 23 | 2016 | 
| 2016-06-09 | 23 | 2016 | 
| 2016-06-10 | 23 | 2016 | 
| 2016-06-11 | 23 | 2016 | 
| 2016-06-12 | 23 | 2016 | 
+------------+------+------+ 
7 rows in set (0.01 sec) 
+0

謝謝,我會試試看。 – SPlatten

+0

我懷疑這樣使用'sequence'會導致計算整年的數據量,只返回一天的結果? –

+0

@RickJames:沒錯,這是解決方案選項的缺點。 – wchiquito

0

作爲一個測試,我會找到當前周的開始,第一個音符:

mysql> SELECT NOW(), WEEK(NOW()); 
+---------------------+-------------+ 
| NOW()    | WEEK(NOW()) | 
+---------------------+-------------+ 
| 2016-06-18 12:10:58 |   24 | 
+---------------------+-------------+ 

那麼這樣的函數的肉:

mysql> SELECT '2016-01-01' 
      + INTERVAL 7*24 
      - DAYOFWEEK('2016-01-01') 
      + 1 DAY; 
+----------------------------------------------------------------+ 
| '2016-01-01' + INTERVAL 7*24 - DAYOFWEEK('2016-01-01') + 1 DAY | 
+----------------------------------------------------------------+ 
| 2016-06-12              | 
+----------------------------------------------------------------+ 

'2016-01-01'是的開始有問題的一年。
24WEEK()號碼。
+ 1 DAY是爲了補償周的開始。
需要做其他事情來處理您選擇每週星期幾開始一週的選項。