首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用SQL查询的多标签分组

使用SQL查询的多标签分组
EN

Stack Overflow用户
提问于 2017-07-26 12:00:47
回答 2查看 491关注 0票数 1

我希望能够浏览多标签条目的列表,在其中我可以找到常见标签组的数据。我有一个表,结构如下:

代码语言:javascript
运行
复制
---------------------------------------------------------------------
|GameID |   Title   |Label1 |Label2 |Label3 |Label4 |...    |LabelN |
|-------|-----------|-------|-------|-------|-------|-------|-------|
|1      |   A       |   1   |   0   |   1   |   0   |   ... |   1   |   
|2      |   B       |   1   |   1   |   0   |   1   |   ... |   0   |
|3      |   C       |   0   |   0   |   1   |   1   |   ... |   0   |
|4      |   D       |   1   |   0   |   0   |   0   |   ... |   1   |
|...    |   ...     |   ... |   ... |   ... |   ... |   ... |   ... |
---------------------------------------------------------------------

如果某个条目的标签下有1,则表示该条目与该标签相关联。否则,它不会与该标签相关联。例如,游戏A具有与其关联的标签"Label1“、"Label3”、...和"LabelN“。

现在,以下面的SQL查询为例:

代码语言:javascript
运行
复制
SELECT GameID, Title
FROM GameTagsBinary
WHERE "Gun Customization" = 1 AND "Zombies" = 1

此查询将返回下表:

代码语言:javascript
运行
复制
-------------------------------------
|GameID |   Title                   |
|-------|---------------------------|
|263070 |   Blockstorm              |
|209870 |   Blacklight: Retribution |
|436520 |   Line of Sight           |
-------------------------------------

我想要的是一个查询,它将遍历每一列,Label1到LabelN,并打印出与这些标签相关的游戏数量。

代码语言:javascript
运行
复制
-------------------------------------------------
|Combination                |   NumberOfGames   |
|---------------------------|-------------------|   
|Label1 + Label2            |   5               |
|Label1 + Label3            |   11              |
|Label1 + Label4            |   9               |
|...                        |   ...             |
|Gun Customization + Zombies|   3               |
|...                        |   ...             |
|LabelN + Label(N-1)        |   7               |
-------------------------------------------------
EN

回答 2

Stack Overflow用户

发布于 2017-07-27 10:14:34

尝试下面的逻辑(用实际的表名替换YOUR_TABLE_NAME )

您可能需要创建一个存储过程来运行SQL脚本以获取结果。

顺便说一句,这段代码是为SQL server编写的,如果你使用其他数据库,有些代码会有所不同。

查找列的不同组合的代码

代码语言:javascript
运行
复制
select result.* into #temp from (
select ROW_NUMBER() over (order by a.name) as id, a.name as a_name, b.name as b_name from 
(select name from sys.columns where object_id=object_id('YOUR_TABLE_NAME')) a
 cross join
(select name from sys.columns where object_id=object_id('YOUR_TABLE_NAME')) b
where a.name <> b.name ) result

select a_name, b_name into #combination from #temp temp1 where not exists(select 1 from #temp temp2 where temp1.a_name = temp2.b_name and temp1.b_name = temp2.a_name and temp1.id > temp2.id)

游标遍历组合并插入到另一个临时表

代码语言:javascript
运行
复制
create table #result
(
    Combination varchar(100),
    NumberOfGames int
)

DECLARE @a_name varchar(100);
DECLARE @b_name varchar(100);
DECLARE @combination_string varchar(100);
DECLARE @count int;
DECLARE @count_sql varchar(1000);
DECLARE combo_cursor CURSOR FOR select a_name, b_name FROM #combination
OPEN combo_cursor
FETCH NEXT FROM combo_cursor INTO @a_name, @b_name;

WHILE @@FETCH_STATUS = 0 
BEGIN
    set @combination_string = @a_name + ' + ' + @b_name

    set @sql = 'select @count = count(*) from YOUR_TABLE_NAME where ' + @a_name + ' = 1 and ' + @b_name + ' = 1'

    EXEC SP_EXECUTESQL @sql

    insert into #result (Combination, NumberOfGames)
    values (@combination_string, @count)

    FETCH NEXT FROM combo_cursor INTO @a_name, @b_name;
END

CLOSE combo_cursor;
DEALLOCATE combo_cursor;

你的最终结果

代码语言:javascript
运行
复制
select * from #result

在执行后删除临时表

代码语言:javascript
运行
复制
drop table #temp
drop table #combination
drop table #result
票数 1
EN

Stack Overflow用户

发布于 2017-07-26 12:39:36

您是否尝试过在select语句中使用count()参数?

即。

Select count(*) From GameTagsBinary Where Label1 = "Gun Customization" AND Label2 = "Zombies"

然而,我不确定如何一次完成所有的标签,我假设可以在一个包含大量连接的大规模查询中做到这一点。很抱歉,我不能提供更多的帮助,但这应该能提供一点帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45317224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档