English 中文(简体)
单一或多重的INSERT基于SlectTed的数值
原标题:Single or multiple INSERTs based on values SELECTed
  • 时间:2009-08-25 07:56:45
  •  标签:

我们把我们十尼斯法院的资料从我们的KQQ数据库中摘录为一份简单的成果表,以帮助我们树立法院使用情况的情景。 它只是简单直截了当,只有时间超过一小时。

目前,每张书记本都使我们的成绩表一行。 每一行都有一个起步、期限和法院号码。 我们愿将这一表格直接描绘成一个电子表格或纸张表,这样我们就可以看到我们的法院被记上了多少小时,以及当天的几小时。

目前,我们的问题是:

INSERT INTO Results (year, month, day, hour, duration, court)
SELECT DATEPART (yy, b.StartDateTime),
       DATEPART (mm, b.StartDateTime),
       DATEPART (dd, b.StartDateTime),
       DATEPART (hh, b.StartDateTime),
       a.Duration,
       a.Court
FROM Bookings b
INNER JOIN Activities a
ON b.ActivityID = a.ID

我们的问题是,在2、3或3小时以上的账簿上只有一行,也就是说,在预订的第一小时。 这是因为,账龄数据涵盖了账龄长度。 我们可以就数据进行一些后处理,以达到我们的目的,但是,如果能够在我们的Kingry中做到这一点,就会更容易。

可以通过某种方式修改这一询问,根据期限(可以是1、2、3、......小时)将适当数目的行文列入结果表,每次期限为1。 因此,从9am开始的3小时记账,结果表将分3行,1排在9am,1排在10am,1排在11am,每段1小时。

因此,在成果表中,没有以下段落:

Year, Month, Day, Hour, Duration, Court
2009,    08,  25,   09,        3,     1

我们取得了以下进展:

Year, Month, Day, Hour, Duration, Court
2009,    08,  25,   09,        1,     1
2009,    08,  25,   10,        1,     1
2009,    08,  25,   11,        1,     1

这将使把成果表绘制成电子表格更加容易。

UPDATE 2009-08-25: 当然,正如第一批答复所显示的那样,这只是一个问题。 罚款。

UPDATE 2009-08-26: 曾被双管齐下,没有机会尝试拟议的解决办法。 希望尽快这样做,并将根据结果选择答案。

UPDATE 2009-08-27: 最后,有机会尝试解决办法。 愤怒和共同提出解决办法的表格是表面开的。 尤其是利用交叉工作来制作这种表格。 这可能是做事的更清洁、更灵活的方式。

然而,最后,我采用了Aaaa语解决办法,涉及旗帜和简单的算法。 我确实加强了它,把他的算法总结为时段,以便保持循环,直到没有时间,而且有1人离开。 这是迅速和容易实施的。 它还强调,我们确实有大约10小时的预订,因此我不需要硬编码。

我应该指出,我把杰夫关于最长期限的想法纳入 lo中,而不是我最初的将项目定在期限和幅度上的想法;1. 略微减少代码。

最佳回答

这并非三重。 第一,你需要另一栏“Flag”,即:

INSERT INTO Results (year, month, day, hour, duration, court, Flag)
SELECT DATEPART (yy, b.StartDateTime),
       DATEPART (mm, b.StartDateTime),
       DATEPART (dd, b.StartDateTime),
       DATEPART (hh, b.StartDateTime),
       a.Duration,
       a.Court,
       0
FROM Bookings b
INNER JOIN Activities a
ON b.ActivityID = a.ID

您需要几次处理这些询问:

-- Copy all rows with duration > 1 and set the flag to 1
insert into results(year, month, day, hour, duration, court, Flag)
select year, month, day, hour+1, duration-1, court, 1
from result
where duration > 1
;
-- Set the duration of all copied rows to 1
update result
set duration = 1
where flag = 0 and duration > 1
;
-- Prepare the copies for the next round
update result
set flag = 0
where flag = 1

这将为每个<代码>duration > 1增加一个条目。 我的猜测是,你可以分配一个法庭,时间超过8小时,因此,你只需要管理这三八次,以安抚所有法院。

问题回答

www.un.org/Depts/DGACM/index_spanish.htm 纠正缺失小时计算方法:

设立一个单独的栏目临时表格,nn-(我假定最大预订时间为8小时)。

