甚至将一组人分成1%以内

时间:2018-02-02 16:30:13

标签: sql-server-2012 ssms-2016

我的任务是接纳一组客户,并将每个客户分成两个相同的组。请求的结果集将使每个商店位置的两个组在客户数量方面彼此相差1%,在订单数量方面彼此相差1%,在订购数量方面彼此相差不到1%。

以下是我提出的代码,它运作得相当好,大部分时间都能得到理想的结果,但有时候(我认为由于组中的异常值),%将进一步低于1%。< / p>

If OBJECT_ID('tempdb.dbo.#Orders')  IS NOT NULL     DROP TABLE #Orders

Select
     StoreID
    ,CustomerID
    ,Sum(OrderID) as Orders
    ,Sum(OrderAmount) as AmountSold

Into #Orders

From CustomerOrders

Group by StoreID,CustomerID

IF OBJECT_ID('tempdb.dbo.#OrderRanking')    IS NOT NULL     DROP TABLE #OrderRanking

Select
     O.*
    ,ROW_NUMBER() Over(Partition by StoreID Order by AmountSold, Orders) as Ranking

Into #OrderRanking

From #Orders as O


Select
     R.StoreID
    ,Count(CustomerID) as CustomerCount
    ,Sum(R.Orders) as Orders
    ,Sum(R.AmountSold) as Amountsold
    ,Case When Ranking%2 = 0 Then 'A' Else 'B' End as 'Grouping'

From #OrderRanking as R

Group by
     R.StoreID
    ,Case When Ranking%2 = 0 Then 'A' Else 'B' End

是否有更好的方法来拆分组以确保1%的差异?或者也许是一种循环几种不同分裂的方法,直到找到1%?如果循环需要一个故障安全来防止无限循环,如果不可能分裂,就像在x循环后只需要最接近的分割。

我正在使用SQL Server 2012和SSMS 2016.感谢您提供的任何帮助。

编辑: 我试图将代码转换为非公司特定的东西,我搞砸了代码。我意识到并调整了代码以显示真正受到追捧的内容。

Edit2:我自己取得了一些进展,并希望更新问题。

所以我正在研究这个问题,每次运行代码时我都可以按随机顺序对其进行排序,并让它显示每个组的方差。现在我要添加的是一种循环X次的方法,并保持整体方差最小的方法。本周末我可能会尝试更多的东西。但是现在下面是我提到的新代码。

If OBJECT_ID('tempdb.dbo.#Orders')  IS NOT NULL     DROP TABLE #Orders

Select
     StoreID
    ,CustomerID
    ,Sum(OrderID) as Orders
    ,Sum(OrderAmount) as AmountSold
    ,Rand() as Random

Into #Orders

From CustomerOrders

Group by StoreID,CustomerID

IF OBJECT_ID('tempdb.dbo.#OrderRanking')    IS NOT NULL     DROP TABLE #OrderRanking

Select
     O.*
    ,ROW_NUMBER() Over(Partition by StoreID Order by Random) as Ranking

Into #OrderRanking

From #Orders as O


If OBJECT_ID('tempdb.dbo.#Split')   IS NOT NULL     DROP TABLE #Split

Select
     R.StoreID
    ,Count(CustomerID) as CustomerCount
    ,Sum(R.Orders) as Orders
    ,Sum(R.AmountSold) as Amountsold
    ,Case When Ranking%2 = 0 Then 'A' Else 'B' End as 'Grouping'

Into #Split

From #OrderRanking as R

Group by
     R.StoreID
    ,Case When Ranking%2 = 0 Then 'A' Else 'B' End


Select
     S.StoreID
     ,((Cast(Max(Case When S.[Grouping] = 'A' Then S.CustomerCount Else 0 End) as decimal(18,2))-Cast(Max(Case When S.[Grouping] = 'B' Then S.CustomerCount Else 0 End) as decimal(18,2)))
        / Cast(Max(Case When S.[Grouping] = 'B' Then S.CustomerCount Else 0 End) as decimal(18,2)))*100 as CustomerCountVar
     ,((Cast(Max(Case When S.[Grouping] = 'A' Then S.Orders Else 0 End) as decimal(18,2))-Cast(Max(Case When S.[Grouping] = 'B' Then S.Orders Else 0 End) as decimal(18,2)))
        / Cast(Max(Case When S.[Grouping] = 'B' Then S.Orders Else 0 End) as decimal(18,2)))*100 as OrderVar
    ,((Cast(Max(Case When S.[Grouping] = 'A' Then S.Amountsold Else 0 End) as decimal(18,2))-Cast(Max(Case When S.[Grouping] = 'B' Then S.Amountsold Else 0 End) as decimal(18,2)))
        / Cast(Max(Case When S.[Grouping] = 'B' Then S.Amountsold Else 0 End) as decimal(18,2)))*100 as AmountsoldVar


From #Split as S

Group by S.StoreID

1 个答案:

答案 0 :(得分:1)

所以我们所有人的预期总是在1%之内完全不可能,但就像我说的那样,在尝试X次尝试之后我们尽可能地接近。我已经想出如何实现这一目标。下面是我目前使用10次尝试设置的代码,但可以更改为适合业务的任何数字。

If OBJECT_ID('tempdb.dbo.#TestB')       IS NOT NULL     DROP TABLE #TestB
Create Table #TestB
(
 StoreID int
,CustomerID VarChar(11)
,Orders int
,AmountSold Float
,Random Float
,Ranking bigint
,CombinedVar Decimal(18,2)
)

