sql >> Database >  >> RDS >> Mysql

Groepeer per week en selecteer records van de huidige week

 SELECT DATE_FORMAT(cc.datefield, '%d-%b') AS DATE
        , SUM(IFNULL(cds.quantity,0)) AS total_sales 
 FROM cart_calendar cc
 INNER JOIN cart_daily_sales cds
         ON (DATE(cds.order_date) = cc.datefield)
 WHERE WEEK(cc.datefield) = WEEK(now())
 GROUP BY WEEK(cc.datefield)

Of als je het per dag wilt:

 SELECT DATE_FORMAT(cc.datefield, '%d-%b') AS DATE
        , SUM(IFNULL(cds.quantity,0)) AS total_sales 
 FROM cart_calendar cc
 INNER JOIN cart_daily_sales cds
         ON (DATE(cds.order_date) = cc.datefield)
 WHERE WEEK(cc.datefield) = WEEK(now())
 GROUP BY MOD(WEEKDAY(cc.datefield)+1,6) WITH ROLLUP



  1. vergelijk 2 strings in sql [eerste string bevat de substrings van de 2e string]

  2. Laravel Welsprekende groep volgens meest recente record

  3. Array doorgeven via AJAX van php naar javascript

  4. HQL genereert onvolledige 'cross join' op executeUpdate