我不得不在我们的数据库中的新列中插入值,但我无法以一致的方式解决这个问题。有很多数据所以手动做任何事都是不可能的。让我上台:
我们有一个名为Occurrence的表和一个名为OccurenceBuckets的表,其中每个引用都引用已分配到的桶。以前这是一个单向引用,但由于各种原因,我们决定将OccurrenceBucket中的引用添加到第一个Occurrence(首先是时间,即)。表现在看起来像这样:
CREATE TABLE Occurrence
OccurrenceID uniqueidentifier,
OccurrenceBucketID uniqueidentifier,
OccurrenceTime datetime,
OccurrenceMessage nvarchar
...other meta data...
CREATE TABLE OccurrenceBucket
OccurrenceBucketID uniqueidentifier,
...other meta data...
FirstOccurrenceID uniqueidentifier,
FirstOccurrenceTime datetime,
FirstOccurrenceMessage nvarchar
我正在寻找一种方法来确定属于某个存储桶的第一个事件,并为FirstOccurrenceID,FirstOccurrenceTime和FirstOccurrenceMessage分配所有mybucket的第一次出现的值。
你们中的任何一位sql-fu专家都有时间帮助我,我所有的尝试都会产生不正确或不完整的事件选择。
答案 0 :(得分:1)
那么,要查找存储桶中的第一个事件,不应该进行以下工作吗?
SELECT TOP 1
OccurranceID, OccurranceTime, OccurranceMessage
FROM Occurance
WHERE
OccurranceBucketID = @OccurranceBucketID
ORDER BY
OccurranceTime ASC
您可以将返回的字段分配给变量,然后相应地更新OccurranceBucket。
注意:“发生”中没有“a”。
答案 1 :(得分:1)
你可以试试这个
DECLARE @Occurrence TABLE(
OccurrenceID INT,
OccurrenceBucketID INT,
OccurrenceTime DATETIME,
OccurrenceMessage VARCHAR(MAX)
)
INSERT INTO @Occurrence (OccurrenceID,OccurrenceBucketID,OccurrenceTime,OccurrenceMessage)
SELECT 1, 1, '01 Jan 2009', 'A'
INSERT INTO @Occurrence (OccurrenceID,OccurrenceBucketID,OccurrenceTime,OccurrenceMessage)
SELECT 2, 1, '02 Jan 2009', 'B'
INSERT INTO @Occurrence (OccurrenceID,OccurrenceBucketID,OccurrenceTime,OccurrenceMessage)
SELECT 3, 1, '03 Jan 2009', 'C'
INSERT INTO @Occurrence (OccurrenceID,OccurrenceBucketID,OccurrenceTime,OccurrenceMessage)
SELECT 4, 2, '04 Jan 2009', 'D'
INSERT INTO @Occurrence (OccurrenceID,OccurrenceBucketID,OccurrenceTime,OccurrenceMessage)
SELECT 5, 2, '05 Jan 2009', 'E'
INSERT INTO @Occurrence (OccurrenceID,OccurrenceBucketID,OccurrenceTime,OccurrenceMessage)
SELECT 6, 2, '06 Jan 2009', 'F'
SELECT * FROM @Occurrence
DECLARE @OccurrenceBucket TABLE(
OccurrenceBucketID INT,
FirstOccurrenceID INT,
FirstOccurrenceTime DATETIME,
FirstOccurrenceMessage VARCHAR(MAX)
)
INSERT INTO @OccurrenceBucket (OccurrenceBucketID) SELECT 1
INSERT INTO @OccurrenceBucket (OccurrenceBucketID) SELECT 2
SELECT * FROM @OccurrenceBucket
UPDATE @OccurrenceBucket
SET FirstOccurrenceID = OccurrenceID,
FirstOccurrenceTime = OccurrenceTime,
FirstOccurrenceMessage = OccurrenceMessage
FROM @OccurrenceBucket oc INNER JOIN
(
SELECT o.*
FROM @Occurrence o INNER JOIN
(
SELECT OccurrenceBucketID,
MIN(OccurrenceID) FirstOccurrenceID
FROM @Occurrence
GROUP BY OccurrenceBucketID
) Mins ON o.OccurrenceID = mins.FirstOccurrenceID
) Vals ON oc.OccurrenceBucketID = Vals.OccurrenceBucketID
SELECT * FROM @OccurrenceBucket
编辑:
UPDATE @OccurrenceBucket
SET FirstOccurrenceID = OccurrenceID,
FirstOccurrenceTime = OccurrenceTime,
FirstOccurrenceMessage = OccurrenceMessage
FROM @OccurrenceBucket oc INNER JOIN
(
SELECT o.*
FROM @Occurrence o INNER JOIN
(
SELECT OccurrenceBucketID,
MIN(OccurrenceTime) FirstOccurrenceTime
FROM @Occurrence
GROUP BY OccurrenceBucketID
) Mins ON o.OccurrenceTime = mins.FirstOccurrenceTime
) Vals ON oc.OccurrenceBucketID = Vals.OccurrenceBucketID
答案 2 :(得分:0)
此答案依赖于OccurrenceTime对于每次出现都是唯一的: -
更新OccBuck设置
OccBuck.FirstOccurrenceID = Occ.OccurrenceID,
OccBuck.FirstOccurrenceTime = Occ.OccurrenceTime,
OccBuck.FirstOccurrenceMessage = Occ.OccurrenceMessage
从
dbo.OccurrenceBucket as OccBuck
内部联接dbo。在OccBuck.OccurrenceBucketID = Occ.OccurrenceBucketID上作为Occ发生
内连接(选择OccurrenceBucketID,
MIN(OccurrenceTime)为'MinOccurrenceTime'
来自dbo.Occurrence
Occ.OccurrenceBucketID = minOcc.OccurrenceBucketID和
作为minOcc on OccurrenceBucketID)
Occ.OccurrenceTime = minOcc.MinOccurrenceTime