2

我有一个我无法解决的问题。我在 Microsoft SQL Server 2008 上工作,我有一个包含四列的表

Id
Date (2013-07, 2013-08, 2011-03, etc)
Amount 1 (100, 150, etc.) 
Amount 2 (100, 80, etc.)

如果Amount 1 > 150那时我需要创建新列,其中的值作为列名,并在值后一个月开始Date分配到 6 个(日期)期间。Amount 2Date

它应该如下所示:

      Id  Date      Amount 1  Amount 2
      ----------------------------------
      1   2013-07    160         60
      2   2013-10    180         80                            

      Id  Date     Amount 1  2013-08  2013-09  2013-10   2013-11   2013-12  2014-01 ...
      --------------------------------------------------------------------------------
      1   2013-07    160         10       10      10         10        10      10
      2   2013-10    180                                     20        20      20...   

我不知道该怎么做,非常感谢任何帮助!谢谢!

4

1 回答 1

1

The table itself should not have these additional columns because that would be a denormalized table structure. That's a poor way to store data in many cases. But you can easily do a query against your existing table that will return the additional columns in the form you want, so that you can display it this way. Check out PIVOT and UNPIVOT.

于 2013-07-23T16:24:06.613 回答