gpt4 book ai didi

mysql - SQL分类

转载 作者:可可西里 更新时间:2023-11-01 06:48:08 27 4
gpt4 key购买 nike

我有一个系统可以跟踪用户查看的文档。每个文档都有它的 ID 和它所属的集群。我的系统跟踪 session ID 和查看次数。我现在想构建一个 SQL 查询,它会给我两列 - session ID 和分类集群。分类算法很简单:

1. select all sessions
2. for each session S
I. prepare an accumulator ACC for clusters
II. select the clusters of viewed documents for this session
III. for each cluster C accumulate the cluster count ( ACC[C]++ )
IV. find the maximum in the ACC. That is the cluster that the session was classified to

表结构如下,我用的是MySQL 5.5.16:

session

+-------+-----------+--------------------+
| ID | sessionID | classified_cluster |
+-------+-----------+--------------------+

session 文档

+-------+-----------+------------+
| ID | sessionID | documentID |
+-------+-----------+------------+

集群

+-------+-------+
| ID | label |
+-------+-------+

集群文档

+-------+-----------+------------+
| ID | clusterID | documentID |
+-------+-----------+------------+

所以基本上,我想为每个 session 选择集群,计算每个集群在查看文档中的出现次数,并找到最大出现次数。那么出现次数最多的集群的 ID 就是 session 的结果,因此最终结果集包含 session ID 和出现次数最多的集群:

结果

+-----------+-----------------------+
| sessionID | classifiedIntoCluster |
+-----------+-----------------------+

我设法通过以下查询获取每个 session (步骤 2/II.)的已查看文档的集群:

SELECT SD.session_id, CD.cluster_id 
FROM cluster_document AS CD
INNER JOIN session_document AS SD
ON CD.document_id = SD.document_id
WHERE session_id IN (SELECT session_id FROM session)

我无法弄清楚其余部分。这甚至可以用于嵌套的 SELECT 查询吗?我应该使用光标吗?如果是的话,有人可以用光标展示一个例子吗?任何帮助都感激不尽。

编辑 #1:添加了 C# 实现、MySQL 转储和预期结果

C# 实现

    private void ClassifyUsers() {
int nClusters = Database.SelectClusterCount(); //get number of clusters
DataSet sessions = Database.SelectSessions(); //get all sessions
foreach (DataRow session in sessions.Tables[0].Rows) { //foreach session
int[] acc = new int[nClusters]; //prepare an accumulator for each known cluster
string s_id = session["session_id"].ToString();
DataSet sessionClusters = Database.SelectSessionClusters(s_id); //get clusters for this session

foreach (DataRow cluster in sessionClusters.Tables[0].Rows) { //for each cluster
int c = Convert.ToInt32(cluster["cluster_id"].ToString()) - 1;
acc[c]++; //accumulate the cluster count
}

//find the maximum in the accumulator -> that is the most relevant cluster
int max = 0;
for (int j = 0; j < acc.Length; j++) {
if (acc[j] >= acc[max]) max = j;
}
max++;
Database.UpdateSessionCluster(s_id, max); //update the session with its new assigned cluster
}
}

表结构、测试数据和预期结果

Table structure and test data

Expected result

编辑 #2:添加了一个较小的数据集和进一步的算法演练

这是一个较小的数据集:

session

session id    |  cluster
abc 0
def 0
ghi 0
jkl 0
mno 0

集群

cluster_id  | label
1 A
2 B
3 C
4 D
5 E

SESSION_DOCUMENT

id      | session_id    |   document_id
1 abc 1
2 def 5
3 jkl 3
4 ghi 4
5 mno 2
6 def 2
7 abc 5
8 ghi 3

CLUSTER_DOCUMENT

id      | cluster_id    |   document_id
1 1 2
2 1 3
3 2 5
4 3 5
5 3 1
6 4 3
7 5 2
8 5 4

算法详解

第 1 步:获取 session 查看的文档的集群

