查詢以獲取客戶在SQL Server DB中購買的類似產品

[英]Query to fetch the similar products purchased by customer in SQL Server DB


Example :

  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

By above example we can say, Mr. John always purchase "MILK" & "BREAD" (Mean, when ever he purchases Milk he buy Bread similarly Pen & Refils )

通過上面的例子,我們可以說,約翰先生總是購買“牛奶”和“面包”(意思是,當他購買牛奶時,他會購買面包類似的筆和筆芯)

Can anyone send me the query for the above example?

任何人都可以向我發送上述示例的查詢嗎?

3 个解决方案

#1


0  

If seems that you want to perform some kind of top basket analysis on your data. If that is the case then you can use the following DBMS_FREQUENT_ITEMSET package.

如果您希望對數據執行某種頂級分析。如果是這種情況,那么您可以使用以下DBMS_FREQUENT_ITEMSET包。

Here are some good links on the same.

這里有一些很好的鏈接。

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/

#2


0  

This is a way to obtain the result, returning the products grouped per couples:

這是一種獲得結果的方法,返回每對夫婦分組的產品:

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) 
);

The query joins the table with itself searching couples of product buyed togheter in all purchasing dates present in table.

查詢將表連接到自己,在表中顯示的所有購買日期中搜索所購買的產品對。

This returns following result:

這將返回以下結果:

JOHN    BREAD   MILK
JOHN    PEN     REFILS

#3


-1  

This is one (ugly, but working) way to achieve this -

這是一種(丑陋但有效)的方法來實現這一目標 -

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))

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/05/10/83678031c0713443e26407cf33bd6d4b.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com