SQL中的权重因子

SQL中的权重因子,sql,sql-server,Sql,Sql Server,我有一份报告,统计各国和次区域的报价。现在我有一些国家为空的引号 Table Quote Number | Country | Subregion | Quote Count 12233 | Germany | EMEA | 100 2223 | Blank | EMEA | 3333 3444 | France | EMEA | 200 455454 | Spain | EMEA

我有一份报告,统计各国和次区域的报价。现在我有一些国家为空的引号

Table 
Quote Number | Country | Subregion | Quote Count 
12233        | Germany | EMEA      | 100 
2223         | Blank   | EMEA      | 3333
3444         | France  | EMEA      | 200
455454       | Spain   | EMEA      | 300
无空国家的总报价等于1000,其中10%与德国等相关,因此德国权重系数为0.1

因此,我总共有3333个引号,其中国家字段为空。因此,我被要求获取欧洲、中东和非洲地区的报价,然后使用基于当前报价分布的加权因子将报价分布到德国、西班牙、法国和其他欧洲、中东和非洲国家,然后将报价添加到该地区其他国家(如欧洲、中东和非洲)的统计中

所以现在我完全不知道该怎么做?有什么想法吗

帮忙?有人吗

步骤1-获取EMEA所有国家的当前权重系数。在sql中

步骤2-获取没有为EMEA分配国家/地区的报价的报价计数。在sql中


第3步-根据权重因子,在每个国家/地区添加x个报价。在sql中,要获取权重因子,请执行以下操作:

select country, quotecount,
       quotecount*1.0/sum(quotecount) over (partition by null) as allocp
from t
where country <> 'Blank'
要重新添加这些,请加入总数:

select t.country, t.region, t.quotecount,
       (t.quotecount + allocp*b.BlankBQ) as AllocatedQC
from (select country, quotecount,
             quotecount*1.0/sum(quotecount) over (partition by region) as allocp
      from t
      where country <> 'Blank'
     ) t join
     (select sum(QuoteCount) as BlankBQ
      from t
      where country = 'Blank'
      group by region
     ) b
     on t.region = b.region

根据样本中的数据:

WITH    Factors
      AS ( SELECT   Country ,
                    SUM([Quote Count])
                    / CAST(( SELECT SUM([Quote Count])
                             FROM   dbo.quotes
                             WHERE  Region = q.Region
                                    AND Country IS NOT NULL
                           ) AS FLOAT) AS WeightingFactor ,
                    q.Region
           FROM     dbo.quotes q
           WHERE    country IS NOT NULL
           GROUP BY q.Country ,
                    q.Region
         )
SELECT  q.country ,
        ( SELECT    SUM([quote count])
          FROM      dbo.quotes
          WHERE     Country IS NULL
                    AND Region = q.Region
        ) * f.WeightingFactor + SUM(q.[Quote Count]) AS [Quote Count]
FROM    dbo.quotes q
        JOIN Factors f ON q.Country = f.Country
                          AND q.Region = f.Region
WHERE   q.Country IS NOT NULL
GROUP BY q.Country ,
        q.Region ,
        WeightingFactor

添加到数字中的引号数字必须是整数