没有聚合函数的 TSQL Pivot
我有一个这样的表...
CustomerID | DBColumnName | Data |
---|---|---|
1 | FirstName | Joe |
1 | MiddleName | S |
1 | LastName | Smith |
1 | Date | 12/12/2009 |
2 | FirstName | Sam |
2 | MiddleName | S |
2 | LastName | Freddrick |
2 | Date | 1/12/2009 |
3 | FirstName | Jaime |
3 | MiddleName | S |
3 | LastName | Carol |
3 | 日期 | 12/1/2009 |
我想要这个...
使用 PIVOT 可以吗?
客户 | ID 名字 | 中间 | 名姓氏 | 日期 |
---|---|---|---|---|
1 | Joe | S | Smith | 12/12/2009 |
2 | Sam | S | Freddrick | 1/12/2009 |
3 | Jaime | S | Carol | 12/1/2009 |
I have a table like this...
CustomerID | DBColumnName | Data |
---|---|---|
1 | FirstName | Joe |
1 | MiddleName | S |
1 | LastName | Smith |
1 | Date | 12/12/2009 |
2 | FirstName | Sam |
2 | MiddleName | S |
2 | LastName | Freddrick |
2 | Date | 1/12/2009 |
3 | FirstName | Jaime |
3 | MiddleName | S |
3 | LastName | Carol |
3 | Date | 12/1/2009 |
And I want this...
Is this possible using PIVOT?
CustomerID | FirstName | MiddleName | LastName | Date |
---|---|---|---|---|
1 | Joe | S | Smith | 12/12/2009 |
2 | Sam | S | Freddrick | 1/12/2009 |
3 | Jaime | S | Carol | 12/1/2009 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
是的,但是为什么!!!
yes, but why !!??
您可以使用 MAX 聚合,它仍然可以工作。一个值的最大值 = 该值。
在这种情况下,您还可以在 customerid 上自连接 5 次,按每个表引用的 dbColumnName 进行过滤。效果可能会更好。
You can use the MAX aggregate, it would still work. MAX of one value = that value..
In this case, you could also self join 5 times on customerid, filter by dbColumnName per table reference. It may work out better.
好吧,抱歉这个问题不好。 gbn 让我走上了正轨。
这就是我在答案中寻找的内容。
然后我必须使用 while 语句并将上述语句构建为 varchar 并使用 dynmaic sql。
使用类似的东西
使用 while 循环构建 @fulltext 并从表中选择不同的列名称。感谢您的回答。
Ok, sorry for the poor question. gbn got me on the right track.
This is what I was looking for in an answer.
Then I had to use a while statement and build the above statement as a varchar and use dynmaic sql.
Using something like this
Having a to build @fulltext using a while loop and select the distinct column names out of the table. Thanks for the answers.
OP实际上不需要在没有聚合的情况下进行旋转,但是对于那些来这里了解如何查看的人来说:
sql 参数化 cte 查询
该问题的答案涉及需要不进行聚合的数据透视的情况,因此执行此操作的示例是解决方案的一部分。
The OP didn't actually need to pivot without agregation but for those of you coming here to know how see:
sql parameterised cte query
The answer to that question involves a situation where pivot without aggregation is needed so an example of doing it is part of the solution.
编辑:我在没有编辑的情况下写了这篇文章还没有运行SQL。我希望,你明白了。
Edit: I have written this without an editor & have not run the SQL. I hope, you get the idea.
这应该有效:
This should work:
试试这个:
Try this:
这是为数据透视查询构建动态字段的好方法:
--将值汇总到临时表中
---查看生成的字段
Here is a great way to build dynamic fields for a pivot query:
--summarize values to a tmp table
---see the fields generated
根据定义,所有数据透视都是聚合的,但是有一种简单的方法可以确保所有数据都得到透视。
除了枢轴之外的列是分组依据的列。因此,您可以在由其他分组依据划分的数据中创建一个 row_number 并将其包含在您的数据透视数据中。例如:
By definition, all pivots aggregate, however there is a simple way to make sure all the data gets pivoted.
The columns besides for the pivot are the group by's. So you can create a row_number in your data partioned by the other group by's and include that in your pivot data. for example: