相同数据一起统计、合并

    技术2022-05-11  21

    高难度啊!!!请问怎么样把相同数据一起统计、合并显示?company与name相同的数据出现的次数,并一起显示最新的数据,按次数多少进行排序,以company与name排序id company name Product Mark date1 A YY 1 H 2008-11-32 A HFF 2 Y 2008-11-33 B JGR 1 H 2008-11-54 C NYJ 3 U 2008-11-55 B NRM 1 O 2008-11-66 A YY 2 P 2008-11-77 A YY 2 Y 2008-11-88 B JGR 1 Y 2008-11-8上表的统计结果应为id company name Product Mark date count7 A YY 2 Y 2008-11-8 38 B JGR 1 Y 2008-11-8 22 A HFF 2 Y 2008-11-3 13 B JGR 1 H 2008-11-5 14 C NYJ 3 U 2008-11-5 1即,每A公司的YY职员一共来了3次,显示最后日期的记录+次数。 =====================================================================================-->生成测试数据declare @t table(id int,company varchar(10), name varchar(10), Product int, Mark varchar(10),date datetime)insert @t select 1,'A','YY',1,'H','2008-11-3'insert @t select 2,'A','HFF',2,'Y','2008-11-3'insert @t select 3,'B','JGR',1,'H','2008-11-5'insert @t select 4,'C','NYJ',3,'U','2008-11-5'insert @t select 5,'B','NRM',1,'O','2008-11-6'insert @t select 6,'A','YY',2,'P','2008-11-7'insert @t select 7,'A','YY',2,'Y','2008-11-8'insert @t select 8,'B','JGR',1,'Y','2008-11-8'-->开始查询select    *,    [count]=(select count(1) from @t where company=t.company and name=t.name)  from @t t where not exists(    select 1     from @t     where company=t.company and name=t.name and date>t.date)order by [count] desc


    最新回复(0)