Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 把顾客分配到商店。商店拥有最多的指定客户_Sql Server 2008 - Fatal编程技术网

Sql server 2008 把顾客分配到商店。商店拥有最多的指定客户

Sql server 2008 把顾客分配到商店。商店拥有最多的指定客户,sql-server-2008,Sql Server 2008,概述: 我有半径为20英里的商店。我有顾客。如果客户距离商店不超过20英里,则该客户将被分配到此商店。如果一位客户距离多家店铺20英里以内,则该客户将被分配到该客户花费最多的店铺 上述措施已得到实施,效果良好。但现在我有了新的要求,我需要帮助。这些商店现在有指定顾客的最大数量限制。 所以我的问题是,当一个客户可以被分配到两个商店,并且由于花费最多,假设该客户被分配到,但是该商店已经达到了分配客户的最大数量。在这种情况下,必须将客户分配到shop2 到目前为止的查询: -- assigned al

概述:

我有半径为20英里的商店。我有顾客。如果客户距离商店不超过20英里,则该客户将被分配到此商店。如果一位客户距离多家店铺20英里以内,则该客户将被分配到该客户花费最多的店铺

上述措施已得到实施,效果良好。但现在我有了新的要求,我需要帮助。这些商店现在有指定顾客的最大数量限制。 所以我的问题是,当一个客户可以被分配到两个商店,并且由于花费最多,假设该客户被分配到,但是该商店已经达到了分配客户的最大数量。在这种情况下,必须将客户分配到shop2

到目前为止的查询:

-- assigned all customers to all shops
INSERT  INTO #assignment
        SELECT  cd.ap_business_id ,
                cb.customer_id ,
                dist.distance
        FROM    shops cd
                CROSS JOIN customer cb
                JOIN ZipDistance dist ON dist.zip1 = cd.business_zip
                                         AND cb.zip = dist.zip2

-- If customer is assigned to more than one shop, keep the assignment to the shop that is closest to the customer
-- or to the shop where the customer spends most money. Remove other assignments.
;
WITH    CTE
          AS ( SELECT   a.ap_business_id ,
                        a.customer_id ,
                        a.vehicle_id ,
                        ROUND(a.distance, 1) AS distance ,
                        db$.max_dealer_net_sales AS total1 ,
                        b$.max_dealer_net_sales AS total2 ,
                        ROW_NUMBER() OVER ( PARTITION BY a.customer_id ORDER BY ROUND(a.distance,
                                                              1) ASC, ISNULL(c$.max_shop_net_sales,
                                                              0) DESC, a.customer_id ) AS RN
               FROM     #assignment a -- total $ that given Business spend for collision parts
                        LEFT JOIN ( SELECT  ap_business_id ,
                                            SUM(amount) AS max_customer_net_sales
                                    FROM    customer_payments iq
                                    WHERE   iq.close_date > DATEADD(YEAR, -1,
                                                              GETDATE())
                                    GROUP BY iq.ap_business_id
                                  ) c$ ON c$.ap_business_id = a.ap_business_id
               WHERE    a.customer_id IN ( SELECT   vehicle_id
                                           FROM     #assignment aa
                                           GROUP BY aa.customer_id
                                           HAVING   COUNT(*) > 1 )
             )
    DELETE  #assignment
    FROM    #assignment a
            JOIN CTE c ON a.ap_business_id = c.ap_business_id
                          AND a.customer_id = c.customer_id
                          AND c.RN > 1

如果您分配的客户数量在分配过程中发生变化,那么我看不到一种不使用循环的方法,我认为再添加一个过滤条件可能会起作用。计数(客户)< Max IsAgigNeDeCube,只考虑那些记录…