Question[SQL]:Can you use a SQL statement to calculating it!

本文介绍了一个SQL案例,演示如何根据书籍的价格将结果显示为不同的类别。“10到20”用于价格在10到20美元之间的书籍,“unknown”用于价格未知的书籍,而“other”则用于所有其他价格。
Question:Can you use a SQL statement to calculating it!

How can I print "10 to 20" for books that sell for between $10 and $20,"unknown" for books whose price is null, and "other" for all other prices?

Answer:

    if OBJECT_ID('dbo.Table101301'is not null
        
drop table dbo.Table101301
    
create table dbo.Table101301
    (
        
[bookid] int not null,
        
[bookname] varchar(10),
        
[price] varchar(10
    )
    
insert into dbo.Table101301 values(1,'book1'1)
    
insert into dbo.Table101301 values(2,'book2'5)
    
insert into dbo.Table101301 values(3,'book3'8)
    
insert into dbo.Table101301 values(4,'book4'10)
    
insert into dbo.Table101301 values(5,'book5'12)
    
insert into dbo.Table101301 values(6,'book6'null)
    
insert into dbo.Table101301 values(7,'book7'20)
    
insert into dbo.Table101301 values(8,'book8'21)
    
insert into dbo.Table101301 values(9,'book9'null)
    
    
select [bookid][bookname],
        
[price] = (
            
case 
                
when [price]  is null then 'unknown' 
                
when [price] between 10 and 20 then '10 to 20'
                
else [price]
            
end 
        )
        
from dbo.Table101301    
    
    
drop table dbo.Table101301

 

转载于:https://www.cnblogs.com/chenjunbiao/archive/2008/10/13/1760190.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值