A B C
66 32.78 2011-04-02
66 31.98 2011-04-02
67 30.32 2011-04-02
67 0.02 2011-04-02
66 30.82 2011-04-02
47 17.96 2011-04-02
65 34.36 2011-04-02
67 28.7 2011-04-02
67 32 2011-04-02
67 32.02 2011-04-02
65 29.7 2011-04-02
67 28.76 2011-04-02
67 28.28 2011-04-02
66 30.34 2011-04-02要求按40-50、50-60、60-70范围汇总。
A字段中47在40-50分范围内,剩下的应该全部在60-70范围内,之后汇总B
最后的结果应该是
50 17.96 2011-04-02
70 370.08 2011-04-02求解!!!!!
66 32.78 2011-04-02
66 31.98 2011-04-02
67 30.32 2011-04-02
67 0.02 2011-04-02
66 30.82 2011-04-02
47 17.96 2011-04-02
65 34.36 2011-04-02
67 28.7 2011-04-02
67 32 2011-04-02
67 32.02 2011-04-02
65 29.7 2011-04-02
67 28.76 2011-04-02
67 28.28 2011-04-02
66 30.34 2011-04-02要求按40-50、50-60、60-70范围汇总。
A字段中47在40-50分范围内,剩下的应该全部在60-70范围内,之后汇总B
最后的结果应该是
50 17.96 2011-04-02
70 370.08 2011-04-02求解!!!!!
insert into tb select 66,32.78,'2011-04-02'
insert into tb select 66,31.98,'2011-04-02'
insert into tb select 67,30.32,'2011-04-02'
insert into tb select 67,0.02,'2011-04-02'
insert into tb select 66,30.82,'2011-04-02'
insert into tb select 47,17.96,'2011-04-02'
insert into tb select 65,34.36,'2011-04-02'
insert into tb select 67,28.7,'2011-04-02'
insert into tb select 67,32,'2011-04-02'
insert into tb select 67,32.02,'2011-04-02'
insert into tb select 65,29.7,'2011-04-02'
insert into tb select 67,28.76,'2011-04-02'
insert into tb select 67,28.28,'2011-04-02'
insert into tb select 66,30.34,'2011-04-02'
go
select a,sum(b)b,c from(
select (a+9)/10*10 a,b,c from tb
)t group by a,c
go
drop table tb
/*
a b c
----------- --------------------------------------- -----------------------
50 17.96 2011-04-02 00:00:00.000
70 370.08 2011-04-02 00:00:00.000(2 行受影响)
*/
declare @table table (A int,B numeric(4,2),C datetime)
insert into @table
select 66,32.78,'2011-04-02' union all
select 66,31.98,'2011-04-02' union all
select 67,30.32,'2011-04-02' union all
select 67,0.02,'2011-04-02' union all
select 66,30.82,'2011-04-02' union all
select 47,17.96,'2011-04-02' union all
select 65,34.36,'2011-04-02' union all
select 67,28.7,'2011-04-02' union all
select 67,32,'2011-04-02' union all
select 67,32.02,'2011-04-02' union all
select 65,29.7,'2011-04-02' union all
select 67,28.76,'2011-04-02' union all
select 67,28.28,'2011-04-02' union all
select 66,30.34,'2011-04-02'select '40-50',sum(case when A between 40.00 and 49.99 then B else 0 end),max(C) from @table
union all
select '50-60',sum(case when A between 50.00 and 59.99 then B else 0 end),max(C) from @table
union all
select '60-70',sum(case when A between 60.00 and 69.99 then B else 0 end),max(C) from @table不想要0 就加个条件。
go
create table [TB] (A int,B numeric(4,2),C datetime)
insert into [TB]
select 66,32.78,'2011-04-02' union all
select 66,31.98,'2011-04-02' union all
select 67,30.32,'2011-04-02' union all
select 67,0.02,'2011-04-02' union all
select 66,30.82,'2011-04-02' union all
select 47,17.96,'2011-04-02' union all
select 65,34.36,'2011-04-02' union all
select 67,28.7,'2011-04-02' union all
select 67,32,'2011-04-02' union all
select 67,32.02,'2011-04-02' union all
select 65,29.7,'2011-04-02' union all
select 67,28.76,'2011-04-02' union all
select 67,28.28,'2011-04-02' union all
select 66,30.34,'2011-04-02'select * from [TB]WITH TT
AS(
SELECT [Col]=CASE WHEN A BETWEEN 40 AND 50 THEN '50' ELSE '70' END,
B,C
FROM TB)SELECT col,SUM(B),MAX(C) FROM TT GROUP BY col
/*
col (无列名) (无列名)
50 17.96 2011-04-02 00:00:00.000
70 370.08 2011-04-02 00:00:00.000*/
a,sum(b) as b,convert(varchar(10),c,120)
from
(select (a+9)/10*10 a,b,c from tb)t
group by
a,convert(varchar(10),c,120)
select (a+9)/10*10 a,b,c from tb
)t group by a,convert(varchar(10),c,120)