选择ID,计数(ID)和按日期分组

时间:2015-03-27 19:42:00

标签: sql sql-server date count group-by

我有一个文章表,其中包含id和日期(月/年)列,首先我想计算ID并按日期对它们进行分组,然后我想查看哪个ID属于哪个日期组在单个这样的查询:

id   date   count
-----------------
1    01/2015   2
2    01/2015   2
3    02/2015   1
4    03/2015   4
5    03/2015   4
6    03/2015   4
7    03/2015   4

我有2个查询

Select Count(id) 
from article 
group by date

Select id 
from article

给出结果;

count date            id  date
-------------         ----------
2     01/2015         1  01/2015
1     02/2015         2  01/2015 
4     03/2015         3  02/2015

我需要一个像

这样的查询
select count(id), id, date 
from....

带来了我的C#代码中使用的id,count,date列。

有人可以帮我这个吗?

4 个答案:

答案 0 :(得分:3)

SELECT id,
       date,
       COUNT(*) OVER (PARTITION BY date) AS Count
FROM article

Sql fiddle

答案 1 :(得分:1)

在一个查询中不能完全执行此操作,但您可以使用CTE生成单个结果集:

create table #tt (id int null, dt varchar(8))
insert #tt values
(1,'01/2015'),
(2,'01/2015'),
(3,'02/2015'),
(4,'03/2015'),
(5,'03/2015'),
(6,'03/2015'),
(7,'03/2015')

;with cteCount(d, c) AS
(
    select dt, count(id) from #tt group by dt
)
select id, dt, c
from #tt a
inner join cteCount cc
on a.dt = cc.d

drop table #tt

结果:

id  dt      c
1   01/2015 2
2   01/2015 2
3   02/2015 1
4   03/2015 4
5   03/2015 4
6   03/2015 4
7   03/2015 4

答案 2 :(得分:0)

if not exists(select * from TEST.sys.objects where type=N'U' and name=N'article')
begin
    create table article(
    [id] int,
    [date] date)
end

有这些数据:

insert into article(id,date) values(1,convert(date,'15/01/2015',103));
insert into article(id,date) values(1,convert(date,'15/02/2015',103));
insert into article(id,date) values(2,convert(date,'15/03/2015',103));
insert into article(id,date) values(2,convert(date,'15/01/2015',103));
insert into article(id,date) values(3,convert(date,'15/02/2015',103));
insert into article(id,date) values(4,convert(date,'15/03/2015',103));
insert into article(id,date) values(5,convert(date,'15/01/2015',103));
insert into article(id,date) values(5,convert(date,'15/02/2015',103));
insert into article(id,date) values(1,convert(date,'15/03/2015',103));
insert into article(id,date) values(2,convert(date,'15/01/2015',103));
insert into article(id,date) values(3,convert(date,'15/02/2015',103));
insert into article(id,date) values(4,convert(date,'15/03/2015',103));
insert into article(id,date) values(5,convert(date,'15/01/2015',103));
insert into article(id,date) values(1,convert(date,'15/02/2015',103));
insert into article(id,date) values(2,convert(date,'15/03/2015',103));
insert into article(id,date) values(3,convert(date,'15/01/2015',103));
insert into article(id,date) values(4,convert(date,'15/03/2015',103));

select id,[date], count(id) [count] from article
group by [date],[id]

the result:

id  date    count
1   2015-01-15  1
1   2015-02-15  2
1   2015-03-15  1
2   2015-01-15  2
2   2015-03-15  2
3   2015-01-15  1
3   2015-02-15  2
4   2015-03-15  3
5   2015-01-15  2
5   2015-02-15  1

答案 3 :(得分:0)

  

目前尚不清楚如何在结果中生成id字段。如果你   想要手动生成它然后使用RANK()或者如果你想获得它   从表id值开始,您可以使用max()min()(取决于   根据您的预期结果)

使用RANK() Fiddle Demo Here

尝试:

create table tt (id int null, dt varchar(8),count int)
insert tt values
(1,'01/2015',2),
(2,'01/2015',2),
(3,'02/2015',1),
(4,'03/2015',4),
(5,'03/2015',4),
(6,'03/2015',4),
(7,'03/2015',4)

查询:

 select count(id) as count,dt,RANK() 
    over(order by count(id)) as id from tt group by dt

<强> EDIT2: 或者你可以使用MAX()MIN()

像:

select count(id) as count,dt,Min(id) as id from tt group by dt

select count(id) as count,dt,MAX(id) as id from tt group by dt