sql查询得到月份计数

时间:2015-05-08 11:59:06

标签: mysql

下面是我数据库中的数据。

startingdate - varchar

tablename - couponentry

coupon     price     month     startingdate
 100        15       1-month    02-03-2015
 101        15       1-month    04-03-2015
 102        15       1-month    05-03-2015
 103        15       1-month    07-04-2015
 104        15       1-month    08-04-2015
 105        15       1-month    15-05-2015
 106        15       1-month    18-05-2015

 107        15       2-month    02-02-2015
 108        15       2-month    04-02-2015
 109        15       2-month    05-02-2015
 110        15       2-month    07-03-2015
 111        15       2-month    08-03-2015
 112        15       2-month    15-05-2015
 113        15       2-month    18-05-2015
 114        15       2-month    18-05-2015

我需要基于1个月,2个月等的总优惠券数量...还需要显示月份明智的优惠券总数。

FOR EX - 在上述数据中 - 在1个月内创建了7张优惠券。 从这3张优惠券进入3月,2月4月和2月5月.... 喜欢明智......

预期输出如下

 month     jan  feb  mar  april   may   june ...  totalcoupon    totalprice
1-month               3    2       2                   7            105
2-month          3    2            3                   8            120

下面是我试过的查询...

SELECT month,COUNT(CE.coupon) As Total,SUM(CE.Price) AS TotalPrice FROM coupon_entry CE LEFT JOIN subagentmaster SAM ON CE.subagentid = SAM.id  LEFT JOIN tehsilmaster TM ON SAM.tehsil = TM.id LEFT JOIN city_master CM ON CE.city = CM.id WHERE CM.cityname = 'nagpur'    GROUP BY CE.month

但这低于输出

month       totalcoupon     totalprice
1-month        7                 105
2-month        8                 120

2 个答案:

答案 0 :(得分:1)

以下查询可以帮助您:

mv

答案 1 :(得分:0)

忽略整个nagpur位...

SELECT month
     , SUM(CASE WHEN startingdate >= '2015-01-01' AND startingdate < '2015-02-01' THEN 1 ELSE 0 END) 'jan' 
     , SUM(CASE WHEN startingdate >= '2015-02-01' AND startingdate < '2015-03-01' THEN 1 ELSE 0 END) 'feb' 
     , SUM(CASE WHEN startingdate >= '2015-03-01' AND startingdate < '2015-04-01' THEN 1 ELSE 0 END) 'mar' 
     , SUM(CASE WHEN startingdate >= '2015-04-01' AND startingdate < '2015-05-01' THEN 1 ELSE 0 END) 'apr' 
     , SUM(CASE WHEN startingdate >= '2015-05-01' AND startingdate < '2015-06-01' THEN 1 ELSE 0 END) 'may' 
     , SUM(CASE WHEN startingdate >= '2015-06-01' AND startingdate < '2015-07-01' THEN 1 ELSE 0 END) 'jun' 
     , SUM(CASE WHEN startingdate >= '2015-07-01' AND startingdate < '2015-08-01' THEN 1 ELSE 0 END) 'jul' 
     , SUM(CASE WHEN startingdate >= '2015-08-01' AND startingdate < '2015-09-01' THEN 1 ELSE 0 END) 'aug' 
     , SUM(CASE WHEN startingdate >= '2015-09-01' AND startingdate < '2015-10-01' THEN 1 ELSE 0 END) 'sep' 
     , SUM(CASE WHEN startingdate >= '2015-10-01' AND startingdate < '2015-11-01' THEN 1 ELSE 0 END) 'oct' 
     , SUM(CASE WHEN startingdate >= '2015-11-01' AND startingdate < '2015-12-01' THEN 1 ELSE 0 END) 'nov' 
     , SUM(CASE WHEN startingdate >= '2015-12-01' AND startingdate < '2016-01-01' THEN 1 ELSE 0 END) 'dec'
     , COUNT(*) totalcoupon
     , SUM(price) totalprice
  FROM couponentry
 GROUP 
    BY month;

但是,正如你提到的xampp - 在PHP中执行此操作会更简单(也更具伸缩性)。