首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

如何写sql查询

2012-12-17 
怎么写sql查询好久好久没有碰电脑了……一个库存表,设有最大库存值和最小库存值。要求找出高于最高库存或者低

怎么写sql查询
好久好久没有碰电脑了……

一个库存表,设有最大库存值和最小库存值。要求找出高于最高库存或者低于最低库存的商品,并说明是高还是低。

表结构:
商品ID,最高库存,最低库存,当前库存
0001    500    200       10000
0002    400    100        2
0003    5000   100        452
……

要求结果:
商品ID,当前库存,  判断类型
0001    10000    高于最大库存
0012    2        低于最小库存
[最优解释]
赶紧结贴了,多给我点分,谢谢
[其他解释]
null
[其他解释]

SELECT  商品ID ,
         当前库存 ,
         '高于最大库存' 判断类型
 FROM    TB
 WHERE   EXISTS ( SELECT 1
                  FROM   TB b
                  WHERE  TB.当前库存 > b.最高库存
                         AND b.商品id = TB.商品id )
 UNION ALL 
 SELECT  商品ID ,
         当前库存 ,
         '低于最小库存' 判断类型
 FROM    TB
 WHERE   EXISTS ( SELECT 1
                  FROM   TB b
                  WHERE  TB.当前库存 < b.最高库存
                         AND b.商品id = TB.商品id )
                         
[其他解释]

declare @T table 
(商品ID varchar(4),最高库存 int,最低库存 int,当前库存 int)
insert into @T
select '0001',500,200,10000 union all
select '0002',400,100,2 union all
select '0003',5000,100,452

select 
商品ID,当前库存,
判断类型='高于最大库存' 
from @T where 当前库存>最高库存 union all
select 
商品ID,当前库存,
判断类型='低于最小库存' 
from @T where 当前库存<最低库存
order by 商品ID

/*
商品ID 当前库存        判断类型
---- ----------- ------------
0001 10000       高于最大库存
0002 2           低于最小库存
*/

[其他解释]

--创建数据开始
if(object_id('a')is not null) drop table a
go


create table a
(
GoodID varchar(50),
maxstore int,
minstore int,
RealStore int
)
go
insert into a
select '0001',500,200,10000 union all
select '0002',400,100,2 union all
select '0003',5000,100,452
--创建数据结束
--语句
select GoodID,RealStore,case when realStore>maxStore then '高于最大库存' when realStore <minstore then '低于最小库存' else '正常库存'end as judge
from a 

--结果展示.
/*
GoodID                                             RealStore   judge
-------------------------------------------------- ----------- ------------
0001                                               10000       高于最大库存
0002                                               2           低于最小库存
0003                                               452         正常库存

(3 行受影响)


*/


[其他解释]
毫秒之差,错失沙发,看来我真是老了..
[其他解释]
别这样嘛叶子姐姐....我要是造数据,早就在你之后了
[其他解释]
引用:
别这样嘛叶子姐姐....我要是造数据,早就在你之后了


 现在SQL 版有点冷清呀
[其他解释]
时间一样    对字符串来讲DBA_Huangzj>maco_wang
[其他解释]
就没有简单的一句话就搞定的吗?比如 case when<XXX then XXX这样的语句?
[其他解释]

select 商品ID,当前库存,
(case when 当前库存>最高库存 then '高于最大库存' when 当前库存<最低库存 then '低于最小库存' end)as 判断类型 
from tableName
where 当前库存>最高库存 or 当前库存<最低库存 

[其他解释]

-->测试数据
declare @tableName table(商品ID varchar(5),最高库存 int,
最低库存 int,当前库存 int)
insert into @tableName
select '0001',500,200,10000 union all
select '0002',400,100,2 union all
select '0003',5000,100,452

-->查询语句
select 商品ID,当前库存,
(case when 当前库存>最高库存 then '高于最大库存' 
      when 当前库存<最低库存 then '低于最小库存' 
 end)as 判断类型 


from @tableName
where 当前库存>最高库存 or 当前库存<最低库存 

-->查询结果
商品ID  当前库存        判断类型
----- ----------- ------------
0001  10000       高于最大库存
0002  2           低于最小库存

(2 行受影响)


[其他解释]
厉害。。。还没打开,就有人发了。
[其他解释]
楼上的大牛,这种简单的,你们不会让着点啊。
[其他解释]
null

热点排行
Bad Request.