易截截图软件、单文件、免安装、纯绿色、仅160KB

mssql 求多字段最大值2

昨天发的这个帖子:http://topic.csdn.net/u/20100407/15/1af60691-1b11-4a0a-bf85-6187add9fb4f.html

SQL code:

select tId,max(class1) maxClass
from(select tId,class1 from tb1 union all select tId,class2 from tb1) tt
group by tId
--2
select tId,max(cc1) maxClass
from(select tId,class1+class2 cc1 from tb1 union all select tId,class1+class2 cc2 from tb1) tt
group by tId



现在我要显示这个的结果
tId tName maxClass maxSum
1 test2 35 60
2 test21 85 125
3 test32 70 140

请问怎么把子查询合并起来,谢谢。


select tId,max(CASE WHEN class1>class2 THEN class1 ELSE class2 END) as maxClass,
max(class1+class2) as maxSum
from tb1
group by tId

以上假设class1 和 class2都不能为Null值


比如再加一个字段:
SQL code:

create table tb1
(
tId int,
tName nvarchar(20),
class1 int,
class2 int,
class3 int
)
insert into tb1
select 1,'test',10,20,30
union all
select 1,'test1',20,35,25
union all
select 1,'test2',30,30,40
union all
select 2,'test21',30,85,20
union all
select 2,'test22',50,75,90
union all
select 3,'test31',60,65,40
union all
select 3,'test32',70,70,50



那又怎么解决


相关问答:

MSSQL如何插入不相同的记录 - .NET技术 / ASP.NET

如题,用户注册时,用户名是不能重复的,如何判断插入的记录不重复呢?
请看清楚我的题目,不要从网上抄一些答案过来,网上很多人写的先select再insert的都是瞎扯,压根就没考虑到并发性问题,我现在不想加上序列化 ......

MSSQL中如何选择一行中最小的时间?

一张表,字段分为id,time1,time2,time3,time4,time5 
请问我应该如何选择ID为1的那条记录中time时间最小的值呢?
UP!

[code=SQL][/code]select min(t) from 
(select time1 as t from t1 where id ......

mssql 默认参数怎样处理? - MS-SQL Server / 基础类

我定义一个函数,有五个参数
但实际上我调用时不一定传五个参数,有时只需要传二个参数,有时只需要传三个参数等等
这种参数怎么定义呢?
SQL code:
CREATE FUNCTION [ owner_name.] function_name
( [ { ......

MSSQL有 类似于 SPLIT的函数吗?

如题- -~
我有这么个东西
一个
字符串如下所示
1,2,3,4,5
然后我
需要循环的把 这些数据插入到数据库中
我该怎么做?
http://blog.csdn.net/htl258/archive/2009/09/09/4533846.aspx


SQL code:
我现 ......

mssql sql简化

SQL code:

declare @begin datetime --开始时间
declare @end datetime --结束时间
declare @startIndex int --起始值默认为8
declare @interval int --时段长
select @startIndex=8
select @ ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号