日期:2014-05-17 浏览次数:20508 次
Select 名称,max(ID),max(数量),min(类别) From TB Group By 名称
------解决方案--------------------
不知道你是不是有个行列转换的意思?
CREATE TABLE Ball ( ProuctId int, Name varchar(20) ) insert into Ball values (1,'篮球') insert into Ball values (2,'足球') insert into Ball values (3,'乒乓球') insert into Ball values (4,'羽毛球') Create Table BallInfo ( Id int, ProcutId int, Amount int, Status bit ) insert into BallInfo values (1, 1 ,1 ,1) insert into BallInfo values (2 ,1 ,5 ,0) insert into BallInfo values (3, 2 ,5, 0) insert into BallInfo values (4 ,2 ,1, 1) select C.Name, A.ProcutId, A.好的,B.坏的 from Ball C, (select ProcutId,SUM(Amount)over (partition by ProcutId,Status) as 好的 from ballInfo where status='1') A , (select ProcutId,SUM(Amount)over (partition by ProcutId,Status) as 坏的 from BallInfo where Status='0') B where A.ProcutId=B.ProcutId and C.ProuctId=B.ProcutId Name ProcutId 好的 坏的 篮球 1 1 5 足球 2 1 5
------解决方案--------------------
select id, name,sum(amount) over (partition by name) as amount , category from TestGroup
SELECT ROW_NUMBER() OVER(PARTITION BY NAME ORDER BY amount,category),id,name,amount,category from TestGroup
这样的结果吗
3 篮球 0 3
6 篮球 5 1
5 排球 10 0
2 乒乓球 2 1
1 羽毛球 1 0
4 足球 0 5
7 足球 10 0