通过pivot,union,cross apply或其他

时间:2017-06-23 08:59:15

标签: sql sql-server sql-server-2008

从下表中,每个赛季产生一排的最佳方式是什么,其中包括赌注,回报和利润?随着季节和桌子的增加,手工联合是不理想的,因此枢轴,交叉应用或条件联合的方法可能吗? NULLS被过滤掉了。

Season  Stake   Return  Profit
2012-13 5.70    0.20    -5.50
2012-13 5.70    0.50    -5.20
2012-13 5.70    0.00    -5.70
2012-13 5.70    0.66    -5.04
2012-13 5.70    3.62    -2.08
2012-13 5.70    7.83    2.13
2012-13 5.70    0.00    -5.70
2012-13 5.70    3.64    -2.06
2012-13 5.70    0.70    -5.00
2012-13 5.70    0.00    -5.70
2012-13 5.70    60.86   55.16
2012-13 5.70    2.59    -3.11
2012-13 5.70    0.00    -5.70
2012-13 5.70    24.18   18.48
2012-13 5.70    49.49   43.79
2012-13 8.40    0.00    -8.40
2012-13 8.40    0.00    -8.40
2012-13 5.40    3.33    -8.07
2012-13 5.40    14.47   3.07
2012-13 8.40    0.00    -8.40
2012-13 8.40    11.68   3.28
2012-13 8.40    3.47    -4.93
2012-13 8.40    2.51    -5.89
2012-13 8.40    20.39   11.99
2012-13 8.40    94.36   85.96
2012-13 8.40    0.00    -8.40
2012-13 8.40    6.56    -1.84
2012-13 8.40    0.00    -8.40
2012-13 8.40    0.00    -8.40
2013-14 8.40    0.00    -8.40
2013-14 8.40    52.94   44.54
2013-14 8.20    34.61   26.01
2013-14 8.40    0.00    -8.40
2013-14 8.40    0.00    -8.40
2013-14 8.40    4.83    -3.57
2013-14 8.40    0.00    -8.40
2013-14 8.40    0.00    -8.40
2013-14 8.40    0.00    -8.40
2013-14 8.40    0.00    -8.40
2013-14 8.40    2.41    -5.99
2013-14 8.40    0.00    -8.40
2013-14 8.40    0.00    -8.40
2013-14 8.40    0.00    -8.40
2013-14 8.40    0.00    -8.40
2013-14 8.40    2.31    -6.09
2013-14 8.40    0.00    -8.40
2013-14 8.40    0.00    -8.40
2013-14 8.40    1.90    -6.50
2013-14 8.40    11.51   3.11
2013-14 8.40    9.26    0.86
2013-14 8.40    0.00    -8.40
2013-14 8.40    9.73    1.33
2013-14 8.40    0.00    -8.40
2013-14 8.40    2.35    -6.05
2013-14 8.40    0.00    -8.40
2013-14 8.40    0.00    -8.40
2013-14 8.40    0.00    -8.40
2013-14 8.40    0.00    -8.40
2013-14 8.40    0.00    -8.40
2013-14 8.40    0.00    -8.40
2013-14 8.40    0.00    -8.40
2013-14 8.40    0.00    -8.40
2013-14 8.40    2.06    -6.34
2013-14 8.40    0.00    -8.40
2013-14 8.40    16.90   8.50
2014-15 8.40    1.46    -6.94
2014-15 8.40    1.47    -6.93
2014-15 8.40    0.00    -8.40
2014-15 8.40    0.00    -8.40
2014-15 8.40    9.43    1.03
2014-15 8.40    0.00    -8.40
2014-15 8.40    0.00    -8.40
2014-15 8.40    0.00    -8.40
2014-15 8.40    0.00    -8.40
2014-15 8.40    8.47    0.07
2014-15 8.40    30.32   21.92
2014-15 8.40    6.56    -1.84
2014-15 16.80   12.96   -3.84
2014-15 16.80   2.35    -14.45
2014-15 16.80   12.65   -4.15
2014-15 16.80   12.84   -3.96
2014-15 16.80   2.17    -14.63
2014-15 16.80   9.11    -7.69
2014-15 16.80   43.94   27.14
2014-15 16.80   8.92    -7.88
2014-15 16.80   0.00    -16.80
2014-15 16.80   0.00    -16.80
2014-15 16.80   28.06   11.26
2014-15 16.80   8.28    -8.52
2014-15 16.80   0.00    -16.80
2014-15 16.80   9.52    -7.28
2014-15 16.80   25.26   8.46
2014-15 16.80   7.56    -9.24
2014-15 16.80   0.00    -16.80
2014-15 16.80   2.33    -14.47
2014-15 16.80   31.52   14.72
2014-15 16.80   12.69   -4.11
2014-15 16.80   2.49    -14.31
2014-15 16.80   4.04    -12.76
2014-15 16.80   10.27   -6.53
2014-15 16.80   24.86   8.06
2014-15 16.80   0.00    -16.80
2014-15 16.80   11.15   -5.65
2014-15 16.80   31.98   15.18
2015-16 16.80   0.00    -16.80
2015-16 16.80   0.00    -16.80
2015-16 16.80   25.56   8.76
2015-16 16.80   2.94    -13.86
2015-16 16.80   0.00    -16.80
2015-16 16.80   3.91    -12.89
2015-16 16.80   0.00    -16.80
2015-16 16.80   0.00    -16.80
2015-16 16.80   16.78   -0.02
2015-16 16.80   2.52    -14.28
2015-16 16.80   2.86    -13.94
2015-16 16.80   3.82    -12.98
2015-16 17.50   37.83   20.33
2015-16 17.50   16.40   -1.10
2015-16 17.50   17.50   0.00
2015-16 17.50   4.04    -13.46
2015-16 17.50   50.30   32.80
2015-16 17.50   15.83   -1.67
2015-16 17.50   6.25    -11.25
2015-16 17.50   22.44   4.94
2015-16 17.50   61.91   44.41
2015-16 17.50   13.26   -4.24
2015-16 17.50   14.23   -3.27
2015-16 17.50   11.67   -5.83
2015-16 17.50   14.34   -3.16
2015-16 17.50   5.13    -12.37
2015-16 17.50   16.10   -1.40
2015-16 17.50   5.13    -12.37
2015-16 17.50   16.46   -1.04
2015-16 17.50   14.17   -3.33
2015-16 17.50   4.25    -13.25
2015-16 17.50   3.50    -14.00
2015-16 17.50   4.17    -13.33
2015-16 17.50   4.88    -12.62
2015-16 17.50   13.65   -3.85
2015-16 17.50   5.75    -11.75
2015-16 17.50   37.17   19.67
2015-16 17.50   0.00    -17.50
2015-16 17.50   17.81   0.31
2015-16 17.50   5.13    -12.37
2015-16 17.50   52.32   34.82
2016-17 17.50   65.27   47.77
2016-17 17.50   16.06   -1.44
2016-17 17.50   4.58    -12.92
2016-17 17.50   13.08   -4.42
2016-17 17.50   0.00    -17.50
2016-17 17.50   5.00    -12.50
2016-17 17.50   0.00    -17.50
2016-17 17.50   14.69   -2.81
2016-17 17.50   4.00    -13.50
2016-17 17.50   13.21   -4.29
2016-17 17.50   58.01   40.51
2016-17 17.50   12.47   -5.03
2016-17 17.50   55.94   38.44
2016-17 17.50   18.13   0.63
2016-17 17.50   11.94   -5.56
2016-17 12.50   0.00    -22.50
2016-17 17.50   16.14   -1.36
2016-17 17.50   5.25    -12.25
2016-17 17.50   10.92   -6.58
2016-17 17.50   3.41    -14.09
2016-17 17.50   28.34   10.84
2016-17 17.50   55.03   37.53
2016-17 17.50   34.12   16.62
2016-17 17.50   10.36   -7.14
2016-17 17.50   12.50   -5.00
2016-17 17.50   0.00    -17.50
2016-17 17.50   12.63   -4.87
2016-17 17.50   37.22   19.72
2016-17 17.50   13.08   -4.42
2016-17 17.50   36.31   18.81
2016-17 17.50   34.33   16.83
2016-17 17.50   14.77   -2.73
2016-17 17.50   5.75    -11.75
2016-17 17.50   15.71   -1.79
2016-17 17.50   11.56   -5.94
2016-17 18.13   11.62   -6.51
2016-17 17.50   0.00    -17.50
2016-17 17.50   13.57   -3.93
2016-17 17.50   0.00    -17.50
2016-17 17.50   50.39   32.89
2016-17 17.50   13.24   -4.26
2016-17 17.50   9.55    -7.95

3 个答案:

答案 0 :(得分:1)

所以你只想要每个赛季的总数?

select season, 
       sum(Stake) as Stakes, 
       sum(Return) as Returrns, 
       sum(Profit) as Profits
from MyTable
group by season

答案 1 :(得分:1)

试试这个,

SELECT SEASON,SUM(STAKE) ,SUM(Return),Sum(Profits) from table 
Group BY Season
Order By Season ASC

答案 2 :(得分:0)

试试这个:

select Season, Sum([Stake]+[Return]+[Profit]) As Total FROM [YourTable] group by Season