如何将我的数据行组织成列

时间:2013-11-06 23:29:58

标签: sql sql-server-2008 tsql

我有一个非常简单的查询,因为它只是从表中获取记录,输出是这样的:

ID      group   StatusType      Task                DateAdministered        ScoreA  ScoreB
5199    MSP     Completed   Follow Up Contact   2011-04-01 00:00:00.000         2       5
5199    MSP     Completed   Follow Up Contact   2011-06-02 00:00:00.000         2       7
5199    MSP     Completed   Follow Up Contact   2011-06-02 00:00:00.000         2       8
5199    MSP     Completed   Follow Up Contact   2011-06-02 00:00:00.000         2       7
5199    MSP     Completed   Follow Up Contact   2011-04-28 00:00:00.000         4       8
5199    MSP     Completed   Follow Up Contact   2011-06-02 00:00:00.000         2       7
5199    MSP     Completed   Follow Up Contact   2011-05-19 00:00:00.000         2       7
5199    MSP     Completed   Follow Up Contact   2011-05-26 00:00:00.000         3       7
5199    MSP     Completed   Follow Up Contact   2011-06-02 00:00:00.000         3       7
5199    MSP     Completed   Follow Up Contact   2011-06-09 00:00:00.000         1       6
5199    MSP     Completed   Follow Up Contact   2011-06-15 00:00:00.000         3       5
5199    MSP     Completed   Follow Up Contact   2011-06-23 00:00:00.000         2       6
5199    MSP     Completed   Follow Up Contact   2011-06-30 00:00:00.000         1       6
5199    MSP     Completed   Follow Up Contact   2011-07-07 00:00:00.000         4       6
5199    MSP     Completed   Follow Up Contact   2011-07-14 00:00:00.000         3       6
5199    MSP     Completed   Follow Up Contact   2011-07-21 00:00:00.000         2       7

我需要做的是根据每个ID,组,StatusType和Task的DateAdministered特定年份的最接近月末的日期,将数据重新组织成列而不是行。换句话说,如果4月份的任务有两行,我将需要这两行的最大日期以及ScoreA和ScoreB的报告。 5月,6月等都做同样的事情......

最终结果应如下所示:

ID      group       StatusType      Task            April Assessment    ScoreA  ScoreB  May Assessment  ScoreA  ScoreB  June Assessment ScoreA  ScoreB  July Assessment ScoreA  ScoreB
5199    MSP         Completed   Follow Up Contact       4/1/2011            2       5   5/26/2011           3       7   6/30/2011           1       6   7/21/2011           2       7

我尝试过几件事,我不确定这样做的最佳方法。这几乎看起来像一个枢轴,但不需要SUM或ADD任何列,所以我不知道如果我需要一个轴,这将如何工作。

0 个答案:

没有答案