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
那又怎么解决
相关问答:
最近下面反应说程序异常
然后打开数据库一看
发现很多表都"只读"状态??
用sa登陆也是...
很奇怪啊..
大牛知道是什么原因吗?
愿意出高分求
谁解决了
给200分!!
数据库出问题了?
引用
最近下面 ......
一张表,字段分为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 ......
我用的是real数据类型,但是存储小于1的数时前面的0没有了,该怎么解决,比如:我想存储0.1,在数据库里只有“.1”,0没有了,要怎么设置,或者数据类型是不是要换一种 ??
decimal(9,1)
这个貌似与类型无关
与操 ......
SQL code:
/*
create database temp
use temp
create table employees
(
eID int identity primary key,
eName nvarchar(50) not null,
eSex smallint not null default(1),
eSort smalli ......
//相关sql
SQL code:
create table tb1
(
tId int,
tName nvarchar(20),
class1 int,
class2 int
)
insert into tb1
select 1,'test',10,20
union all
select 1,'test1',20,35
uni ......