【发布时间】:2015-05-18 14:40:39
【问题描述】:
表名 - 收据
优惠券 - varchar
coupondate customer
02-04-2015 A
02-05-2015 A
02-06-2015 A
02-07-2015 A
02-08-2015 A
02-09-2015 A
05-04-2015 B
05-05-2015 B
05-06-2015 B
05-07-2015 B
05-08-2015 B
05-09-2015 B
我需要计算最小和最大优惠券日期之间的总天数。
FOR EX -
客户 A 的优惠券从 2015 年 2 月 4 日开始,到 2015 年 2 月 9 日结束..所以总天数相差 153 天..
客户 B 的优惠券从 2015 年 5 月 4 日开始,到 2015 年 9 月 9 日结束..所以总天数相差 153 天..
预期输出
customer totaldiff
A 153 days
B 153 days
下面是我的sql查询
SELECT customer_name,
COUNT( coupondate ) AS totaldiff
FROM receipt_entry
GROUP BY customer_name
【问题讨论】:
-
您希望结果中有两列(customer 和 totaldiff),但您选择了 5 列。也许您应该只选择 customer_name, MAX( coupondate ) - MIN( coupondate ) ?
-
是的,我只需要 2 列,但我需要的主要输出是 totaldifference ...
-
总差异,独立于用户,还是每个用户?
-
总差异基于每个用户或客户...