【发布时间】:2009-05-01 16:38:43
【问题描述】:
我有一个表,在 BIT 概念中有 4 组 25 列。实际上字段是 smallint,但它的数据中要么是 0 要么是 1。
这是我的代码,它试图获取第一组 25 列的总数。
Declare @rows int
, @ID uniqueidentifier
, @LocTotal bigint
select @rows = ( select count(*) from #t1 )
while @rows > 0
begin
print @rows
-- get that rowID
select @ID = (select top 1 recid from #t1)
select @LocTotal =
(select top 1
case when cbHPILoc1 = 1 then 1 else 0 end +
case when cbHPILoc2 = 1 then 2 else 0 end +
case when cbHPILoc3 = 1 then 4 else 0 end +
< snip >
case when cbHPILoc25 = 1 then 16777216 else 0 end
as Total
from dbo.MyTest_BitMap
where RecKey = @ID
)
print @ID
print @LocTotal
我的输出:
(5 row(s) affected)
5
67A16306-B27D-4882-88A2-1146CBAAA8D9
(1 row(s) affected)
4
F94929B9-3DA7-4AA3-96F6-728EF025B21C
我没能把总数发到@LocTotal
TIA
【问题讨论】:
-
代码不完整,所以很多都没有意义......你在MyTest_BitMap中真的有RecKey与#t1中的recid匹配的记录吗?
-
这是我总共 25 列中的前 5 列 cbHPILocN 0 0 1 0 0 所以我希望 @TotLoc >= 4 但我没有得到任何回报?
-
@__Stephen:你想做什么?找出给定 RecKey 的位图?给定的 RecKey 是否不止一行?你想找到那个总和吗?
标签: sql sql-server tsql bit-manipulation