当前位置: 代码迷 >> Sql Server >> 从表中失去每日的顾客数量
  详细解决方案

从表中失去每日的顾客数量

热度:48   发布时间:2016-04-24 09:28:26.0
从表中得到每日的顾客数量
表结构
id    date                    customiD
1     2014-07-08          001
1     2014-07-08          002
1     2014-07-08          003
1     2014-07-09         003

怎么从该表中得出每日的顾客数量?谢谢
------解决思路----------------------
SELECT date,
       COUNT(DISTINCT customiD) 不重复顾客数量,
       COUNT(*) 顾客人次
  FROM table1
 GROUP BY date

你要哪个数量自己挑。
------解决思路----------------------
SELECT id,date,COUNT(customiD)人次
,COUNT(DISTINCT customiD)人数
FROM GROUP BY id,date
------解决思路----------------------

create table #TT(
[id] int not null,
[date] date not null,
[customid] varchar(10) not null
)

insert into #TT
select 1,'2014-07-08','001' union all
select 1,'2014-07-08','002' union all
select 1,'2014-07-08','003' union all
select 1,'2014-07-09','003'

select [date],count(distinct customid) as qty from #TT  group by [date] 
/* 结果
date qty
2014-07-08 3
2014-07-09 1

*/

------解决思路----------------------
LZ 可能是要的是人次,一个会员天来 8 回,就算 8 个。 
------解决思路----------------------
with a(id,date,customid) as
(select 1,'2014-07-08','001' union all
select 1,'2014-07-08','002' union all
select 1,'2014-07-08','003' union all
select 1,'2014-07-09','003')
select date,customid,COUNT(*) pty from a group by customid,date

------解决思路----------------------
SELECT 
date
,COUNT(DISTINCT customiD) 人数
,COUNT(*) 人次
FROM tb1
GROUP BY date
  相关解决方案