SQL Server动态枢轴查询?
我的任务是提出一种翻译以下数据的方法:
date category amount
1/1/2012 ABC 1000.00
2/1/2012 DEF 500.00
2/1/2012 GHI 800.00
2/10/2012 DEF 700.00
3/1/2012 ABC 1100.00
为以下内容:
date ABC DEF GHI
1/1/2012 1000.00
2/1/2012 500.00
2/1/2012 800.00
2/10/2012 700.00
3/1/2012 1100.00
空白点可以为空或空白,要么很好,而且类别需要动态。另一个可能的警告是,我们将以有限的容量运行查询,这意味着临时表已算出。我已经尝试研究并登陆了pivot
,但是尽管我尽了最大的努力来弄清楚它,但我从未真正使用过它。谁能向我指向正确的方向?
I've been tasked with coming up with a means of translating the following data:
date category amount
1/1/2012 ABC 1000.00
2/1/2012 DEF 500.00
2/1/2012 GHI 800.00
2/10/2012 DEF 700.00
3/1/2012 ABC 1100.00
into the following:
date ABC DEF GHI
1/1/2012 1000.00
2/1/2012 500.00
2/1/2012 800.00
2/10/2012 700.00
3/1/2012 1100.00
The blank spots can be NULLs or blanks, either is fine, and the categories would need to be dynamic. Another possible caveat to this is that we'll be running the query in a limited capacity, which means temp tables are out. I've tried to research and have landed on PIVOT
but as I've never used that before I really don't understand it, despite my best efforts to figure it out. Can anyone point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
动态SQL枢轴:
结果:
Dynamic SQL PIVOT:
Results:
动态SQL枢轴
创建列的不同方法字符串
结果
Dynamic SQL PIVOT
Different approach for creating columns string
Result
我知道这个问题年龄较大,但我正在通过答案来寻找我的问题,并认为我可能能够扩展问题的“动态”部分,并可能帮助某人。
首先,我建立了这个解决方案来解决一个问题,几个同事遇到了不一致和大型数据集,需要快速枢转。
该解决方案需要创建存储过程,因此,如果您的需求不满意,请立即停止阅读。
此过程将采用Pivot语句的关键变量,以动态创建不同表,列名和聚合的枢轴语句。静态列被用作枢轴的组 /身份列(如果不需要的话,可以从代码中删除,但在枢轴语句中很常见,并且是解决原始问题的必要条件),枢轴列是最终结果列名将是从中生成的,值列是将汇总应用到的。表参数是表的名称,包括架构(schema.tablename)此部分可以使用某些爱,因为它不像我想要的那样干净。它对我有用,因为我的用法不是公开面对的,而SQL注入也不关心。汇总参数将接受任何标准的SQL汇总“ AVG”,“ sum”,“ max”等。代码还默认为Max作为汇总,这不是必需的,但是最初为此构建的受众群体不了解枢轴,并且通常是将MAX用作汇总。
让我们从代码开始以创建存储过程。该代码应在所有版本的SSM 2005及更高版本中使用,但我尚未在2005年或2016年对其进行测试,但我看不出为什么它不起作用。
接下来,我们将为示例准备好数据。我从接受的答案中添加了几个数据元素,以在此概念验证中使用数据示例,以显示汇总变化的各种输出。
以下示例显示了不同的执行语句,显示了各种聚合作为一个简单的示例。我没有选择更改静态,枢轴和值列以使示例保持简单。您应该能够仅复制和粘贴代码即可自己开始处理此
执行,分别返回以下数据集。
I know this question is older but I was looking thru the answers and thought that I might be able to expand on the "dynamic" portion of the problem and possibly help someone out.
First and foremost I built this solution to solve a problem a couple of coworkers were having with inconstant and large data sets needing to be pivoted quickly.
This solution requires the creation of a stored procedure so if that is out of the question for your needs please stop reading now.
This procedure is going to take in the key variables of a pivot statement to dynamically create pivot statements for varying tables, column names and aggregates. The Static column is used as the group by / identity column for the pivot(this can be stripped out of the code if not necessary but is pretty common in pivot statements and was necessary to solve the original issue), the pivot column is where the end resultant column names will be generated from, and the value column is what the aggregate will be applied to. The Table parameter is the name of the table including the schema (schema.tablename) this portion of the code could use some love because it is not as clean as I would like it to be. It worked for me because my usage was not publicly facing and sql injection was not a concern. The Aggregate parameter will accept any standard sql aggregate 'AVG', 'SUM', 'MAX' etc. The code also defaults to MAX as an aggregate this is not necessary but the audience this was originally built for did not understand pivots and were typically using max as an aggregate.
Lets start with the code to create the stored procedure. This code should work in all versions of SSMS 2005 and above but I have not tested it in 2005 or 2016 but I can not see why it would not work.
Next we will get our data ready for the example. I have taken the data example from the accepted answer with the addition of a couple of data elements to use in this proof of concept to show the varied outputs of the aggregate change.
The following examples show the varied execution statements showing the varied aggregates as a simple example. I did not opt to change the static, pivot, and value columns to keep the example simple. You should be able to just copy and paste the code to start messing with it yourself
This execution returns the following data sets respectively.
SQL Server 2017的更新版本使用String_agg函数来构造枢轴列列表:
Updated version for SQL Server 2017 using STRING_AGG function to construct the pivot column list:
taryn的答案随着性能改进:
数据
动态枢轴
执行计划
结果
A version of Taryn's answer with performance improvements:
Data
Dynamic pivot
Execution plans
Results
以下代码提供了将 null 替换为零的结果。
表创建和数据插入:
查询要生成确切的结果,该结果还用零代替NULL:
output:
The below code provides the results which replaces NULL to zero in the output.
Table creation and data insertion:
Query to generate the exact results which also replaces NULL with zeros:
OUTPUT :
有我的解决方案正在清理未核心的零值
There's my solution cleaning up the unnecesary null values
完全通用的方式将在非传统的MS SQL环境(例如Azure Synapse Analytics无服务器SQL池)中使用 - 它在Sproc中,但无需这样使用...
Fully generic way that will work in non-traditional MS SQL environments (e.g. Azure Synapse Analytics Serverless SQL Pools) - it's in a SPROC but no need to use as such...