If OBJECT_ID('tempdb.dbo.#BestPrep')        IS NOT NULL     DROP TABLE #BestPrep
Create Table #BestPrep
(
 StoreID int
,CustomerID VarChar(11)
,Orders int
,AmountSold Float
,Random Float
,Ranking bigint
,CombinedVar Decimal(18,2)
)


Declare @Giveup int

Set     @GiveUp = 10






WHILE @GiveUp > 0
BEGIN 


If OBJECT_ID('tempdb.dbo.#Orders')  IS NOT NULL     DROP TABLE #Orders

Select
     StoreID
    ,CustomerID
    ,Sum(OrderID) as Orders
    ,Sum(OrderAmount) as AmountSold
    ,Rand() as Random

Into #Orders

From CustomerOrders

Group by StoreID,CustomerID

IF OBJECT_ID('tempdb.dbo.#OrderRanking')    IS NOT NULL     DROP TABLE #OrderRanking

Select
     O.*
    ,ROW_NUMBER() Over(Partition by StoreID Order by Random) as Ranking

Into #OrderRanking

From #Orders as O


If OBJECT_ID('tempdb.dbo.#Split')   IS NOT NULL     DROP TABLE #Split

Select
     R.StoreID
    ,Count(CustomerID) as CustomerCount
    ,Sum(R.Orders) as Orders
    ,Sum(R.AmountSold) as Amountsold
    ,Case When Ranking%2 = 0 Then 'A' Else 'B' End as 'Grouping'

Into #Split

From #OrderRanking as R

Group by
     R.StoreID
    ,Case When Ranking%2 = 0 Then 'A' Else 'B' End

If OBJECT_ID('Tempdb.dbo.#Var')     IS NOT NULL     DROP TABLE #Var

Select
     S.StoreID

    ,ABS(((Cast(Max(Case When S.[Grouping] = 'A' Then S.CustomerCount Else 0 End) as decimal(18,2))-Cast(Max(Case When S.[Grouping] = 'B' Then S.CustomerCount Else 0 End) as decimal(18,2)))
        / Cast(Max(Case When S.[Grouping] = 'B' Then S.CustomerCount Else 0 End) as decimal(18,2)))*100) as CustomerCountVar

    ,ABS(((Cast(Max(Case When S.[Grouping] = 'A' Then S.Orders Else 0 End) as decimal(18,2))-Cast(Max(Case When S.[Grouping] = 'B' Then S.Orders Else 0 End) as decimal(18,2)))
        / Cast(Max(Case When S.[Grouping] = 'B' Then S.Orders Else 0 End) as decimal(18,2)))*100) as OrderVar

    ,ABS(((Cast(Max(Case When S.[Grouping] = 'A' Then S.Amountsold Else 0 End) as decimal(18,2))-Cast(Max(Case When S.[Grouping] = 'B' Then S.Amountsold Else 0 End) as decimal(18,2)))
        / Cast(Max(Case When S.[Grouping] = 'B' Then S.Amountsold Else 0 End) as decimal(18,2)))*100) as AmountsoldVar

    ,ABS(((Cast(Max(Case When S.[Grouping] = 'A' Then S.Orders Else 0 End) as decimal(18,2))-Cast(Max(Case When S.[Grouping] = 'B' Then S.Orders Else 0 End) as decimal(18,2)))
        / Cast(Max(Case When S.[Grouping] = 'B' Then S.Orders Else 0 End) as decimal(18,2)))*100) 
    +
     ABS(((Cast(Max(Case When S.[Grouping] = 'A' Then S.Amountsold Else 0 End) as decimal(18,2))-Cast(Max(Case When S.[Grouping] = 'B' Then S.Amountsold Else 0 End) as decimal(18,2)))
        / Cast(Max(Case When S.[Grouping] = 'B' Then S.Amountsold Else 0 End) as decimal(18,2)))*100) as CombinedVar

INTO #Var    

From #Split as S

Group by S.StoreID

If Exists (Select * From #Var Where (OrderVar < 1 and AmountSoldVar <1) Or CombinedVar < 2)
If Object_ID('tempdb.dbo.#TestA')       IS NOT NULL     DROP TABLE #TestA

Select
 A.StoreID 
,A.CustomerID
,A.Orders
,A.AmountSold
,A.Random
,A.Ranking
,V.CombinedVar

Into #TestA

From #OrderRanking as A
    Join #var as V
        on A.StoreID = V.StoreID

Where A.StoreID in 
(Select StoreID From #Var Where (OrderVar < 1 and AmountSoldVar <1) Or CombinedVar < 2)


Insert Into #TestB

Select
 A.StoreID 
,A.CustomerID
,A.Orders
,A.AmountSold
,A.Random
,A.Ranking
,A.CombinedVar


From #TestA as A
    Left Join #TestB as B
        on      A.CustomerID = B.CustomerID

Where
        B.CustomerID is null


Insert Into #BestPrep

Select
 A.StoreID 
,A.CustomerID
,A.Orders
,A.AmountSold
,A.Random
,A.Ranking
,V.CombinedVar

From #OrderRanking as A
    Join #Var as V
        on A.StoreID = V.StoreID
    Left Join #BestPrep as B
        on      A.CustomerID = B.CustomerID
            and V.CombinedVar > B.CombinedVar

Where
        B.CustomerID is null


Set @Giveup = @Giveup-1



END




If Object_ID('tempdb.dbo.#bestPrep2')       IS NOT NULL DROP TABLE #bestPrep2
Select
     A.StoreID 
    ,Min(CombinedVar) as CombinedVar

Into #BestPrep2

From #BestPrep as A

Group by
     A.StoreID 

Select A.*

From #BestPrep as A
    Join #BestPrep2 as B
        on      A.StoreID = B.StoreID
            and A.CombinedVar = B.CombinedVar
Union
Select * From #TestB