c# - 如何对标志 bigint 列设置的 T-SQL 进行分组
问题描述
我正在从事一个搜索功能的效率至关重要的项目。
我有几个标志列(如 c# 中的枚举标志)。搜索这些数据的速度非常快(往返 3 毫秒),但现在我必须进行组计数了。
所以,我有一个包含红色 (1)、白色 (8) 和蓝色 (64) 的项目“A”,因此“颜色”列包含数字 73。
要搜索,我可以用这个搜索带有红色的项目
Declare @colour int
set @colour = 1
Select *
From Items
Where (Colour & @colour) > 0
这很好用。现在我必须对它进行分组(也超级快)
因此,如果我总共有 8 个项目,5 个包含红色,3 个包含白色,7 个包含蓝色,结果将如下所示:
Colour Qty
------------------
1 5
8 3
64 7 ( I don't have to worry about the name )
所以:有什么办法可以把数字 73 按位分成几组?
(第 2 部分:如何将其转换为 Linq to SQL?)
任何建议将不胜感激
谢谢^_^
解决方案
好的 - 我想我已经找到了最好的解决方案:
我尝试了一个 cte 的视图:
with cte as (
select cast(1 as bigint) as flag, 1 pow
union all
select POWER(cast(2 as bigint),pow), pow + 1
from cte
where flag < POWER(cast(2 as bigint),62)
)
, cte2 as (
select flag from cte
union select -9223372036854775808
)
但这太慢了,所以现在我把它变成了一个静态表。我加入了按位“&”:
select Flag, Count(*)
From FlagValues fv
inner join Items i on (fv.Flag & i.Colour)
快很多^_^
推荐阅读
- python-3.x - 无法覆盖詹金斯管道中的python命令
- three.js - three.js 如何以编程方式从数据集中生成平面
- python - 我想从应用程序中抓取所有元素,但我遇到了 selenium python 问题
- multithreading - 如何使用锁定例程和睡眠功能来摆脱同步的显式障碍?
- sql-server - 将 AND (x OR y) 添加到视图的现有 where 子句不起作用
- angular - 带有参数的数组中的打字稿/角度删除/拼接对象
- python - 在 Tensorflow 2.0 中检查张量的值
- python - 无法在 Windows 上为 Py 3.7 安装 pycurl
- reactjs - CreateContext 挂钩不允许渲染任何其他组件
- docker - 删除 .../docker/overlay2 目录下的孤立文件夹(哈希)是否安全