查询以获取客户在SQL Server DB中购买的类似产品

时间:2016-05-10 11:37:34

标签: sql sql-server database data-mining data-analysis

示例:

  C NAME   PRODUCT      DATE OF PURCHASE 
    **JOHN   MILK        12/17/2015** 
    **JOHN   BREAD       12/17/2015** 
    John     soap        12/17/2015 
   **John    milk        03/21/2016** 
   **John    bread       03/21/2016**
     John    laptop      03/21/2016
     John    pen         07/30/2015
     John    Refils       07/30/2015
     John    Pen         08/05/2016
     John    Refils       08/05/2016

通过上面的例子我们可以说,约翰先生总是购买" MILK" &安培; " BREAD" (意思是,当他购买Milk时,他会购买面包类似Pen& Refils)

有人可以向我发送上述示例的查询吗?

3 个答案:

答案 0 :(得分:0)

如果您希望对数据执行某种顶级分析。如果是这种情况,那么您可以使用以下DBMS_FREQUENT_ITEMSET包。

以下是一些很好的链接。

https://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_frqist.htm https://technology.amis.nl/2004/10/16/hidden-plsql-gem-in-10g-dbms_frequent_itemset-for-plsql-based-data-mining/

答案 1 :(得分:0)

这是一种获得结果的方法,返回按照夫妻分组的产品:

SELECT UPPER(t1.c_name), UPPER(t1.product), UPPER(t2.product), COUNT(*)
FROM products t1, products t2
WHERE UPPER(t1.c_name)  = UPPER(t2.c_name)
  AND t1.dt_purchase    = t2.dt_purchase
  AND UPPER(t1.product) < UPPER(t2.product)
GROUP BY UPPER(t1.c_name), UPPER(t1.product), UPPER(t2.product)
HAVING COUNT(*) = (
    SELECT COUNT(*)
    FROM products t3 
    WHERE UPPER(t3.c_name)  = UPPER(t1.c_name) 
      AND UPPER(t3.product) = UPPER(t1.product) 
) AND COUNT(*) = (
    SELECT COUNT(*)
    FROM products t3 
    WHERE UPPER(t3.c_name)  = UPPER(t1.c_name) 
      AND UPPER(t3.product) = UPPER(t2.product) 
);

查询将表格与自己联系,在表格中的所有购买日期中搜索购买的产品对象。

返回以下结果:

JOHN    BREAD   MILK
JOHN    PEN     REFILS

答案 2 :(得分:-1)

这是一种(丑陋但有效)的方法来实现这一目标 -

select C_Name, product 
from products p1
where date_of_purchase = (select min(date_of_purchase) 
                                                   from products p2                   
                                                   where p1.C_Name = p2.C_Name)
and product not in (select product
                                   from products p3
                                   where p1.C_Name = p3.C_Name
                                   and date_of_purchase <> (select min(date_of_purchase)
                                                                                    from products p4
                                                                                    where p3.C_Name = p4.C_Name))