create table #t
(id int
,addHour int
)

insert #t
select 1,0
union all select 2,0
union all select 2,1
union all select 3,0
union all select 3,1
union all select 3,2
union all select 4,0
union all select 4,1
union all select 4,2
union all select 4,3
union all select 5,0
union all select 5,1
union all select 5,2
union all select 5,3
union all select 5,4
union all select 6,0
union all select 6,1
union all select 6,2
union all select 6,3
union all select 6,4
union all select 6,5
union all select 7,0
union all select 7,1
union all select 7,2
union all select 7,3
union all select 7,4
union all select 7,5
union all select 7,6
union all select 8,0
union all select 8,1
union all select 8,2
union all select 8,3
union all select 8,4
union all select 8,5
union all select 8,6
union all select 8,7

您可以证实,临时表格的浏览量正确无误。

select id, count(1)  
from #t
group by id
order by id

请加入临时表格:

INSERT INTO Results (year, month, day, hour, duration, court)
SELECT DATEPART (yy, b.StartDateTime),
       DATEPART (mm, b.StartDateTime),
       DATEPART (dd, b.StartDateTime),
       DATEPART (hh, b.StartDateTime) + addHour,
       1 AS Duration,
       a.Court 
FROM Bookings b
INNER JOIN Activities a
ON b.ActivityID = a.ID
INNER JOIN #t AS t
ON t.id = a.Duration

<>strong>EDIT - 说明这项工作如何进行

在加入表格时,源表中各加权组合的产出中各加一行,符合合并标准。

I m将临时表格改为“多”图书和活动的原始结果,按记账时间的长短乘以。 只有在全时进行记账的情况下才能做到这一点。

如果你想更清楚地看到这一点,在编号上增加一个第二栏,专门确定每一行,并将其列入产出结果:

create table #t
(id int
,unique_id int identity
)

INSERT #t (id)
select 1
union all select 2
... etc

SELECT DATEPART (yy, b.StartDateTime),
       DATEPART (mm, b.StartDateTime),
       DATEPART (dd, b.StartDateTime),
       DATEPART (hh, b.StartDateTime) + addHour,
       1 AS Duration,
       a.Court,
       t.unique_id
FROM Bookings b
INNER JOIN Activities a
ON b.ActivityID = a.ID
INNER JOIN #t AS t
ON t.id = a.Duration

这应当澄清,所设定的结果中的每一行都是用单一有效组合的书簿、活动和编号来制作的。

对您的原始内容稍加修改,如果您介绍integers table (或VIEW),作为系列发电机:

INSERT INTO Results (year, month, day, hour, duration, court)
SELECT DATEPART (yy, b.StartDateTime),
       DATEPART (mm, b.StartDateTime),
       DATEPART (dd, b.StartDateTime),
       DATEPART (hh, b.StartDateTime) + (a.Duration - i.iii - 1)
       1,
       a.Court
FROM Bookings b
INNER JOIN Activities a
  ON b.ActivityID = a.ID
INNER JOIN Integers999 i       -- N.B.: Integers999 (iii INT), all numbers 0 .. 999
  ON a.Duration > i.iii;       -- So, a true Duration of 1 appears once, of 2 twice ...

您可考虑在“成绩”表上添加一个INSTEAD OF INSERT的触发点,使每台时间超过一小时。 这确实增加了复杂性,但可能是一种合理的方法,因为这类似于大量远洋班轮运输系统。

我没有机会对此进行反驳,但正如你一样:

DECLARE @maxDuration    INTEGER
DECLARE @curDuration    INTEGER

SELECT @MaxDuration = SELECT MAX(Duration) FROM Activities
SET @curDuration = 1

WHILE @curDuration <= @MaxDuration
BEGIN
    INSERT INTO Results (year, month, day, hour, duration, court)
    SELECT DATEPART (yy, b.StartDateTime),
           DATEPART (mm, b.StartDateTime),
           DATEPART (dd, b.StartDateTime),
           DATEPART (hh, b.StartDateTime) + @curDuration - 1,
           a.Duration,
           a.Court
    FROM Bookings b
    INNER JOIN Activities a
    ON b.ActivityID = a.ID
    WHERE a.Duration <= @MaxDuration

    SET @curDuration = @curDuration + 1
END




相关问题
热门标签