SQL Server 2017-购买或出现次数最多的类别和子类别

时间:2018-08-09 17:52:14

标签: sql sql-server

我是SQL新手,所以有几个问题。

在我的桌子上

enter image description here

-第1部分-在这里,我已经获得了基于客户表的客户信息,并与交易一起获得了总金额-

SELECT DISTINCT
    (cust_id),
    DOB,
    DATEDIFF(MONTH, CONVERT(DATE, Customer.DOB, 103), GETDATE()) AS [Age IN Months],
    Gender,
    city_code,
    (total_amt - (Rate + Tax)) AS [Total_sale_amt] 
FROM    
    Customer 
JOIN 
    Transactions ON Customer.customer_Id = Transactions.cust_id 
ORDER BY 
    [Age IN Months] DESC;

-第2部分-对不同的Cust_ID进行了篮子计数----对总交易计数以及产品cat和subcat的唯一计数进行了计数

SELECT DISTINCT
    (cust_id) AS [Dist_Cust_ID],
    COUNT(transaction_id) AS [Transaction_Count],
    COUNT(DISTINCT prod_cat_cdoe) AS [Unq_Cat_Count],
    COUNT(DISTINCT prod_subcat_code) AS [Unq_SubCat_Count],
    MAX(tran_date) AS [Last_Trans_Date],
    SUM(Qty) AS Total_Sale_Quantity,
    COUNT(Qty) AS Basket_Count,
    AVG(QTY) AS [Average_Basket_Quant],
    AVG(total_amt) AS Avg_Basket_Amt 
FROM    
    Transactions 
GROUP BY    
    cust_id;

在这里,我打算添加客户购买最多的类别和子类别,该如何添加?

我知道我的桌子看起来很乱,因为我必须在病房之后添加或加入桌子,但很想听听建议。

---我的问题是- ---添加4个新类别--- ( 1)购买最多的类别- 2)购买最多的子类别- 3)购买最多的类别名称- 4)每个客户的购买最多的子类别名称-)

0 个答案:

没有答案
相关问题