本文介绍了如何从表中获取不同年份的成员数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在我的数据库中,我有这样的数据 Id |会员名称| CreatedAt | ____________________________ 1 | Dhinnaa | 14-06-2001 2 | senthil | 11-03-2002 3 | Jhonson | 22-06-2002 4 | DanielD | 30-11-2007 5 | Henryee | 01-01-2008 6 | AnilRex | 06-07-2008 7 | RexDani | 01-03-2008 i需要这样的 year1(1) year2(2) year3(1) year4(3) 如何编写查询或linq来获取此信息?解决方案 如果你想在数据库端完成,请按照以下步骤操作。 声明 @ tab1 table ( id int , nam varchar ( 20 ), cdate date ) 插入 进入 @ tab1 值( 1 ,' Dhinnaa',' 06/14/2001') insert 进入 @ tab1 值( 2 ,' senthil',' 06/14/2002') insert 进入 @ tab1 值( 3 ,' Jhonson',' 06/14/2007') insert 进入 @ tab1 值( 4 ,' DanielD',' 06/14/2008') 插入 进入 @ tab1 值( 5 ,' Henryee',' 06/14/2002') insert 进入 @ tab1 值( 6 ,' AnilRex',' 06/14/2008') 插入 进入 @ tab1 values ( 7 ,' RexDani',' 06/14/2008') 选择 ' year(' + cast((ROW_NUMBER() over ( order by cyear)) as varchar )+ ' )',cyear 来自 (选择计数(p.id)idcount,cyear cyear 来自( 选择 id,DATEPART(yy,cdate) as cyear 来自 @ TAB1 )p group by cyear)q - 你也可以尝试这样做, SELECT T.Year,' 年份(' + CAST(ROW_NUMBER( ) OVER ( ORDER BY T.Year ) AS VARCHAR ( 5 ))+ ' )' ' YearCount' FROM ( SELECT 年份(cdate) )' 年',COUNT(ID)' RecCount' FROM @ tab1 GROUP BY 年(cdate))T 试试这个 foreach ( var item in data.GroupBy(x = > x.CreatedAt)。选择( group = > new { CreatedAt = group .Key, Count = group .Count()}) .OrderBy(x = > x.CreatedAt) { Console.WriteLine( {0} {1},item.CreatedAt,item.Count); } 希望这有帮助 In my database I have data like thisId | MemberName | CreatedAt |____________________________1 | Dhinnaa | 14-06-20012 | senthil | 11-03-20023 | Jhonson | 22-06-20024 | DanielD | 30-11-20075 | Henryee | 01-01-20086 | AnilRex | 06-07-20087 | RexDani | 01-03-2008i need to get like thisyear1 (1)year2 (2)year3 (1)year4 (3)How to write query or linq to get this? 解决方案 If you want to do in at the database end follow this.declare @tab1 table( id int, nam varchar(20), cdate date)insert into @tab1 values(1,'Dhinnaa','06/14/2001')insert into @tab1 values(2,'senthil','06/14/2002')insert into @tab1 values(3,'Jhonson','06/14/2007')insert into @tab1 values(4,'DanielD','06/14/2008')insert into @tab1 values(5,'Henryee','06/14/2002')insert into @tab1 values(6,'AnilRex','06/14/2008')insert into @tab1 values(7,'RexDani','06/14/2008')select 'year(' + cast((ROW_NUMBER() over (order by cyear)) as varchar) + ')',cyear from(select count(p.id) idcount,cyear cyear from (select id,DATEPART(yy,cdate) as cyear from @tab1) pgroup by cyear) q-- U can try like this also, SELECT T.Year, 'Year('+CAST(ROW_NUMBER() OVER(ORDER BY T.Year) AS VARCHAR(5))+')' 'YearCount'FROM (SELECT YEAR(cdate) 'Year', COUNT(ID) 'RecCount' FROM @tab1 GROUP BY YEAR(cdate)) TTry this foreach(var item in data.GroupBy(x => x.CreatedAt) .Select(group => new { CreatedAt = group.Key, Count = group.Count() }) .OrderBy(x => x.CreatedAt){ Console.WriteLine("{0} {1}", item.CreatedAt, item.Count);}Hope this helps 这篇关于如何从表中获取不同年份的成员数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 13:39