如何根据单个 table 中的数据计算 2 个特定时期的收入差异?
How to calculate the difference in revenue for 2 specific periods from data in a single table?
我正在使用 SQL Server 2014,我有以下 T-SQL 查询,它应该根据 2 个特定时期计算总收入的差异。执行计算的数据来自单个 SQL Table.
总而言之,我在 table T1
中有一个名为 Revenue
的专栏和另一个名为 Month
的专栏。我需要找出 2020 年 9 月至 2020 年 12 月与 2019 年 9 月至 2019 年 12 月的收入差异。
我的T-SQL查询如下:
USE [MyDatabase]
;with cte1 as
(
SELECT
sum ([Revenue]) as 'Revenue Sep 19 - Dec 19'
FROM
[T1]
WHERE
[Month] between '2019-09-01' and '2019-12-01'
),
cte2 as (
SELECT
sum ([Revenue]) as 'Revenue Sep 20 - Dec 20'
FROM
[T1]
WHERE
[Month] between '2020-09-01' and '2020-12-01'
),
cte3 as (
SELECT
cte2.[Revenue Sep 20 - Dec 20] as 'Total Revenue',
'Sep 20 - Dec 20' as 'Period',
'1' as 'ID'
FROM
[cte2]
UNION ALL
SELECT
cte1.[Revenue Sep 19 - Dec 19] as 'Total Revenue',
'Sep 19 - Dec 19' as 'Period',
'1' as 'ID'
FROM
[cte1]
)
select a.[Total Revenue] - b.[Total Revenue]
from
(select cte3.[Total Revenue] from [cte3] where cte3.[Period] = 'Sep 20 - Dec 20') a
JOIN
(select cte3.[Total Revenue] from [cte3] where cte3.[Period] = 'Sep 19 - Dec 19') b
ON b.[ID] = a.[ID]
我的查询基于以下内容:How to calculate between different group of rows of the same table
但是,当 运行 我的查询时,我收到以下错误消息:
Invalid column name 'ID'.
我不知道我做错了什么。 cte3
中不存在第 ID
列吗?
ID
必须出现在 a
和 b
的 select 列表中才能对 join
:
from
(select cte3.ID, cte3.[Total Revenue] from [cte3] where cte3.[Period] = 'Sep 20 - Dec 20') a
JOIN
(select cte3.ID, cte3.[Total Revenue] from [cte3] where cte3.[Period] = 'Sep 19 - Dec 19') b
ON b.[ID] = a.[ID]
在使用 SUB QUERY
时,您还必须提及在 Join 条件中提及的列
SELECT A.[TOTAL REVENUE] - B.[TOTAL REVENUE]
FROM
(SELECT CTE3.[TOTAL REVENUE],CTE3.ID FROM [CTE3] WHERE CTE3.[PERIOD] = 'SEP 20 - DEC 20') A
JOIN
(SELECT CTE3.[TOTAL REVENUE],CTE3.ID FROM [CTE3] WHERE CTE3.[PERIOD] = 'SEP 19 - DEC 19') B ON B.[ID] = A.[ID]
试试下面的代码,它会对你有所帮助
USE [MyDatabase]
select sum([Revenue Sep 20 - Dec 20])-sum([Revenue Sep 19 - Dec 19]) as revenue_diffrence
from
(
SELECT
[Revenue] as 'Revenue Sep 19 - Dec 19', 0 as 'Revenue Sep 20 - Dec 20'
FROM
[T1]
WHERE
[Month] between '2019-09-01' and '2019-12-01'
union all
SELECT
0 as 'Revenue Sep 19 - Dec 19',[Revenue] as 'Revenue Sep 20 - Dec 20'
FROM
[T1]
WHERE
[Month] between '2020-09-01' and '2020-12-01'
)a
您的查询比必要的复杂得多。
SELECT (SUM(CASE WHEN [Month] between '2020-09-01' and '2020-12-01' THEN [Revenue] ELSE 0 END) -
SUM(CASE WHEN [Month] between '2019-09-01' and '2019-12-01' THEN [Revenue] ELSE 0 END)
) as difference
FROM [T1];
您的版本具有三个 CTE、一个 UNION ALL
、两个子查询和一个 JOIN
。
备注:
- 不要使用单引号来分隔列别名。仅对字符串和日期使用单引号。
- 命名您的列别名,这样它们就不需要转义了。例如,使用下划线代替空格。
- 不要使用无意义的 table 别名,例如
a
和 b
。使用有意义的,例如 t2019
.
我正在使用 SQL Server 2014,我有以下 T-SQL 查询,它应该根据 2 个特定时期计算总收入的差异。执行计算的数据来自单个 SQL Table.
总而言之,我在 table T1
中有一个名为 Revenue
的专栏和另一个名为 Month
的专栏。我需要找出 2020 年 9 月至 2020 年 12 月与 2019 年 9 月至 2019 年 12 月的收入差异。
我的T-SQL查询如下:
USE [MyDatabase]
;with cte1 as
(
SELECT
sum ([Revenue]) as 'Revenue Sep 19 - Dec 19'
FROM
[T1]
WHERE
[Month] between '2019-09-01' and '2019-12-01'
),
cte2 as (
SELECT
sum ([Revenue]) as 'Revenue Sep 20 - Dec 20'
FROM
[T1]
WHERE
[Month] between '2020-09-01' and '2020-12-01'
),
cte3 as (
SELECT
cte2.[Revenue Sep 20 - Dec 20] as 'Total Revenue',
'Sep 20 - Dec 20' as 'Period',
'1' as 'ID'
FROM
[cte2]
UNION ALL
SELECT
cte1.[Revenue Sep 19 - Dec 19] as 'Total Revenue',
'Sep 19 - Dec 19' as 'Period',
'1' as 'ID'
FROM
[cte1]
)
select a.[Total Revenue] - b.[Total Revenue]
from
(select cte3.[Total Revenue] from [cte3] where cte3.[Period] = 'Sep 20 - Dec 20') a
JOIN
(select cte3.[Total Revenue] from [cte3] where cte3.[Period] = 'Sep 19 - Dec 19') b
ON b.[ID] = a.[ID]
我的查询基于以下内容:How to calculate between different group of rows of the same table
但是,当 运行 我的查询时,我收到以下错误消息:
Invalid column name 'ID'.
我不知道我做错了什么。 cte3
中不存在第 ID
列吗?
ID
必须出现在 a
和 b
的 select 列表中才能对 join
:
from
(select cte3.ID, cte3.[Total Revenue] from [cte3] where cte3.[Period] = 'Sep 20 - Dec 20') a
JOIN
(select cte3.ID, cte3.[Total Revenue] from [cte3] where cte3.[Period] = 'Sep 19 - Dec 19') b
ON b.[ID] = a.[ID]
在使用 SUB QUERY
时,您还必须提及在 Join 条件中提及的列
SELECT A.[TOTAL REVENUE] - B.[TOTAL REVENUE]
FROM
(SELECT CTE3.[TOTAL REVENUE],CTE3.ID FROM [CTE3] WHERE CTE3.[PERIOD] = 'SEP 20 - DEC 20') A
JOIN
(SELECT CTE3.[TOTAL REVENUE],CTE3.ID FROM [CTE3] WHERE CTE3.[PERIOD] = 'SEP 19 - DEC 19') B ON B.[ID] = A.[ID]
试试下面的代码,它会对你有所帮助
USE [MyDatabase]
select sum([Revenue Sep 20 - Dec 20])-sum([Revenue Sep 19 - Dec 19]) as revenue_diffrence
from
(
SELECT
[Revenue] as 'Revenue Sep 19 - Dec 19', 0 as 'Revenue Sep 20 - Dec 20'
FROM
[T1]
WHERE
[Month] between '2019-09-01' and '2019-12-01'
union all
SELECT
0 as 'Revenue Sep 19 - Dec 19',[Revenue] as 'Revenue Sep 20 - Dec 20'
FROM
[T1]
WHERE
[Month] between '2020-09-01' and '2020-12-01'
)a
您的查询比必要的复杂得多。
SELECT (SUM(CASE WHEN [Month] between '2020-09-01' and '2020-12-01' THEN [Revenue] ELSE 0 END) -
SUM(CASE WHEN [Month] between '2019-09-01' and '2019-12-01' THEN [Revenue] ELSE 0 END)
) as difference
FROM [T1];
您的版本具有三个 CTE、一个 UNION ALL
、两个子查询和一个 JOIN
。
备注:
- 不要使用单引号来分隔列别名。仅对字符串和日期使用单引号。
- 命名您的列别名,这样它们就不需要转义了。例如,使用下划线代替空格。
- 不要使用无意义的 table 别名,例如
a
和b
。使用有意义的,例如t2019
.