标题: 超级难题挑战 sql高手! [打印本页] 作者: zhangyh123 时间: 2003-09-08 10:27 标题: 超级难题挑战 sql高手! 假设有一个表,记录商品销售情况, 商品唯一编码、名称、价格、
当日销售总数量,销售日期。
现在用一个 sql 语句,找出所有商品连续3天商品销售数量低于指定数值的那些天的日期与商品。
create table sales1 (item_id numeric(8,0) not null ,
item_name varchar(40) not null,
sale_num integer not null ,
sale_date smalldatetime not null)
下表为商品报警表,存放所有商品日销售下限报警数
create table sale_alarm( item_id numeric(8,0) not null ,
alarm_num integer not null )
指定用一个sql 语句完成 销售日期为整日期格式,如 '2003-09-12'作者: cx110 时间: 2003-09-08 11:06 标题: 超级难题挑战 sql高手! 先来一个很蠢的方法:)
select sale_date,item_name
from sales1
where sale_num<n and sale_date='2003-9-1' and
sale_date='2003-9-2' and sale_date='2003-9-3'作者: onlywxw 时间: 2003-09-08 12:07 标题: 超级难题挑战 sql高手! 以下脚本执行的条件仅限于date1和date2间隔为3天有效,n为下限值
select sale_date,item_id, n
where sale_num < n
and sale_date between 'date1' and 'date2'
into temp temptable1;
select item_id,n,count(*) item_count
from temptable
group by item_id,n
order by item_id,n
having count(*) = 3
into temp temptable2;
insert into sale_alarm select item_id,n from temptable2;
如果两个日期的间隔不是3天,而是任意天数的话,我就不知该怎么办了!!!作者: onlywxw 时间: 2003-09-08 13:01 标题: 超级难题挑战 sql高手! sorry!!!
看错了,我以为是informix数据库哪!!!
抱歉,实在抱歉!作者: sybase-bud 时间: 2003-09-09 16:32 标题: 超级难题挑战 sql高手! select A.item_id,A.sale_date,A.sale_num FROM sale1 A,sale_alarm B
WHERE A.item_id=B.item_id and A.sale_num<=B.alarm_num AND
EXISTS(SELECT * FROM sale1 WHERE sale_date=DATEADD(DAY,+1,A.sale_date) AND
sale_num<=B.alarm_num AND item_id=A.item_id AND item_id=B.item_id)
AND
EXISTS(SELECT * FROM sale1 WHERE sale_date=DATEADD(DAY,-1,A.sale_date) AND
sale_num<=B.alarm_num AND item_id=A.item_id AND item_id=B.item_id)作者: buyi 时间: 2003-09-09 17:17 标题: 超级难题挑战 sql高手! 楼上的老兄的方法好像很复杂,在没有环境进行测试时的确有些看不懂。本人提供一法,仅供楼上各位参考:
select a.item_id from sales1 a,sale_alarm b
where a.item_id=b.item_id and a.sale_num<b.sale_num and
(a.sale_date - getdate())>;=3 作者: buyi 时间: 2003-09-09 17:18 标题: 超级难题挑战 sql高手! sorry,少写了函数名,正确的语句如下:
select a.item_id from sales1 a,sale_alarm b
where a.item_id=b.item_id and a.sale_num<b.sale_num and
datediff(a.sale_date - getdate())>;=3作者: sybase-bud 时间: 2003-09-10 07:56 标题: 超级难题挑战 sql高手! 是那样的么,试试就知道了
原帖由 "buyi" 发表:
sorry,少写了函数名,正确的语句如下:
select a.item_id from sales1 a,sale_alarm b
where a.item_id=b.item_id and a.sale_num<b.sale_num and
datediff(a.sale_date - getdate())>;=3
select a.sale_date,a.item_name
from sales1 a,sales1 b ,sales1 c,sale_alarm d
where a.sale_num < d.alarm_num
and b.sale_num < d.alarm_num
and c.sale_num < d.alarm_num
and dateadd(a.sale_date,1)=b.sale_date
and dateadd(a.sale_date,2)=c.sale_date
and dateadd(1.sale_date,1)=c.sale_date作者: solofeng 时间: 2003-09-15 21:43 标题: 超级难题挑战 sql高手! 很难吗?我这个就可以,只不过慢了点,如果写两天语句就可以很快了
select a.sale_date,a.item_name
from sales1 a,sales1 b ,sales1 c,sale_alarm d
where a.sale_num < d.alarm_num
and b.sale_num < d.alarm_num
and c.sale_num < d.alarm_num
and dateadd(a.sale_date,1)=b.sale_date
and dateadd(a.sale_date,2)=c.sale_date
and dateadd(b.sale_date,1)=c.sale_date作者: zhangyh123 时间: 2003-09-16 09:00 标题: 超级难题挑战 sql高手! 不错。 就是因为性能,可能这样的查询无法应用。