我继承了需要清理的用户数据。它最初是在MS Access数据库中,我需要将其转换为SQL Server。我现在在SQL Server中有这个表,但是我需要帮助的是一列数据。
有许多列,如姓名,电子邮件等,都非常简单。
但是,有一列存储按位数据。此列用于显示用户可以属于的组。通过浏览一些记录,我确定了这些组及其相关数字:
All 1
Air 2
Plants 4
Energy 8
Land 16
Elec 32
Fire 64
Water 128
Cloud 256
Soil 512
Waste 1024
Local 2048
Coast 4096
用户列中的数据可以是2,它们属于Air组,或者当它们属于Air和Plants(2 + 4)时可以是6。
我创建了一个包含用户ID字段的表。
User_ID
All
Air
Plants
Energy
Land
Elec
Fire
Water
Cloud
Soil
Waste
Local
Coast
所以我需要遍历旧表,然后相应地在新表的列中插入相应的true值
因此,如果数据是:
User_ID Name Group
1 Jo Smith 2
2 Carl White 8
我最终会以
结束User_ID All Air Plants Energy ....
1 true
2 true
等等。
但是,由于用户可以属于多个组,因此数据看起来更像是:
Jake C 1552
Jeff H 1556
Cath B 1561
Emma B 1564
Alex G 1572
Alan H 1574
Jo L 1596
Roy A 1600
如何构建新桌子?
我想到了一些东西SELECT * FROM [dbo].[Users] where [Group] & 2 != 0
会开始沿着我需要的方式恢复数据,但我不确定这是否是正确的轨道。
编辑:如果需要,我愿意使用外部代码 - 不一定要用SQL命令/查询来完成
答案 0 :(得分:3)
select user_id
,sign ([Group] & 1 ) as [All]
,sign ([Group] & 2 ) as [Air]
,sign ([Group] & 4 ) as [Plants]
,sign ([Group] & 8 ) as [Energy]
,sign ([Group] & 16 ) as [Land]
,sign ([Group] & 32 ) as [Elec]
,sign ([Group] & 64 ) as [Fire]
,sign ([Group] & 128 ) as [Water]
,sign ([Group] & 256 ) as [Cloud]
,sign ([Group] & 512 ) as [Soil]
,sign ([Group] & 1024) as [Waste]
,sign ([Group] & 2048) as [Local]
,sign ([Group] & 4096) as [Coast]
from [dbo].[Users]
或
select user_id
,sign ([Group] & power(2, 0)) as [All]
,sign ([Group] & power(2, 1)) as [Air]
,sign ([Group] & power(2, 2)) as [Plants]
,sign ([Group] & power(2, 3)) as [Energy]
,sign ([Group] & power(2, 4)) as [Land]
,sign ([Group] & power(2, 5)) as [Elec]
,sign ([Group] & power(2, 6)) as [Fire]
,sign ([Group] & power(2, 7)) as [Water]
,sign ([Group] & power(2, 8)) as [Cloud]
,sign ([Group] & power(2, 9)) as [Soil]
,sign ([Group] & power(2,10)) as [Waste]
,sign ([Group] & power(2,11)) as [Local]
,sign ([Group] & power(2,12)) as [Coast]
from [dbo].[Users]
select u.*
,[All] + [Air] + [Plants] + [Energy] + [Land] + [Elec] + [Fire] + [Water] + [Cloud] + [Soil] + [Waste] + [Local] + [Coast] as cnt
from (select user_id
,sign ([Group] & power(2, 0)) as [All]
,sign ([Group] & power(2, 1)) as [Air]
,sign ([Group] & power(2, 2)) as [Plants]
,sign ([Group] & power(2, 3)) as [Energy]
,sign ([Group] & power(2, 4)) as [Land]
,sign ([Group] & power(2, 5)) as [Elec]
,sign ([Group] & power(2, 6)) as [Fire]
,sign ([Group] & power(2, 7)) as [Water]
,sign ([Group] & power(2, 8)) as [Cloud]
,sign ([Group] & power(2, 9)) as [Soil]
,sign ([Group] & power(2,10)) as [Waste]
,sign ([Group] & power(2,11)) as [Local]
,sign ([Group] & power(2,12)) as [Coast]
from [dbo].[Users]
) u
;