Sql 您如何找到前100名客户的变化百分比?

Sql 您如何找到前100名客户的变化百分比?,sql,sql-server,Sql,Sql Server,基本上这是我想回答的问题 “查找排名前100位的客户及其平均支出、每年的平均数量。还可以查找他们支出的变化百分比。” 如何计算每年的变化百分比?我想做的是为每年创建不同的列,并显示与前一年相比的百分比变化 例如:“2003年支出”、“2004年支出”……“2010年支出”将是不同的列,这将针对每个“客户” 到目前为止,我得到了这个: select tier.Customer_Name, sum(case when tier.YEAR = 2003 then tier.Average_Spend

基本上这是我想回答的问题

“查找排名前100位的客户及其平均支出、每年的平均数量。还可以查找他们支出的变化百分比。”

如何计算每年的变化百分比?我想做的是为每年创建不同的列,并显示与前一年相比的百分比变化

例如:“2003年支出”、“2004年支出”……“2010年支出”将是不同的列,这将针对每个“客户”

到目前为止,我得到了这个:

select tier.Customer_Name, 
sum(case when tier.YEAR = 2003 then tier.Average_Spend end) * 100 / (select sum(one.tp) from (select Avg(TotalPrice) tp, YEAR(Date) as [YEAR] from DIM_CUSTOMER c inner join FACT_TRANSACTIONS t on c.IDCustomer = t.IDCustomer
Group By Customer_Name, YEAR(Date)) one where one.YEAR=2003) as spend_2003,

sum(case when tier.YEAR = 2004 then tier.Average_Spend end) * 100 / (select sum(two.tp) from (select Avg(TotalPrice) tp, YEAR(Date) as [YEAR] from DIM_CUSTOMER c inner join FACT_TRANSACTIONS t on c.IDCustomer = t.IDCustomer
Group By Customer_Name, YEAR(Date)) two where two.YEAR=2004) as spend_2004,

sum(case when tier.YEAR = 2005 then tier.Average_Spend end) * 100 / (select sum(three.tp) from (select Avg(TotalPrice) tp, YEAR(Date) as [YEAR] from DIM_CUSTOMER c inner join FACT_TRANSACTIONS t on c.IDCustomer = t.IDCustomer
Group By Customer_Name, YEAR(Date)) three where three.YEAR=2005) as spend_2005,

sum(case when tier.YEAR = 2006 then tier.Average_Spend end) * 100 / (select sum(four.tp) from (select Avg(TotalPrice) tp, YEAR(Date) as [YEAR] from DIM_CUSTOMER c inner join FACT_TRANSACTIONS t on c.IDCustomer = t.IDCustomer
Group By Customer_Name, YEAR(Date)) four where four.YEAR=2006) as spend_2006,

sum(case when tier.YEAR = 2007 then tier.Average_Spend end) * 100 / (select sum(five.tp) from (select Avg(TotalPrice) tp, YEAR(Date) as [YEAR] from DIM_CUSTOMER c inner join FACT_TRANSACTIONS t on c.IDCustomer = t.IDCustomer
Group By Customer_Name, YEAR(Date)) five where five.YEAR=2007) as spend_2007,

sum(case when tier.YEAR = 2008 then tier.Average_Spend end) * 100 / (select sum(six.tp) from (select Avg(TotalPrice) tp,YEAR(Date) as [YEAR] from DIM_CUSTOMER c inner join FACT_TRANSACTIONS t on c.IDCustomer = t.IDCustomer
Group By Customer_Name, YEAR(Date)) six where six.YEAR=2008) as spend_2008,

sum(case when tier.YEAR = 2009 then tier.Average_Spend end) * 100 / (select sum(seven.tp) from (select Avg(TotalPrice) tp, YEAR(Date) as [YEAR] from DIM_CUSTOMER c inner join FACT_TRANSACTIONS t on c.IDCustomer = t.IDCustomer
Group By Customer_Name, YEAR(Date)) seven where seven.YEAR=2009) as spend_2009,

sum(case when tier.YEAR = 2010 then tier.Average_Spend end) * 100 / (select sum(eight.tp) from (select Avg(TotalPrice) as tp, YEAR(Date) as [YEAR] from DIM_CUSTOMER c inner join FACT_TRANSACTIONS t on c.IDCustomer = t.IDCustomer
Group By Customer_Name, YEAR(Date)) eight where eight.YEAR=2010) as spend_2010

from (
select Top 100 Customer_Name, AVG(TotalPrice) as Average_Spend, AVG(Quantity) as Avg_Qty , YEAR(Date) as [YEAR] from DIM_CUSTOMER c inner join FACT_TRANSACTIONS t on c.IDCustomer = t.IDCustomer
Group By Customer_Name, YEAR(Date)
) tier
Group By tier.Customer_Name
我知道这并不是一个百分数的变化,但我想我应该先找到百分数

我也明白我把事情搞砸了,难以置信

数据如下:

CREATE TABLE DIM_MANUFACTURER (
IDManufacturer INT PRIMARY KEY IDENTITY(11, 1),
Manufacturer_Name VARCHAR (20)
)

CREATE TABLE DIM_MODEL (
IDModel INT PRIMARY KEY IDENTITY(101, 1),
Model_Name VARCHAR(20),
Unit_price MONEY ,
IDManufacturer INT REFERENCES DIM_Manufacturer(IDManufacturer)
)

CREATE TABLE DIM_CUSTOMER (
IDCustomer INT PRIMARY KEY IDENTITY(10001, 1),
Customer_Name VARCHAR(30),
Email VARCHAR (40),
Phone BIGINT
)

CREATE TABLE DIM_LOCATION (
IDLocation INT PRIMARY KEY IDENTITY(2001, 1),
ZipCode INT ,
Country VARCHAR (20),
[State] VARCHAR (20),
City VARCHAR (20)
)

CREATE TABLE DIM_DATE (
[DATE] DATE PRIMARY KEY,
[YEAR] AS YEAR([DATE]),
[QUARTER] AS DATEPART(QUARTER, [DATE]),
[MONTH] AS MONTH([DATE])
)

CREATE TABLE FACT_TRANSACTIONS (
IDModel INT REFERENCES DIM_MODEL(IDModel),
IDCustomer INT REFERENCES DIM_CUSTOMER(IDCustomer),
IDLocation INT REFERENCES DIM_LOCATION(IDLocation),
Date DATE REFERENCES DIM_DATE([DATE]),
TotalPrice MONEY,
Quantity INT,
)

INSERT INTO DIM_Manufacturer VALUES 
('Apple'), ('Samsung'), ('One Plus'), ('Nokia'), ('Motorola'), ('HTC')

INSERT INTO DIM_MODEL VALUES
('iPhone 4', 377, '11'), 
('iPhone 4S', 414, '11'), 
('iPhone 5', 456, '11'), 
('iPhone 6', 501, '11'), 
('iPhone 7', 552, '11'), 
('Thunderbolt', 201, '16'), 
('C139', 121, '15'), 
('C200', 148, '15'), 
('Droid Bionic', 155, '15'), 
('RAZR V3', 81, '15'), 
('Motorola Z', 283, '15'), 
('3210', 14, '14'), 
('5230', 31, '14'), 
('6600', 47, '14'), 
('3310 (3330)', 28, '14'), 
('6010 (6020/6030)', 55, '14'), 
('6230 (6233)', 52, '14'), 
('E1100', 148, '12'), 
('E250', 168, '12'), 
('Galaxy Note II', 216, '12'), 
('Galaxy S', 175, '12'), 
('Galaxy S4', 283, '12'), 
('Galaxy S5', 391, '12'), 
('Galaxy S7', 432, '12'), 
('Galaxy S8', 662, '12'), 
('OnePlus X', 168, '13'), 
('OnePlus 2', 189, '13'), 
('OnePlus 5', 317, '13'), 
('OnePlus 5T', 405, '13'), 
('OnePlus 6T', 495, '13') 

INSERT INTO dim_Customer VALUES
('Kallie Blackwood', 'kallie.blackwood@abc.com', '4159170276'), 
('Johnetta Abdallah', 'johnetta_abdallah@abc.com', '9196014934'), 
('Bobbye Rhym', 'brhym@abc.com', '6505905578'), 
('Micaela Rhymes', 'micaela_rhymes@abc.com', '9251192329'), 
('Tamar Hoogland', 'tamar@abc.com', '7401061857'), 
('Moon Parlato', 'moon@abc.com', '5859253831'), 
('Laurel Reitler', 'laurel_reitler@abc.com', '4108683483'), 
('Delisa Crupi', 'delisa.crupi@abc.com', '9737571204'), 
('Viva Toelkes', 'viva.toelkes@abc.com', '7736528556'), 
('Elza Lipke', 'elza@abc.com', '9733365344'), 
('Devorah Chickering', 'devorah@abc.com', '5056458855'), 
('Timothy Mulqueen', 'timothy_mulqueen@abc.org', '7186066652'), 
('Arlette Honeywell', 'ahoneywell@abc.com', '9047469448'), 
('Dominque Dickerson', 'dominque.dickerson@abc.org', '5103067375'), 
('Lettie Isenhower', 'lettie_isenhower@abc.com', '2167709766'), 
('Myra Munns', 'mmunns@abc.net', '8174448751'), 
('Stephaine Barfield', 'stephaine@abc.com', '3108962764'), 
('Lai Gato', 'lai.gato@abc.org', '8479778728'), 
('Stephen Emigh', 'stephen_emigh@abc.com', '3305530535'), 
('Tyra Shields', 'tshields@abc.com', '2153025164'), 
('Tammara Wardrip', 'twardrip@abc.net', '6505352193'), 
('Cory Gibes', 'cory.gibes@abc.com', '6268825109'), 
('Danica Bruschke', 'danica_bruschke@abc.com', '2548757856'), 
('Wilda Giguere', 'wilda@abc.net', '9076724553'), 
('Elvera Benimadho', 'elvera.benimadho@abc.net', '4088708850'), 
('Carma Vanheusen', 'carma@abc.net', '5106621716'), 
('Malinda Hochard', 'malinda.hochard@abc.com', '3177685506')

INSERT INTO DIM_LOCATION VALUES
(85086, 'US', 'Arizona', 'Anthem'), (85117, 'US', 'Arizona', 'Apache Jct'), 
(94005, 'US', 'California', 'Brisbane'), (92703, 'US', 'California', 'Bristol'), 
(21163, 'US', 'Maryland', 'Woodstock'), (21648, 'US', 'Maryland', 'Woolford'), 
(122002, 'India', 'Haryana', 'Gurgaon'), (530068, 'India', 'Karnataka', 'Bangalore'), 
(110004, 'India', 'Delhi', 'Delhi'), (400006, 'India', 'Maharashtra', 'Mumbai')

INSERT INTO DIM_DATE VALUES
('02/15/2005'), ('10/23/2005'), ('02/26/2003'), ('05/25/2009'), ('04/16/2003'), ('11/03/2003'), 
('01/01/2010'), ('04/21/2007'), ('10/19/2010'), ('10/03/2010'), ('10/13/2010'), ('05/20/2003'), 
('02/04/2004'), ('10/22/2010'), ('03/23/2005'), ('02/11/2005'), ('10/08/2008'), ('10/06/2008'), 
('05/14/2006'), ('04/22/2010'), ('12/12/2005'), ('03/27/2010'), ('03/07/2007'), ('04/17/2006'), 
('10/01/2005'), ('09/18/2010'), ('10/06/2004'), ('03/27/2009'), ('07/09/2010'), ('05/04/2010'), 
('04/22/2004'), ('09/21/2005'), ('01/08/2008'), ('02/15/2009'), ('03/14/2007'), ('11/09/2009'), 
('02/21/2010'), ('10/04/2009'), ('07/05/2003'), ('01/04/2005'), ('08/04/2008'), ('08/02/2010'), 
('07/26/2009'), ('02/18/2006'), ('02/21/2009'), ('03/10/2004'), ('01/20/2007'), ('07/22/2006'), 
('05/19/2009'), ('04/14/2008'), ('08/09/2008'), ('07/18/2007'), ('03/26/2007'), ('08/06/2006'), 
('02/11/2003'), ('01/06/2005'), ('01/10/2006'), ('07/10/2004'), ('01/15/2004'), ('01/09/2005'), 
('07/25/2007'), ('04/21/2003'), ('05/19/2010'), ('02/15/2004'), ('06/04/2009'), ('08/12/2005'), 
('04/09/2010'), ('04/11/2008'), ('09/18/2003'), ('06/28/2005'), ('11/28/2003'), ('02/04/2010'), 
('09/04/2004'), ('05/05/2007'), ('05/07/2010'), ('07/16/2008'), ('02/05/2007'), ('10/20/2006'), 
('03/07/2008'), ('02/09/2005'), ('01/27/2007'), ('06/12/2006'), ('10/03/2008'), ('02/12/2005'), 
('05/15/2009'), ('08/27/2003'), ('10/19/2008'), ('02/26/2005'), ('09/05/2006'), ('04/03/2004'), 
('06/20/2004'), ('03/05/2008'), ('04/19/2008'), ('10/11/2007'), ('05/18/2010'), ('05/08/2007'), 
('02/08/2004'), ('04/27/2007'), ('09/03/2006'), ('03/19/2009'), ('11/01/2008'), ('08/01/2008'), 
('04/27/2006'), ('04/05/2005'), ('02/10/2009'), ('07/14/2006'), ('07/16/2005'), ('09/17/2006'), 
('04/02/2004'), ('10/24/2005'), ('05/18/2005'), ('05/25/2003'), ('03/09/2010'), ('02/18/2004'), 
('10/03/2006'), ('06/05/2003'), ('09/03/2007'), ('11/06/2007'), ('02/09/2006'), ('11/03/2006'), 
('07/23/2008'), ('06/24/2010'), ('11/13/2006'), ('08/18/2008'), ('05/06/2005'), ('12/21/2004'), 
('10/06/2006'), ('04/19/2009'), ('11/18/2004'), ('08/03/2006'), ('11/19/2005'), ('07/23/2010'), 
('01/21/2005'), ('09/13/2005'), ('06/22/2010'), ('06/02/2010'), ('10/13/2006'), ('08/01/2007'), 
('09/07/2006'), ('03/28/2009'), ('05/18/2007'), ('05/04/2005'), ('07/23/2004'), ('03/27/2007'),
('05/27/2006'), ('08/14/2010'), ('09/23/2010'), ('12/15/2006'), ('10/21/2004'), ('08/15/2003'), 
('08/19/2009'), ('01/16/2008'), ('09/24/2009'), ('07/17/2006'), ('01/27/2005'), ('10/08/2005'), 
('03/05/2007'), ('12/13/2006'), ('11/15/2007'), ('05/24/2003'), ('07/22/2008'), ('06/16/2003'), 
('01/03/2007'), ('01/05/2004'), ('12/28/2009'), ('01/24/2004'), ('02/26/2010'), ('02/03/2007'), 
('02/02/2008'), ('02/22/2005'), ('12/18/2004'), ('08/25/2003'), ('04/06/2007'), ('05/21/2003'), 
('06/23/2009'), ('05/25/2010'), ('09/27/2006'), ('02/17/2007'), ('08/08/2007'), ('11/18/2008'), 
('07/17/2007'), ('07/27/2009'), ('12/13/2007'), ('12/08/2004'), ('04/13/2004'), ('12/19/2008'), 
('06/14/2008'), ('11/06/2009'), ('06/01/2005'), ('05/05/2009'), ('07/03/2009'), ('12/20/2007'), 
('04/28/2007'), ('03/09/2003'), ('07/18/2008'), ('01/22/2005'), ('07/01/2009'), ('08/18/2004'), 
('02/09/2004'), ('08/25/2004'), ('04/25/2010'), ('04/05/2007'), ('02/07/2003'), ('11/15/2010'), 
('09/28/2007'), ('02/03/2009'), ('06/03/2008'), ('03/02/2008'), ('12/19/2003'), ('12/13/2005'), 
('06/16/2008'), ('08/03/2003'), ('07/26/2005'), ('10/11/2010'), ('10/20/2007'), ('12/22/2007'), 
('01/27/2008'), ('05/07/2008'), ('01/25/2007'), ('07/28/2006'), ('02/16/2004'), ('12/12/2006'), 
('07/10/2003'), ('09/14/2009'), ('09/02/2010'), ('07/13/2008')


    INSERT INTO FACT_TRANSACTIONS VALUES                        
    (117,10002,2003,'10/13/2010',54,1),
    (104,10002,2008,'02/15/2004',503,1),
    (124,10003,2009,'10/03/2010',435,1),
    (104,10004,2002,'09/04/2004',504,1),
    (118,10006,2003,'02/21/2010',300,2),
    (123,10007,2009,'10/20/2006',392,1),    
    (106,10008,2006,'10/06/2004',205,1),
    (121,10008,2003,'03/27/2009',179,1),
    (112,10009,2006,'05/14/2006',18,1),
    (110,10009,2001,'03/26/2007',82,1),
    (118,10009,2002,'07/10/2004',151,1),
    (122,10011,2002,'06/04/2009',286,1),
    (118,10012,2010,'04/21/2007',149,1),
    (106,10012,2006,'05/07/2010',202,1),
    (104,10013,2004,'01/06/2005',505,1),
    (113,10014,2005,'10/06/2008',33,1),
    (114,10015,2001,'02/15/2005',52,1),
    (119,10016,2002,'09/21/2005',173,1),
    (123,10017,2007,'04/22/2010',393,1),
    (101,10018,2006,'02/11/2005',381,1),
    (107,10018,2010,'10/08/2008',122,1),
    (101,10019,2001,'02/21/2009',380,1),
    (126,10022,2005,'11/03/2003',169,1),
    (118,10022,2007,'04/21/2003',153,1),
    (101,10022,2009,'11/28/2003',380,1),
    (114,10023,2007,'04/17/2006',52,1),
    (101,10023,2008,'10/04/2009',760,2),
    (101,10023,2009,'05/19/2010',382,1),
    (112,10024,2010,'03/23/2005',17,1),
    (124,10024,2008,'07/18/2007',435,1),
    (108,10024,2007,'02/05/2007',151,1),
    (126,10025,2009,'01/08/2008',173,1),
    (122,10025,2005,'07/05/2003',858,3),
    (112,10025,2006,'01/20/2007',16,1),
    (110,10025,2005,'01/09/2005',85,1),
    (122,10026,2008,'08/06/2006',285,1),
    (110,10027,2003,'10/23/2005',84,1),
    (103,10027,2006,'03/27/2010',457,1),
    (108,10027,2004,'05/04/2010',153,1),
    (123,10027,2006,'02/15/2009',394,1),
    (107,10027,2009,'05/19/2009',125,1)

我提供的解决方案假设如下

  • “顶级客户”是由每个不同年份的
    平均支出定义的。这在实际业务案例中可能有意义,也可能没有意义(总支出似乎更合理)。但这超出了编程的范围,很容易更改
  • 每年都会重新计算“顶级客户”。这可能也可能不符合业务目的,但编制年度报告是一种非常常见的做法。对此进行调整也很容易
  • 解决这个问题的关键是:

  • 在适当的位置进行分配以进行排名
  • 匹配逻辑实际上非常简单:今年的顶级客户与明年的同一客户。在
    customer\u name
    year
    上的左连接将很容易工作
  • 通过显式创建临时表(以
    #
    开始)来保存中间结果,逻辑可以更容易理解和维护。例如,步骤2和步骤3可以很容易地合并,但通过将它们分开,代码看起来更有条理
  • 解决方案

    use [testdb];
    GO
    
    /* parameters */
    
    declare @top_amount int = 4;  -- top N
    
    /* 1. List of the summary info of customers by year */
    
    if OBJECT_ID('tempdb..#all_customers') is not null
        drop table #all_customers;
    
    select
        Customer_Name, 
        AVG(TotalPrice) as Average_Spend, 
        AVG(Quantity) as Avg_Qty,
        YEAR(Date) as [YEAR],
        ROW_NUMBER() over(PARTITION by YEAR(Date)
                          order by AVG(TotalPrice) desc) as rn
    into #all_customers
    from DIM_CUSTOMER as c 
    inner join FACT_TRANSACTIONS as t
        on c.IDCustomer = t.IDCustomer
    Group By Customer_Name, YEAR(Date);
    
    --select * from #all_customers order by [YEAR], Average_Spend desc;
    
    /* 2. List of top customers by year */
    
    if OBJECT_ID('tempdb..#top_customers') is not null
        drop table #top_customers;
    
    -- If other selection criteria for the legal top customers exist, it would
    -- be more maintainable to not squeeze everything in a CTE in the next step.
    select *
    into #top_customers
    from #all_customers
    where rn <= @top_amount;
    
    --select * from #top_customers order by [YEAR], Average_Spend desc;
    
    
    /* 3. track yearly change for each year */
    
    select
        L.Customer_Name,
        L.Year,
        L.Average_Spend,
        L.rn,
        R.Year as [Year_next],
        R.Average_Spend as Average_Spend_next,
        R.rn as rn_next,
        1.0 * R.Average_Spend / L.Average_Spend - 1.0 as diff
    from #top_customers as L
    left join #all_customers as R
        on L.Customer_Name = R.Customer_Name
        and R.[YEAR] = L.[YEAR] + 1;
    
    -- cleanup
    drop table #top_customers, #all_customers;
    
    在SQL Server 2017(linux docker映像,最新版本)上测试


    您可能会更好地使用更小、更简洁的数据样本和所需的数据results@JohnCappelletti我缩短了它。数据存在问题,事实上,事务有引用,但必需的引用表没有引用数据inserted@DeepakKumar它引用了模型、客户、位置和日期,所有这些都有数据iN输入,没有一个是空的。这就是你要问的吗?你说的“前100名”到底是什么意思"? 它是按平均花费、总花费、单价还是其他数量排序的?该列表是否每年都有变化,或者只是计算所有年份?如果2005年的顶级客户在2006年从名单中消失,我们是否仍将该客户保留到未来的所有年份?有太多的歧义,请澄清,以便人们能够提供帮助。
    | Customer_Name      | Year | Average_Spend | rn | Year_next | Average_Spend_next | rn_next | diff                 |
    |--------------------|------|---------------|----|-----------|--------------------|---------|----------------------|
    | Elvera Benimadho   | 2003 | 858.0000      | 1  | NULL      | NULL               | NULL    | NULL                 |
    | Cory Gibes         | 2003 | 234.0000      | 2  | NULL      | NULL               | NULL    | NULL                 |
    | Micaela Rhymes     | 2004 | 504.0000      | 1  | NULL      | NULL               | NULL    | NULL                 |
    | Johnetta Abdallah  | 2004 | 503.0000      | 2  | NULL      | NULL               | NULL    | NULL                 |
    | Delisa Crupi       | 2004 | 205.0000      | 3  | NULL      | NULL               | NULL    | NULL                 |
    | Viva Toelkes       | 2004 | 151.0000      | 4  | NULL      | NULL               | NULL    | NULL                 |
    | Arlette Honeywell  | 2005 | 505.0000      | 1  | NULL      | NULL               | NULL    | NULL                 |
    | Lai Gato           | 2005 | 381.0000      | 2  | NULL      | NULL               | NULL    | NULL                 |
    | Myra Munns         | 2005 | 173.0000      | 3  | NULL      | NULL               | NULL    | NULL                 |
    | Elvera Benimadho   | 2005 | 85.0000       | 4  | NULL      | NULL               | NULL    | NULL                 |
    | Laurel Reitler     | 2006 | 392.0000      | 1  | NULL      | NULL               | NULL    | NULL                 |
    | Carma Vanheusen    | 2006 | 285.0000      | 2  | NULL      | NULL               | NULL    | NULL                 |
    | Danica Bruschke    | 2006 | 52.0000       | 3  | NULL      | NULL               | NULL    | NULL                 |
    | Viva Toelkes       | 2006 | 18.0000       | 4  | 2007      | 82.0000            | 3       | 3.55555555555555555  |
    | Wilda Giguere      | 2007 | 293.0000      | 1  | NULL      | NULL               | NULL    | NULL                 |
    | Timothy Mulqueen   | 2007 | 149.0000      | 2  | NULL      | NULL               | NULL    | NULL                 |
    | Viva Toelkes       | 2007 | 82.0000       | 3  | NULL      | NULL               | NULL    | NULL                 |
    | Elvera Benimadho   | 2007 | 16.0000       | 4  | 2008      | 173.0000           | 1       | 9.81250000000000000  |
    | Elvera Benimadho   | 2008 | 173.0000      | 1  | NULL      | NULL               | NULL    | NULL                 |
    | Lai Gato           | 2008 | 122.0000      | 2  | NULL      | NULL               | NULL    | NULL                 |
    | Dominque Dickerson | 2008 | 33.0000       | 3  | NULL      | NULL               | NULL    | NULL                 |
    | Danica Bruschke    | 2009 | 760.0000      | 1  | 2010      | 382.0000           | 3       | -0.49736842105263158 |
    | Stephen Emigh      | 2009 | 380.0000      | 2  | NULL      | NULL               | NULL    | NULL                 |
    | Devorah Chickering | 2009 | 286.0000      | 3  | NULL      | NULL               | NULL    | NULL                 |
    | Malinda Hochard    | 2009 | 259.5000      | 4  | 2010      | 305.0000           | 4       | 0.17533718689788053  |
    | Bobbye Rhym        | 2010 | 435.0000      | 1  | NULL      | NULL               | NULL    | NULL                 |
    | Stephaine Barfield | 2010 | 393.0000      | 2  | NULL      | NULL               | NULL    | NULL                 |
    | Danica Bruschke    | 2010 | 382.0000      | 3  | NULL      | NULL               | NULL    | NULL                 |
    | Malinda Hochard    | 2010 | 305.0000      | 4  | NULL      | NULL               | NULL    | NULL                 |
    
       enter code here
    
        SELECT 
        T1.Customer_Name, T1.Year, T1.Avg_Price,T1.Avg_Qty,
        CASE
            WHEN T2.Year IS NOT NULL
            THEN FORMAT(CONVERT(DECIMAL(8,2),(T1.Avg_Price-T2.Avg_Price))/CONVERT(DECIMAL(8,2),T2.Avg_Price),'p') ELSE NULL 
            END AS 'YEARLY_%_CHANGE'
        FROM
            (SELECT t2.Customer_Name, YEAR(t1.DATE) AS YEAR, AVG(t1.TotalPrice) AS Avg_Price, AVG(t1.Quantity) AS Avg_Qty FROM FACT_TRANSACTIONS AS t1 
            left join DIM_CUSTOMER as t2 ON t1.IDCustomer=t2.IDCustomer
            where t1.IDCustomer in (select top 10 IDCustomer from FACT_TRANSACTIONS group by IDCustomer order by SUM(TotalPrice) desc)
            group by t2.Customer_Name, YEAR(t1.Date)
            )T1
        left join
            (SELECT t2.Customer_Name, YEAR(t1.DATE) AS YEAR, AVG(t1.TotalPrice) AS Avg_Price, AVG(t1.Quantity) AS Avg_Qty FROM FACT_TRANSACTIONS AS t1 
            left join DIM_CUSTOMER as t2 ON t1.IDCustomer=t2.IDCustomer
            where t1.IDCustomer in (select top 10 IDCustomer from FACT_TRANSACTIONS group by IDCustomer order by SUM(TotalPrice) desc)
            group by t2.Customer_Name, YEAR(t1.Date)
            )T2
            on T1.Customer_Name=T2.Customer_Name and T2.YEAR=T1.YEAR-1 -- self join`
    
    
    
    
    
    
    
    
    Customer_Name   Year    Avg_Price   Avg_Qty YEARLY_%_CHANGE
    Arlene Klusman  2004    665.00  1   NULL
    Arlene Klusman  2006    415.00  1   NULL
    Arlene Klusman  2008    347.00  1   NULL
    Arlene Klusman  2010    476.00  1   NULL
    Bobbye Rhym 2005    319.00  1   NULL
    Bobbye Rhym 2006    474.00  1   48.59%
    Bobbye Rhym 2007    1106.00 2   133.33%
    Bobbye Rhym 2008    322.00  1   -70.89%
    Bobbye Rhym 2009    442.00  1   37.27%
    Bobbye Rhym 2010    435.00  1   -1.58%
    Celeste Korando 2003    283.00  1   NULL
    Celeste Korando 2004    461.00  1   62.90%
    Celeste Korando 2005    31.00   1   -93.28%
    Celeste Korando 2006    401.6666    1   1,195.71%
    Celeste Korando 2009    613.00  1   NULL
    Celeste Korando 2010    396.00  1   -35.40%
    Cory Gibes  2003    284.00  1   NULL
    Cory Gibes  2004    503.00  1   77.11%
    Cory Gibes  2005    202.00  1   -59.84%
    Cory Gibes  2010    390.00  1   NULL
    Dominque Dickerson  2004    56.00   1   NULL
    Dominque Dickerson  2005    338.00  2   503.57%
    Dominque Dickerson  2006    406.00  2   20.12%
    Dominque Dickerson  2007    920.00  2   126.60%
    Dominque Dickerson  2008    255.00  1   -72.28%
    Edna Miceli 2003    172.00  1   NULL
    Edna Miceli 2004    324.3333    1   88.56%
    Edna Miceli 2005    246.6666    1   -23.95%
    Edna Miceli 2008    553.00  1   NULL
    Edna Miceli 2009    372.00  3   -32.73%
    Edna Miceli 2010    314.00  2   -15.59%
    Laurel Reitler  2003    410.00  1   NULL
    Laurel Reitler  2005    149.00  1   NULL
    Laurel Reitler  2006    226.00  1   51.68%
    Laurel Reitler  2007    288.00  1   27.43%
    Laurel Reitler  2008    557.00  1   93.40%
    Laurel Reitler  2009    1528.00 4   174.33%
    Laurel Reitler  2010    35.00   1   -97.71%
    Moon Parlato    2003    667.00  1   NULL
    Moon Parlato    2004    256.50  1   -61.54%
    Moon Parlato    2009    823.50  2   NULL
    Moon Parlato    2010    226.50  1   -72.50%
    Pamella Schmierer   2004    458.00  1   NULL
    Pamella Schmierer   2005    435.00  1   -5.02%
    Pamella Schmierer   2007    17.00   1   NULL
    Pamella Schmierer   2008    378.20  1   2,124.71%
    Pamella Schmierer   2010    205.50  1   NULL
    Sue Kownacki    2004    286.00  1   NULL
    Sue Kownacki    2005    489.75  1   71.24%
    Sue Kownacki    2006    173.00  1   -64.68%
    Sue Kownacki    2008    285.00  1   NULL
    Sue Kownacki    2009    159.00  1   -44.21%
    Sue Kownacki    2010    298.6666    1   87.84%
    
    [![enter image description here][1]][1]