简体   繁体   English

根据 SQL Server 中其他列的值在列内进行划分

[英]Dividing within a column based on values of other columns in SQL Server

Having some trouble solving this.解决这个问题有些麻烦。

store key  promotion key  sales year  average weekly sales          *Desired Output Column*
1               1           2007             100                        1 (100 / by itself)
1               1           2008             120                        1 (120 / by itself)
1               2           2008             240                        2 (240 / 120)
1               3           2007              50                        0.5 (50 / 100)
1               4           2007              70                        0.7 (70 / 100)
1               5           2007              80                        0.8 (80 / 100)
1               3           2008              180                       1.5 (180 / 120)
2               1           2009              150                       1 (150 / by itself)
2               2           2009              200                       1.3 (200 / 150)
2               3           2007              300                       Null (no baseline value in 2007)

Each store key and sales year has a baseline value indicated by promotion key 1. All other promotion keys reflect entries I want divided into the baseline value for the same store key and sales year.每个商店关键字和销售年份都有一个由促销关键字 1 表示的基准值。所有其他促销关键字反映了我想要划分为相同商店关键字和销售年份的基准值的条目。

So in the above example, I want all entries with store key 1 and year 2007 divided by the baseline value for that year and store (ie, promotion key 1 for the same store key of 1 and sales year of 2007).因此,在上面的示例中,我希望所有带有商店关键字 1 和 2007 年的条目除以该年份和商店的基准值(即,同一商店关键字 1 和 2007 年销售年份的促销关键字 1)。 Similarly, all entries with store key 1 and year 2008 divided by the baseline value for that year (promotion key 1 for store 1 and sales year 2008).类似地,所有具有商店关键字 1 和 2008 年的条目除以该年份的基准值(商店 1 的促销关键字 1 和销售年份 2008)。 Likewise for store 2, I want store key 2 and year 2009 for promotion key 2 divided by the baseline value (promotion key 1, store key 2, and year 2009).同样,对于商店 2,我希望促销密钥 2 的商店密钥 2 和 2009 年除以基线值(促销密钥 1、商店密钥 2 和 2009 年)。 As there is no baseline value (promotion key 1) for store key 2 in 2007, that entry will produce a null.由于 2007 年商店密钥 2 没有基线值(促销密钥 1),因此该条目将产生一个空值。

I think you want:我想你想要:

select t.*,
       (avgweeklysales  * 1.0 /
        max(case when promotionkey = 1 then avgweeklysales end) over (partition by storekey, salesyear)
       ) as desired_output
from t;

This is a very similar idea to Gordon's, but I used FIRST_VALUE :这与 Gordon 的想法非常相似,但我使用了FIRST_VALUE

WITH YourTable AS(
    SELECT V.StoreKey,
           V.PromotionKey,
           V.SalesYear,
           V.AvgSales
    FROM (VALUES(1,1,2007,100),
                (1,1,2008,120),
                (1,2,2008,240),
                (1,3,2007,50),
                (1,4,2007,70),
                (1,5,2007,80),
                (1,3,2008,180),
                (2,1,2009,150),
                (2,2,2009,200),
                (2,3,2007,300))V(StoreKey,PromotionKey,SalesYear,AvgSales))
SELECT YT.StoreKey,
       YT.PromotionKey,
       YT.SalesYear,
       YT.AvgSales,
       (YT.AvgSales * 1.) / FIRST_VALUE(CASE YT.PromotionKey WHEN 1 THEN YT.AvgSales END) OVER (PARTITION BY YT.StoreKey, YT.SalesYear ORDER BY YT.PromotionKey) AS DesiredColumn
FROM YourTable YT
ORDER BY YT.StoreKey,
         YT.PromotionKey,
         YT.SalesYear;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM