简体   繁体   English

在SQL SERVER数据库中进行数据挖掘,找到最可能的组合

[英]Data mining in SQL SERVER database, find most probably combinations

I have to build a query for analyze the "trending" sales from a store. 我必须建立一个查询来分析商店的“趋势”销售。 Basically I need to get the occurrences when a combination of articles are bought, for example: When the article 0001 is bought it is very probably that the article 0002 is also bought, so I would like to retrieve something like: 基本上,当购买商品组合时,我需要获取相关信息,例如:购买商品0001时,很有可能也购买了商品0002,因此我想检索以下内容:

article a | article b | occurrences |
--------- | --------- | ----------- |
0001      | 0002      | 1
0001      | 0003      | 0

In fact I have a table TicketDetails where it is stored every ticket and the article codes contained on each ticket, something like: 实际上,我有一个表格TicketDetails,用于存储每个票证和每个票证中包含的商品代码,例如:

store | station | document | consecutive | article
----- | ------- | -------- | ----------- | ------
w     | x       | y        | a           | 0001
w     | x       | y        | a           | 0002 (same ticket, different article)
w     | x       | y        | b           | 0003

Please give me any suggestions on how to build this query, I feel kinda of lost. 请给我有关如何构建此查询的任何建议,我觉得有些失落。

Note: As shown above every ticket is a combination of the first 4 columns wxyz 注意:如上图所示,每张票证都是前4列wxyz的组合

I think you just want a self-join. 我想你只想一个自我加入。 If you want all articles -- rather than those that just co-occur in an order -- then the SQL is a tad bit trickier. 如果您想要所有文章(而不是仅按顺序共存的文章),则SQL有点棘手。

Let me assume you have a table called articles , so you can first generate all pairs: 让我假设您有一个称为articles的表,因此您可以首先生成所有对:

select a1.article, a2.article, count(td2.article) as occurrences
from articles a1 join
     articles a2
     on a1.article < a2.article left join -- (a, b) is the same as (b, a)
     ticketDetails td1
     on td1.article = a1.article left join
     ticketDetails td2
     on td2.article = a2.article and
        td2.store = td1.store and
        td2.station = td1.station and
        td2.document = td1.document and
        td2.consecutive = td1.consecutive
group by a1.article, a2.article;

Join TicketDetails to itself, matching tickets, but different articles 将TicketDetails加入自身,匹配票证,但文章不同

select t1.article
      ,t2.article
      ,Count(t1.article)
from ticketdetails t1
left join ticketdetails t2
   on t1.store = t2.store
      t1.station = t2.station
      t1.document = t2.document
      t1.consecutive = t2.consecutive
      t1.article < t2.article
group by t1.article, t2.article

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM