表:name t_ident typeName c_ident mident移动 18 合约管理 1 22
紫光 18 合约管理 12 23
互动 10 手机 3 34
互动 10 手机 3 35
移动 17 报纸 1 77
移动 17 报纸 1 78
如何实现如下结果:移动 18 合约管理 1 22
紫光 18 合约管理 12 23
互动 10 手机 3 34
移动 17 报纸 1 78
紫光 18 合约管理 12 23
互动 10 手机 3 34
互动 10 手机 3 35
移动 17 报纸 1 77
移动 17 报纸 1 78
如何实现如下结果:移动 18 合约管理 1 22
紫光 18 合约管理 12 23
互动 10 手机 3 34
移动 17 报纸 1 78
create table #KK1
(
[name] nvarchar(50),
t_ident int,
typeName nvarchar(50),
c_ident int,
mident int
)
insert into #KK1 select '移动',18,'合约管理',1,22
insert into #KK1 select '紫光',18,'合约管理',12,23
insert into #KK1 select '互动',10,'手机',3,34
insert into #KK1 select '互动',10,'手机',3,35
insert into #KK1 select '移动',17,'报纸',1,77
insert into #KK1 select '移动',17,'报纸',1,78select * from #KK1 K where not exists
(select * from #KK1 where t_ident=K.t_ident and mident<K.mident and [name]=K.[name])
(
[name] nvarchar(50),
t_ident int,
typeName nvarchar(50),
c_ident int,
mident int
)
insert into #KK1 select '移动',18,'合约管理',1,22
insert into #KK1 select '紫光',18,'合约管理',12,23
insert into #KK1 select '互动',10,'手机',3,34
insert into #KK1 select '互动',10,'手机',3,35
insert into #KK1 select '移动',17,'报纸',1,77
insert into #KK1 select '移动',17,'报纸',1,78select * from #KK1 K where not exists
(select * from #KK1 where t_ident=K.t_ident and mident<K.mident and [name]=K.[name])name t_ident typeName c_ident mident
-------------------------------------------------- ----------- -------------------------------------------------- ----------- -----------
移动 18 合约管理 1 22
紫光 18 合约管理 12 23
互动 10 手机 3 34
移动 17 报纸 1 77(4 行受影响)
declare @表 table (name varchar(4),t_ident int,typeName varchar(8),c_ident int,mident int)
insert into @表
select '移动',18,'合约管理',1,22 union all
select '紫光',18,'合约管理',12,23 union all
select '互动',10,'手机',3,34 union all
select '互动',10,'手机',3,35 union all
select '移动',17,'报纸',1,77 union all
select '移动',17,'报纸',1,78select name,t_ident,typeName,MIN(c_ident),MIN(mident) from @表 GROUP BY name,typeName,t_ident
改为这样就行了:
SELECT name,t_ident,MAX(typeName),MAX(c_ident),MIN(mident) FROM V_getCropName_Allot
group by name,t_ident多谢!!!
移动 18 合约管理 1 22
紫光 18 合约管理 12 23
互动 10 手机 3 34
前三条都是取得mident 最小值