【发布时间】:2018-12-10 04:51:33
【问题描述】:
我有这个基本的数据库表,并且是 SQL 初学者。
+-------------+--------+----------+
| location | Week | Expenses |
+-------------+--------+----------+
| Backoffice | 201851 | 80 |
| frontoffice | 201852 | 110 |
| Backoffice | 201901 | 120 |
| Backoffice | 201902 | 70 |
| frontoffice | 201903 | 68 |
+-------------+--------+----------+
有没有办法动态检索过去 5 周的结果,而不是每次都在我的数据透视表中硬编码?
这是我的代码:
SELECT * from
(SELECT[week]
,[Expenses]
from [cafe].[dbo].[table]where location = 'Backoffice'
) as Total_Expenses pivot (sum([expenses]) for [week] in ([201902],[201903],[201904],[201905],[201906],[201907],[201908],[201909])) as pivotable;
我希望能够只输入“5”,它会显示过去 5 周。可能是“最大”公式”
【问题讨论】:
-
什么RDBMS系统? SQL Server/MySQL/Oracle/等?
-
SQL Server - 抱歉,仍在学习站点规则
-
是的,你是对的
标签: sql sql-server sql-server-2008 tsql pivot