Frage

Ich habe eine Tabelle, die vier Sätze von 25 Spalten in einem BIT-Konzept hat. Eigentlich Feld ist smallint aber es ist entweder 0 oder 1 in seinen Daten.

Hier ist mein Code, dass ein Versuch ist, die Summe für die erste Gruppe von 25 cols zu erhalten.

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

Meine Ausgabe:

(5 row(s) affected)
5
67A16306-B27D-4882-88A2-1146CBAAA8D9

(1 row(s) affected)
4
F94929B9-3DA7-4AA3-96F6-728EF025B21C

Ich kann nicht die Summe zu bekommen, um @LocTotal

TIA

War es hilfreich?

Lösung

Ich denke, Tomalak hat, weshalb ich ihn modded, aber wenn Sie dies tun oder einmal, da all dieser Unordnung:

cbHPILoc1
  + cbHPILoc2 * 2
  + cbHPILoc3 * 4
  + ...
  + cbHPILoc25 * 16777216

ist mühsam und fehleranfällig, warum es nicht einmal tun als Sicht und Test, der gründlich?

Dann nur select total from viewname where reckey = ?.

Andere Tipps

Warum so kompliziert?

SELECT
  RecKey,
  cbHPILoc1
  + cbHPILoc2 * 2
  + cbHPILoc3 * 4
  + ...
  + cbHPILoc25 * 16777216
  AS Total
FROM
  dbo.MyTest_BitMap
WHERE 
  RecKey = @ID
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top