SQL查询 - 逐年销售增长

时间:2015-11-23 04:54:28

标签: sql-server sql-server-2008 stored-procedures

我有一个SQL查询的结果集。通过对每年租户的销售进行分组以获得每年的总销售额来得出这一结果。表名为TENANTSALES,列为:租户,日期,销售等。

TENANT      YEAR    SALES   
tenant 1    2014    2000    
tenant 1    2015    5000       
tenant 2    2013    1000    
tenant 2    2014    1500       
tenant 2    2015    800    

我使用此SQL查询代码来实现上述结果

select tenant, year(date), SUM(sales)
from tenantSales
group by tenant, YEAR(date)

我需要完成的任务是添加一个列名称年度增长,它将比较并计算每个租户的销售额逐年增长。这是样本正确/期望的输出

TENANT      YEAR    SALES    YEARLY GROWTH
tenant 1    2014    2000    
tenant 1    2015    5000       150%
tenant 2    2013    1000    
tenant 2    2014    1500       50%
tenant 2    2015    800       -46.67%

公式为:((最近一年 - 上一年度)/上一年度)* 100

租户1的示例:

<(>(2015年销售额 - 2014年销售额)/ 2014年销售额)* 100 = 150%

香港专业教育学院曾尝试这样做,在去年增加了第二年的行,以便我可以轻松计算两年的销售额,但我无法将最新的销售额加起来一年,只有一年。有没有办法或正确的方法呢?

select tenantcode, year(date), SUM(gsc), year(date) + 1
from dailymod
where tenantcode = 'cmbina13'
group by tenantcode, YEAR(date)

我们非常感谢您的专业建议。感谢

4 个答案:

答案 0 :(得分:5)

尝试此查询:

SELECT t1.tenant, t1.YEAR, t1.SALES,
    CASE WHEN t2.YEAR IS NOT NULL THEN
        FORMAT(
            CONVERT(DECIMAL(10, 2), (t1.SALES - t2.SALES)) /
            CONVERT(DECIMAL(10, 2), t2.SALES), 'p')
    ELSE NULL END AS "YEARLY GROWTH"
FROM
(
    SELECT tenant, YEAR(date) AS YEAR, SUM(sales) AS SALES
    FROM tenantSales
    GROUP BY tenant, YEAR(date)
) t1
LEFT JOIN
(
    SELECT tenant, YEAR(date) AS YEAR, SUM(sales) AS SALES
    FROM tenantSales
    GROUP BY tenant, YEAR(date)
) t2
ON t1.tenant = t2.tenant AND t2.YEAR = t1.YEAR - 1

点击以下链接查看工作演示:

SQLFiddle

延迟更新:

您也可以使用Common Table Expressions尝试相同的方法。以下是使用此方法的上述查询:

WITH cte AS(SELECT tenant, YEAR(date) AS YEAR, SUM(sales) AS SALES
            FROM tenantSales
            GROUP BY tenant, YEAR(date))
SELECT c1.*, CONVERT(varchar,
                 CONVERT(DECIMAL(10,2),
                     CONVERT(DECIMAL(10, 2), (c1.SALES - c2.SALES)) /
                     CONVERT(DECIMAL(10, 2), c2.SALES))) + '%' AS "YEARLY GROWTH"
FROM cte c1
LEFT JOIN cte c2 ON c1.tenant = c2.tenant AND c2.YEAR = c1.YEAR - 1

这是另一个小提琴,你可以测试一下:

SQLFiddle

答案 1 :(得分:1)

使用cte,您可以重复使用查询。我正在使用窗口功能,因为你可能有一些错过的岁月。但如果连续几年,您可以直接加入年度专栏:

with cte as(select tenant, 
                   year(date) y, 
                   SUM(sales) s,
                   row_number() over(partition by tenant order by sum(sales)) rn
            from tenantSales
            group by tenant, YEAR(date))
select c1.*, ((c1.s - c2.s) / c2.s) * 100 as grouth
from cte c1
left join cte c2 on c1.tenant = c2.tenant and c1.rn = c2.rn + 1

或者:

with cte as(select tenant, year(date) y, SUM(sales) s
            from tenantSales
            group by tenant, YEAR(date))
select c1.*, ((c1.s - c2.s) / c2.s) * 100 as grouth
from cte c1
left join cte c2 on c1.tenant = c2.tenant and c1.y = c2.y + 1

答案 2 :(得分:0)

请考虑我的答案与分析功能。

select tenant ,year(date) ,sum(sales),
  format( ((sum(sales)/convert(float,lag(sum(sales)) over( partition by tenant order by tenant,year(date))))-1),'p')
from tenantSales
group by tenant , year(date)

答案 3 :(得分:-1)

请找到以下解决方案。

create table tenant_details
(tenant varchar(10),
year number(10),
sales number(10)
);

insert into tenant_details values ('tenant 1',2014,2000);
insert into tenant_details values ('tenant 1',2015,5000);
insert into tenant_details values ('tenant 2',2013,1000);
insert into tenant_details values ('tenant 2',2014,1500);
insert into tenant_details values ('tenant 2',2015,800);

commit;

SQL查询:

select tenant,year,sales,case when prev_sales is null then null else
                         to_char((sales-prev_sales)*100/prev_sales,9999.9) ||'%' end profit
                         from (
select tenant,year,sales ,lag(sales,1) over (partition by tenant order by year) prev_sales from tenant_details
);