SQL Server - 交换2行

时间:2015-12-10 21:38:13

标签: sql-server

我为一个我参加过的联赛(足球)创建了一个体育数据库,我遇到了一个问题。当我最初设置存放的麻袋时,我把它们放在错误的团队中。现在,我正在尝试编写一个查询来交换麻袋和麻袋的价值,因为除了麻袋数据之外,我们每行还有更多的数据。

编辑:我应该注意到我的游戏数量非常多。所以手动更新可能不会削减它。

+------------+------------+----------+-------+----------+
| fk_eventID |  teamName  | homeAway | sacks | sackyrds |
+------------+------------+----------+-------+----------+
|    123     |   London   |    0     |   3   |    9     |
|    123     | St Thomas  |    1     |   2   |    5     |
+------------+------------+----------+-------+----------+

我有什么可以让它看起来如下吗?基本上,我需要使用Row2中的值来交换Row1中的麻袋和麻袋。

+------------+------------+----------+-------+----------+    
| fk_eventID |  teamName  | homeAway | sacks | sackyrds |
+------------+------------+----------+-------+----------+
|    123     |   London   |    0     |   2   |    5     |
|    123     | St Thomas  |    1     |   3   |    9     |
+------------+------------+----------+-------+----------+

3 个答案:

答案 0 :(得分:1)

除非你有大量的实例,这是一个问题,你最好的选择只是几个直接的更新。

library(data.table)
setkey(setDT(data)[, seconds2 := seconds], seconds, seconds2)
lookup <- data[, .(seconds, seconds2 = seconds + int)]
res <- foverlaps(lookup, data, which = TRUE)[, values := data$values[yid]]
res[, .(SumValues = sum(values)), by = .(SecInterval = xid)]
#     SecInterval SumValues
#  1:           1         1
#  2:           2         2
#  3:           3         3
#  4:           4         3
#  5:           5         3
#  6:           6         2
#  7:           7         1
#  8:           8         2
#  9:           9         1
# 10:          10         2
# 11:          11         3
# 12:          12         3
# 13:          13         2
# 14:          14         2
# 15:          15         1
# 16:          16         0
# 17:          17         0
# 18:          18         0
# 19:          19         0
# 20:          20         0

答案 1 :(得分:0)

手动更新可能最安全。那就是说,

CREATE TABLE dbo.Teams(
    ID int IDENTITY(1,1) NOT NULL,
    Team nvarchar(50) NOT NULL,
    Sacks int NOT NULL,
    SackYards int NOT NULL
) 
GO

SET IDENTITY_INSERT [dbo].[Teams] ON 
GO
INSERT [dbo].[Teams] ([ID], [Team], [Sacks], [SackYards]) VALUES (1, N'Bears', 5, 5)
GO
INSERT [dbo].[Teams] ([ID], [Team], [Sacks], [SackYards]) VALUES (2, N'Chickens', 2, 2)
GO
INSERT [dbo].[Teams] ([ID], [Team], [Sacks], [SackYards]) VALUES (3, N'ChickenHawks', 10, 10)
GO
SET IDENTITY_INSERT [dbo].[Teams] OFF
GO

select * 
from teams ex
join teams new on 
    (ex.Team = 'Bears' and new.Team = 'Chickens')
    or
    (ex.Team = 'Chickens' and new.Team = 'Bears')

update ex
    set Sacks = new.Sacks, SackYards = new.SackYards
from teams ex
join teams new on 
    (ex.Team = 'Bears' and new.Team = 'Chickens')
    or
    (ex.Team = 'Chickens' and new.Team = 'Bears')

select * 
from teams

答案 2 :(得分:0)

略有不同的方法。将更新语句生成为字符串,然后复制并粘贴它们以单独执行...

-- Update for away teams
select 'UPDATE [TableName] SET sacks = ' + CAST(T2.sacks as VARCHAR(100)) + ', sackyrds = ' + CAST(T2.sackyrds as VARCHAR(100)) + ' WHERE fk_eventID = ' + CAST(T1.fk_eventID as VARCHAR(100)) + ' AND homeAway = 0'
from [TableName] T1
inner join [TableName] T2 on T1.fk_eventID = T2.fk_eventID and T2.homeAway = 1
where T1.homeAway = 0

union

-- Update for home teams
select 'UPDATE [TableName] SET sacks = ' + CAST(T2.sacks as VARCHAR(100)) + ', sackyrds = ' + CAST(T2.sackyrds as VARCHAR(100)) + ' WHERE fk_eventID = ' + CAST(T1.fk_eventID as VARCHAR(100)) + ' AND homeAway = 1'
from [TableName] T1
inner join [TableName] T2 on T1.fk_eventID = T2.fk_eventID and T2.homeAway = 0
where T1.homeAway = 1

第一部分选择所有客场球队(homeAway = 0)并加入同一桌面以获得该赛事的主场。然后它为该数据生成UPDATE语句。然后工会重复,但主队加入了。

注意:我没有对此脚本进行过测试,因此很可能您可能需要修复其中的内容,但希望您能得到一般的想法。

我还假设每个fk_eventID只有2行(homeAway = 0或1)。