sql server 超市会员,查询会员1月消费次数 2月消费次数
大神。。。提问不带表结构。。。
表结构呢?请把问题描述的具体一些。
用sum函数就可以了,查询条件为时间段+会员号
select count(*) from 表名 where 时间段 and 会员编号
分两种;
如果横向是月份,纵向是会员
select 会员,
sum(case when 消费时间 >1月1日 and 消费时间<=1月31日 then 1 else 0 end) January,
sum(case when 消费时间 >2月1日 and 消费时间<=2月28日 then 1 else 0 end) February,
sum(case when 消费时间 >2月1日 and 消费时间<=2月28日 then 1 else 0 end) February
...
from 表名
group by 会员
order by 会员
如果横向是会员纵向是月份
如果有销售表的话,销售单据id是唯一的
select count(销售单据id) from 销售表 where 日期 and 会员