简单表
Date |ChannelID
01/01/2001 | 1001
02/01/2001 | 1001
03/01/2001 | 1001
04/01/2001 | 1001
05/01/2001 | 1001
06/01/2001 | 1001
我需要在每一行随机将ChannelID值替换为1001、2001或2002。
我尝试执行以下操作,但似乎没有给我随机输入的内容,由于某种原因错过了最后一个值。
update A
set ChannelID = (
SELECT TOP 1 b.q
FROM (
SELECT 1001 AS q
UNION ALL
SELECT 2001
UNION ALL
SELECT 2002
) b
CROSS APPLY (
SELECT TOP 1 b2.q2
FROM (
SELECT 1001 AS q2
UNION ALL
SELECT 2001
UNION ALL
SELECT 2002
) b2
WHERE b.q <> b2.q2
ORDER BY newid()
) b2
ORDER BY NEWID()
)
From A
是否有一种不使用临时表/变量表的优雅方法?
抱歉,如果这是类似的帖子,但不使用临时表等就找不到任何内容。
答案 0 :(得分:2)
您可以:
update t
set t.ChannelID = tt.q
from table t cross apply
( select top (1) tt.q
from ( values (1001), (2001), (2002)
) tt (q)
where cast(tt.q as varchar(255)) <> t.date
order by newid()
) tt;
答案 1 :(得分:2)
尝试以下操作:
ax.plot(da.lon.T, da.data.T)
加权随机性
declare @tab table ([Date] date, ChannelID int)
insert into @tab
select '01/01/2001', 1001 union
select '02/01/2001', 1001 union
select '03/01/2001', 1001 union
select '04/01/2001', 1001 union
select '05/01/2001', 1001 union
select '06/01/2001', 1001
select [Date], CASE ascii(newid()) % 3 WHEN 0 THEN '1001' WHEN 1 THEN '2001' ELSE '2002' END AS ChannelID
FROM @tab