客户在sql查询中进行分区

时间:2017-10-01 09:28:19

标签: mysql

我有一张格式如下的表格 -

model

该表基本上包含Customer_id Purchase_date c1 2015-01-11 c2 2015-02-12 c3 2015-11-12 c1 2016-01-01 c2 2016-12-29 c4 2016-11-28 c4 2015-03-15 ... ... 及其customer_idpurchase_date根据customer_id上的购买重复purchase_date。以上只是一个示例数据,该表包含约100,000条记录。

是否有办法根据预定义的类别数据对客户进行分区

类别分区

 - Category-1: Customer who has not made purchase in last 10 weeks, but made a purchase before that
 - Category-2: Customer who as not made a purchase in last 5 weeks, but made purchase before that
 - Category-3: Customer who has made one or more purchase in last 4 weeks or it has been 8 weeks since the first purchase
 - Category-4: Customer who has made only one purchase in the last 1 week
 - Category-5: Customer who has made only one purchase

我正在寻找的是一个告诉客户及其类别的查询 -

Customer_id  Category
 C1          Category-1
 ...          ...

查询可以遵循 - oracle,postgres,sqlserver

2 个答案:

答案 0 :(得分:1)

从您的问题来看,客户似乎可以分为多个类别。因此,让我们找出每个类别中的客户,然后获取结果的 UNION

SELECT DISTINCT Customer_Id, 'CATEGORY-1' AS Category FROM mytable GROUP BY 
Customer_Id HAVING DATEDIFF(ww,MAX(Purchase_date),GETDATE()) > 10
UNION
SELECT DISTINCT Customer_Id, 'CATEGORY-2' AS Category FROM mytable GROUP BY 
Customer_Id HAVING DATEDIFF(ww,MAX(Purchase_date),GETDATE()) > 5
UNION
SELECT DISTINCT Customer_Id, 'CATEGORY-3' AS Category FROM mytable GROUP BY 
Customer_Id HAVING DATEDIFF(ww,MAX(Purchase_date),GETDATE()) < 4 OR 
DATEDIFF(ww,MIN(Purchase_date),GETDATE()) =8 
UNION
SELECT DISTINCT Customer_Id, 'CATEGORY-4' AS Category FROM mytable WHERE 
DATEDIFF(ww,Purchase_date,GETDATE())<=1 GROUP BY Customer_Id  having 
COUNT(*) =1
UNION
SELECT DISTINCT Customer_Id, 'CATEGORY-5' AS Category FROM mytable GROUP BY 
Customer_Id HAVING COUNT(*) =1
ORDER BY Category

希望这符合您的目的。 感谢

答案 1 :(得分:0)

你可以使用这样的东西

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == collectionView{
            if scrollView.contentOffset.y == 0{
                UIView.animate(withDuration: 0.5) { () -> Void in
                    self.constTopTagHeight.constant = 175 // Your UIView and imageview constraints adjust.  
                    self.view.layoutIfNeeded()
                }
            }
            else{
                UIView.animate(withDuration: 0.5) { () -> Void in
                    self.constTopTagHeight.constant = 0 // Your UIView and imageview constraints adjust.
                    self.view.layoutIfNeeded()
                }
            }
        }
    }