规则,如果浓度一样,浓度不变,数量求和,否则不改变原来的逻辑。
Group Sample 浓度 数量
GroupA KK1 1 16
GroupA KK2 2 17
GroupA KK3 3 18
GroupA KK4 4 19
GroupB KK1 5 20
GroupB KK2 5 21
GroupB KK3 5 22
GroupB KK4 5 23
GroupC KK1 16 24
GroupC KK2 15 25
GroupC KK3 19 26
GroupC KK4 30 27
’=》
Group Sample 浓度 数量
GroupA KK1 1 16
GroupA KK2 2 17
GroupA KK3 3 18
GroupA KK4 4 19
GroupB Sum 5 86(20+21+22+23)
GroupC KK1 16 24
GroupC KK2 15 25
GroupC KK3 19 26
GroupC KK4 30 27
Group Sample 浓度 数量
GroupA KK1 1 16
GroupA KK2 2 17
GroupA KK3 3 18
GroupA KK4 4 19
GroupB KK1 5 20
GroupB KK2 5 21
GroupB KK3 5 22
GroupB KK4 5 23
GroupC KK1 16 24
GroupC KK2 15 25
GroupC KK3 19 26
GroupC KK4 30 27
’=》
Group Sample 浓度 数量
GroupA KK1 1 16
GroupA KK2 2 17
GroupA KK3 3 18
GroupA KK4 4 19
GroupB Sum 5 86(20+21+22+23)
GroupC KK1 16 24
GroupC KK2 15 25
GroupC KK3 19 26
GroupC KK4 30 27
解决方案 »
- 遇到个问题,请教数据库高手指点
- 求一个比较有难度的SQL语句 当场给分~~
- 求解!求触发器,BOM层级分解。
- 如何用得到第二天凌晨的时间,比如说现在是'2005-3-1 9:00:00' 我想得到'2005-4-1 0:00:00'
- SQL数据表按天横向分类汇总?
- 如何在aceess 中通过程序访问sqlserver 数据库
- 如何禁止SQL远程连接!请允许本机连接!
- 百分好题怎么实现这条语句?稍微复杂。。
- 由表2中的两个字段值,加上两个变量值,在表1中生成一条新记录。如何写这个sql语句?
- 请问在新建作业中分类为未分类本地是指??
- SQL 函数问题 高手请进
- 关于sqlserver 2000中服务器网络实用工具
group by [Group],浓度
declare @table table ([Group] varchar(6),Sample varchar(3),浓度 int,数量 int)
insert into @table
select 'GroupA','KK1',1,16 union all
select 'GroupA','KK2',2,17 union all
select 'GroupA','KK3',3,18 union all
select 'GroupA','KK4',4,19 union all
select 'GroupB','KK1',5,20 union all
select 'GroupB','KK2',5,21 union all
select 'GroupB','KK3',5,22 union all
select 'GroupB','KK4',5,23 union all
select 'GroupC','KK1',16,24 union all
select 'GroupC','KK2',15,25 union all
select 'GroupC','KK3',19,26 union all
select 'GroupC','KK4',30,27select [Group],max(Sample) as Sample,浓度,
sum(数量) 数量 from @table group by [Group],浓度
/*
Group Sample 浓度 数量
------ ------ ----------- -----------
GroupA KK1 1 16
GroupA KK2 2 17
GroupA KK3 3 18
GroupA KK4 4 19
GroupB KK4 5 86
GroupC KK2 15 25
GroupC KK1 16 24
GroupC KK3 19 26
GroupC KK4 30 27
*/
insert into @table
select 'GroupA','KK1',1,16 union all
select 'GroupA','KK2',2,17 union all
select 'GroupA','KK3',3,18 union all
select 'GroupA','KK4',4,19 union all
select 'GroupB','KK1',5,20 union all
select 'GroupB','KK2',5,21 union all
select 'GroupB','KK3',5,22 union all
select 'GroupB','KK4',5,23 union all
select 'GroupC','KK1',16,24 union all
select 'GroupC','KK2',15,25 union all
select 'GroupC','KK3',19,26 union all
select 'GroupC','KK4',30,27select [Group],max(Sample) as Sample,浓度,
sum(数量) 数量 from @table group by [Group],浓度
/*
Group Sample 浓度 数量
------ ------ ----------- -----------
GroupA KK1 1 16
GroupA KK2 1 17
GroupA KK3 3 18
GroupA KK4 4 19
GroupB KK4 5 86
GroupC KK2 15 25
GroupC KK1 16 24
GroupC KK3 19 26
GroupC KK4 30 27
希望全一样才统计,另外
GroupB KK4 5 86
变成SUM总和
union ALL
SELECT GroupName,'Sum' as [sample],浓度,sum(数量) AS 数量 from table1 group by GroupName,浓度 having count(*)>1以上是按照GroupName 和浓度为分组的,这个你在提问时也没说清楚,当浓度一样,单GroupName不一样的,是否要相加。