如何在T-SQL语句中找到Apriori关联?

发布于 2024-11-05 04:35:25 字数 1311 浏览 3 评论 0原文

数据库:

Transaction  ProductID
 1              1000
 2              1000
 2              1001 
 3              1000
 3              1002
 4              1000
 4              1001
 5              1003

上表中,如何用T-SQL语句找到这个结果?

+-------------+-----------+-----------------+
| PRODUCTID1  | PRODUCTID2|     SUPPORT     |
+-------------+-----------+-----------------+
|      1000   |   1001    |         2       |
+-------------+-----------+-----------------+
|      1000   |   1002    |         1       |
+-------------+-----------+-----------------+
|      1000   |   1003    |         0       |
+-------------+-----------+-----------------+
|      1001   |   1002    |         0       |
+-------------+-----------+-----------------+
|      1001   |   1003    |         0       |
+-------------+-----------+-----------------+
|      1002   |   1003    |         0       |,
+-------------+-----------+-----------------+

测试表:

create table transactions(
   ORDERID    INT,
   PRODUCTID INT
);

insert into transactions(ORDERID, PRODUCTID)
values ('1', '1000')
      ,('2', '1000')
      ,('2', '1001')
      ,('3', '1000')
      ,('3', '1002')
      ,('4', '1000')
      ,('4', '1001'),
       ('5', '1003');

提前致谢。

Database:

Transaction  ProductID
 1              1000
 2              1000
 2              1001 
 3              1000
 3              1002
 4              1000
 4              1001
 5              1003

In the above table, how to find this result with a T-SQL statement?

+-------------+-----------+-----------------+
| PRODUCTID1  | PRODUCTID2|     SUPPORT     |
+-------------+-----------+-----------------+
|      1000   |   1001    |         2       |
+-------------+-----------+-----------------+
|      1000   |   1002    |         1       |
+-------------+-----------+-----------------+
|      1000   |   1003    |         0       |
+-------------+-----------+-----------------+
|      1001   |   1002    |         0       |
+-------------+-----------+-----------------+
|      1001   |   1003    |         0       |
+-------------+-----------+-----------------+
|      1002   |   1003    |         0       |,
+-------------+-----------+-----------------+

Test Table:

create table transactions(
   ORDERID    INT,
   PRODUCTID INT
);

insert into transactions(ORDERID, PRODUCTID)
values ('1', '1000')
      ,('2', '1000')
      ,('2', '1001')
      ,('3', '1000')
      ,('3', '1002')
      ,('4', '1000')
      ,('4', '1001'),
       ('5', '1003');

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她如夕阳 2024-11-12 04:35:25

你能推断出零吗?即我们可以说没有列出的任何内容都是零吗?如果是这样:

select t1.PRODUCTID as [PRODUCTID1], t2.PRODUCTID as [PRODUCTID2],
       COUNT(1) as [SUPPORT]
from transactions t1 inner join transactions t2
  on t1.ORDERID = t2.ORDERID
  and t1.PRODUCTID < t2.PRODUCTID
group by t1.PRODUCTID, t2.PRODUCTID

输出:

PRODUCTID1  PRODUCTID2  SUPPORT
----------- ----------- -----------
1000        1001        2
1000        1002        1

Can you infer the zeros? i.e. can we just say that anything not listed is zero? if so:

select t1.PRODUCTID as [PRODUCTID1], t2.PRODUCTID as [PRODUCTID2],
       COUNT(1) as [SUPPORT]
from transactions t1 inner join transactions t2
  on t1.ORDERID = t2.ORDERID
  and t1.PRODUCTID < t2.PRODUCTID
group by t1.PRODUCTID, t2.PRODUCTID

With output:

PRODUCTID1  PRODUCTID2  SUPPORT
----------- ----------- -----------
1000        1001        2
1000        1002        1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文