0

让我们举个例子来说明我的担忧:
假设我们有一个表(标签),它有两列像这样

UserID -------------------------------- Tag
1 -------------------------------------- SQL
1 -------------------------------------- Select
1 -------------------------------------- DB
2 -------------------------------------- SQL
2 -------------------------------------- Programming
2 -------------------------------------- Code
2 -------------------------------------- Software
3 -------------------------------------- Code
4 -------------------------------------- SQL
4 -------------------------------------- Code

我需要根据 UserID 计算每个标签的 DISTINCT 共现次数

所以,输出应该是这样的(按共现顺序排序):

Tag -------------------------------- Co-occurrences
---------------------------------------------
SQL --------------------------------------- 5
Programming ------------------------------- 3
Code -------------------------------------- 3
Software ---------------------------------- 3
Select ------------------------------------ 2
DB ---------------------------------------- 2

这只是一个例子。。

我怎样才能做出可以做到这一点的 Select 语句?

我想出了一种方法,但只有一个特定的标签:

SELECT count (distinct (Tag)) - 1 as Co_occurrences
FROM Tags
WHERE Tag is NOT NULL and UserID in
    (   SELECT UserID
        FROM Tags 
        where tag = 'SQL')

是否可以更改上述语句以使其对表中的所有标签通用?

4

2 回答 2

1
SELECT t2.tag, count (distinct (t1.Tag)) - 1 as Co_occurrences
FROM Tags t1 inner join 
    Tags t2 on t1.UserId = t2.UserId 
GROUP BY t2.tag    
ORDER BY count (distinct (t1.Tag)) desc      
于 2013-02-07T21:33:47.563 回答
1

AGROUP BY是您正在寻找的内容:

SELECT 
    UserID,
    Tag,
    COUNT(DISTINCT Tag) - 1 AS Co_occurrences
FROM Tags
GROUP BY UserID, Tag
ORDER BY UserID, Tag

编辑:如评论中所述,以上内容并未回答问题。我稍微改进了@OSA-E 的答案,以解释-1计数后在做什么。

SELECT
  [t1].[Tag],
  COUNT(DISTINCT [t2].[Tag]) AS [Co_occurrences]
FROM [Tags] [t1]
INNER JOIN [Tags] [t2] ON [t1].[UserID] = [t2].[UserID]
WHERE [t1].[Tag] <> [t2].[Tag]
GROUP BY [t1].[Tag]
ORDER BY [Co_occurrences] DESC

这是小提琴

于 2013-02-07T21:21:29.477 回答