table:
--duedate timestamp
--submissiondate timestamp
--blocksreq numeric
--file clob
--email varchar2(60)
每个条目都是一个文件,它将使blocksreq完成。每天分配8个区块(但稍后可以修改)。在我插入表格之前,我想确保在NOW()和@duedate
的时间范围内有足够的块来完成它我在考虑以下问题,但我认为我做错了:
R1 = select DAY(), @blocksperday - sum(blocksreq) as free
from table
where @duedate between NOW() and @duedate
group by DAY()
order by DAY() desc
R2 = select sum(a.free) from R1 as a;
if(R2[0] <= @blocksreq){ insert into table; }
原谅部分伪代码。
SQL FIDDLE:http://sqlfiddle.com/#!2/5bda5
警告:我的sql小提琴有垃圾代码......因为我不知道如何制作大量的测试用例。也没有将duedate设置为NOW()+ 5天
答案 0 :(得分:1)
这样的东西? (不确定如何处理部分日子,因此忽略了那部分)
CREATE TABLE `DatTable` (
`duedate` datetime DEFAULT NULL,
`submissiondate` datetime DEFAULT NULL,
`blocksreq` smallint(6) DEFAULT NULL
)
SET @duedate:='2012-10-15';
SET @submissiondate:=CURRENT_TIMESTAMP;
SET @blocksreq:=5;
INSERT INTO DatTable(duedate,submissiondate,blocksreq)
SELECT @duedate,@submissiondate,@blocksreq
FROM DatTable AS b
WHERE duedate > @submissiondate
HAVING COALESCE(SUM(blocksreq),0) <= DATEDIFF(@duedate,@submissiondate)*8-@blocksreq;