session_id  |  cluster_id   | label     | document_id   
abc 3 C 1
abc 2 B 5
abc 3 C 5
-----
def 2 B 5
def 3 C 5
def 1 A 2
def 5 E 2
----
ghi 5 E 4
ghi 1 A 3
ghi 4 D 3
----
jkl 1 A 3
jkl 4 D 3
----
mno 1 A 2
mno 5 E 2

第 2 步:计算簇的出现次数

session_id |    cluster_id  | label |   occurrence
abc 3 C 2 <--- MAX
abc 2 B 1
----
def 2 B 1
def 3 C 1
def 1 A 1
def 5 E 1 <--- MAX
----
ghi 5 E 1
ghi 1 A 1
ghi 4 D 1 <--- MAX
----
jkl 1 A 1
jkl 4 D 1 <--- MAX
----
mno 1 A 1
mno 5 E 1 <--- MAX

第 3 步(最终结果):为每个 session 找到最大发生的簇(见上文)并构建最终结果集(session_id、cluster_id):

session_id |    cluster_id  
abc 3
def 5
ghi 4
jkl 4
mno 5

编辑 #3:已接受的答案说明

给出的两个答案都是正确的。他们都提供了解决问题的方法。我给了 Mosty Mostacho 可接受的答案,因为他首先交付了解决方案,并提供了带有 VIEW 的解决方案的另一个版本。来自 mankuTimma 的解决方案与 Mosty Mostacho 的解决方案质量相同。因此,我们有两个同样好的解决方案,我只是选择了 Mosty Mostacho,因为他是第一个。

感谢他们的贡献。.

最佳答案

好吧,我对如何在有很多相等的情况下选择一个事件表示怀疑,但查看 C# 代码似乎这个选择是不确定的。

现在,给定样本数据,第 2 步实际导致:

+------------+------------+-------+------------+
| SESSION_ID | CLUSTER_ID | LABEL | OCCURRENCE |
+------------+------------+-------+------------+
| abc | 3 | C | 2 |
| def | 1 | A | 1 |
| def | 2 | B | 1 |
| def | 3 | C | 1 |
| def | 5 | E | 1 |
| ghi | 1 | A | 1 |
| ghi | 4 | D | 1 |
| ghi | 5 | E | 1 |
| jkl | 1 | A | 1 |
| jkl | 4 | D | 1 |
| mno | 1 | A | 1 |
| mno | 5 | E | 1 |
+------------+------------+-------+------------+

因此,继续处理这些数据,我得到了该 session ID 的 session_id 和 max(cluster_id),结果是:

+------------+------------+
| SESSION_ID | CLUSTER_ID |
+------------+------------+
| abc | 3 |
| def | 5 |
| ghi | 5 |
| jkl | 4 |
| mno | 5 |
+------------+------------+

max(cluster_id) 就是用来执行非确定性选择的。这是查询:

select s1.session_id, max(s1.cluster_id) as cluster_id from (
select sd.session_id, cd.cluster_id, count(*) as Occurrence
from session_document sd
join cluster_document cd
on sd.document_id = cd.document_id
join cluster c
on c.cluster_id = cd.cluster_id
group by sd.session_id, cd.cluster_id, c.label
) as s1
left join (
select sd.session_id, count(*) as Occurrence
from session_document sd
join cluster_document cd
on sd.document_id = cd.document_id
join cluster c
on c.cluster_id = cd.cluster_id
group by sd.session_id, cd.cluster_id, c.label
) as s2
on s1.session_id = s2.session_id and s1.occurrence < s2.occurrence
where s2.occurrence is null
group by s1.session_id

也许添加一个 View 会提高性能(替换上面的查询):

create view MaxOccurrences as (
select sd.session_id, cd.cluster_id, count(*) as Occurrence
from session_document sd
join cluster_document cd
on sd.document_id = cd.document_id
join cluster c
on c.cluster_id = cd.cluster_id
group by sd.session_id, cd.cluster_id, c.label
);

select s1.session_id, max(s1.cluster_id) as cluster_id
from MaxOccurrences as s1
left join MaxOccurrences as s2
on s1.session_id = s2.session_id and s1.occurrence < s2.occurrence
where s2.occurrence is null
group by s1.session_id

让我知道它是否有效。

关于mysql - SQL分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9308216/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com