Transform vertical result into horizontal mode (T-SQL)(将垂直结果转换为水平模式 (T-SQL))
问题描述
以下是示例数据:
计算日期PLResult
2014-01-02 100
2014-01-03 200
2014-02-03 300
2014-02-04 400
2014-02-27 500
这是预期的结果(以逻辑格式):
Here are the expected result (in logical format) :
一月 二月 ;
CalculationDatePLResultCalculationDatePLResult
2014-01-02 100 2014-02-03 300
2014-01-03 200 2014-02-04 400
; 2014-02-27 500
这是预期的结果(使用 T-SQL 查询):
Here are the expected result (using T-SQL Query) :
Jan-CalculationDateJan-PLResultFeb-CalculationDateFeb-PLResult
2014-01-02 100 2014-02-03 ; 300
2014-01-03 200 2014-02-04 ; 400
; 2014-02-27 ; 500
目标:
- 按月份对结果进行分类.在上面的示例中,一月份的结果放在一月份的细分中.
- 月数可以是动态的.在上面的例子中,它只显示了 1 月和 2 月,因为只有 2 个月的结果
- 结果将通过 Excel 显示.其实我可以查询多个查询表来汇总不同月份的结果,但是如果可以通过一个查询返回所有结果,那么维护和调试会更容易.
以下是填充示例数据的脚本:
Here are the scripts to populate the sample data :
到目前为止,这是我构建查询的尝试:
So far here is my attempt in building the query :
推荐答案
正如人们所说,这实际上是不可能的,你能得到的最接近的是:
As has been said this isn't actually possible, the closest you could get is:
即使这并不简单,我仍然建议在 sql 之外处理这样的格式.第一步是按月对数据进行分区,然后对每个月的日期进行排序:
And even that is not simple and I would still advise handling formatting like this outside of sql. The first step is to partition the data by month, and then rank the dates in each month:
这给出:
然后您可以旋转这些数据:
You can then pivot this data:
这给出:
那么由于您有一个动态的月份数,您需要动态构建上述语句并使用 SP_EXECUTESQL
来运行它:
Then since you have a dynamic number of months you need to build the above statement dynamically and use SP_EXECUTESQL
to run it:
SQL Fiddle 示例
请注意,我仍然建议不要使用这种技术,并认为 SQL 应该用于存储/检索数据,以及用于格式化数据的表示层
这篇关于将垂直结果转换为水平模式 (T-SQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!