用sql做apriori商品关联销售分析

原始代码是一个朋友给的,做了一些修改后使用。

——————1

create table apriori
([flow_no] [char](20),
[item_clsno] [char](10))
insert into apriori(flow_no,item_clsno)
select a.flow_no,b.item_clsno from
(select flow_no,item_no from hang.eShop4.dbo.t_rm_saleflow) a
left join hang.eShop4.dbo.t_bd_item_info b
on a.item_no=b.item_no


—————2

apriori mode1
create table a_mode_1([flow_no] [char](20),[item_clsno] [char](10))
insert into A_MODE_1 select flow_no,item_clsno from apriori where item_clsno in (
select item_clsno from (select item_clsno,COUNT(item_clsno) as count from apriori group by item_clsno) a  where a.COUNT>=20)


—————3

apriori mode2
create table a_mode_2_1
([flow_no] [char](20),[item_clsno1] [char](10),[item_clsno2] [char](10))
insert into A_MODE_2_1 select a.flow_no,a.item_clsno,b.item_clsno from A_MODE_1 as a,A_MODE_1 as b where a.flow_no=b.flow_no and a.item_clsno>b.item_clsno


—————4

create table a_mode_2_2
([flow_no] [char](20),[item_clsno1] [char](10),[item_clsno2] [char](10))
insert into A_MODE_2_2 select a.flow_no,a.item_clsno1,a.item_clsno2 from A_MODE_2_1 as a,
(select item_clsno1,item_clsno2 from
(select item_clsno1,item_clsno2,COUNT(item_clsno1) as count  from A_MODE_2_1 group by item_clsno1,item_clsno2) aa where aa.count>=20) as b
where a.item_clsno1=b.item_clsno1 and a.item_clsno2=b.item_clsno2


—————5

create table a_mode_3_1
([flow_no] [char](20),[item_clsno1] [char](10),[item_clsno2] [char](10),[item_clsno3] [char](10))
insert into A_MODE_3_1  select a.flow_no,a.item_clsno1,a.item_clsno2,b.item_clsno from a_mode_2_2 as a,A_MODE_1 as b
where a.flow_no=b.flow_no and a.item_clsno1<>b.item_clsno and a.item_clsno2<>b.item_clsno


—————6

create table a_mode_3
([flow_no] [char](20),[item_clsno1] [char](10),[item_clsno2] [char](10),[item_clsno3] [char](10))
insert into A_MODE_3 select a.flow_no,a.item_clsno1,a.item_clsno2,a.item_clsno3 from A_MODE_3_1 as a,
(select item_clsno1,item_clsno2,item_clsno3 from 
(select item_clsno1,item_clsno2,item_clsno3,COUNT(flow_no) as count from A_MODE_3_1 group by item_clsno1,item_clsno2,item_clsno3) aa where aa.count>=20) as b
where a.item_clsno1=b.item_clsno1 and a.item_clsno2=b.item_clsno2 and a.item_clsno3=b.item_clsno3

你可能感兴趣的:(SQL)