简单的最大最小问题:)

来源:互联网 发布:广西妇幼网络医院app 编辑:程序博客网 时间:2024/05/16 09:31

例表tb:
id  price    netprice
1   8.00      20.00
2   10.00     5.00
3   0.00      3.00    
我想要查出price与netprice之间大的列
比如查出如下数据:
id  newprice
1    20.00
2    10.00
3    3.00
sql语句要怎么写?

Create Table tb
(id Int,
 price Numeric(10, 2),
 netprice Numeric(10, 2))

Insert tb Select 1,   8.00,      20.00
Union All Select 2,   10.00,     5.00
Union All Select 3,   0.00,      3.00    
GO

select id,(case when price>netprice then price else netprice end)as aa
from tb

select id,max(aa)
from
(select id,price as aa from tb
 union all
 select id,netprice as aa from tb
)t group by id
--result:
1 20.00 
2 10.00 
3 3.00