转置SQL结果集 [英] Transpose SQL result set

查看:142
本文介绍了转置SQL结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据格式如下:

Machine |  week_end  | widgets
 A      | 05-26-2013 | 5
 B      | 05-26-2013 | 4
 A      | 04-01-2013 | 6
 B      | 04-01-2013 | 0

我希望我的结果集如下所示:

I want my result set to look as follows:

Machine | 05-26-2013 | 04-01+2013 | ...
 A      | 5          | 6          |
 B      | 4          | 0          |

我不太确定如何进行此操作-如何在不显式指定查询中指定周末(或其他任何时间)的情况下转置这样的表? (数据库是SQL Server 2008.)

I'm not quite sure how to proceed here--how does one transpose a table like this without having to specify the week ends (or whatever) explicitly in the query? (DB is SQL Server 2008.)

推荐答案

您将需要对SQL PIVOT函数进行一些研究.它将允许您将行变成列.一旦找到如何应用它,这应该就是您要寻找的东西.

You will need to do some research on the SQL PIVOT function. It will allow you to turn rows into columns. This should be what you are looking for, once you find out how to apply it.

查看此链接:使用PIVOT和UNPIVOT

这篇关于转置SQL结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