Select ID、Count(ID) 和按日期分组

Select ID, Count(ID) and Group by Date

我有一篇文章 table,其中包含 ID 和日期 (month/year) 列,首先我想计算 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 列。

有人可以帮我解决这个问题吗?

不能在一个查询中完全做到这一点,但您可以使用 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
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

It's not clear how you want to generate the id field in result. if you want to generate it manually then use RANK() or if you want to get it from the table id value then you can use max() or min()(depends upon on your expected result)

使用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

编辑2: 或者你可以使用 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
SELECT id,
       date,
       COUNT(*) OVER (PARTITION BY date) AS Count
FROM article

Sql fiddle