# SQL枢轴使用先前计算的值，而不是使用聚合函数SQL pivot with values calculated previously rather than using aggregate function

``````ID   STATION ID   DATE         ELEMENT ID   00     01     02     03......23
1    6106000      2009-05-11   110          2550   900    1050   1550    3000
2    6106000      2009-05-12   110          380    1400   1550   5090    200
3    6106000      2009-05-13   110          500    1250   800    3550    4020
``````

``````SELECT * FROM
(SELECT
Convert(char(15),[DATE],107) AS 'Date',
Format([DATE],'MMM dd') AS 'Day',
Probability = Cast(Round(((
IIf([00]>1410,1,IIf([00]=0,1,0)) +
IIf([01]>1410,1,IIf([01]=0,1,0)) +
IIf([02]>1410,1,IIf([02]=0,1,0)) +
IIf([03]>1410,1,IIf([03]=0,1,0)) +
IIf([04]>1410,1,IIf([04]=0,1,0)) +
IIf([05]>1410,1,IIf([05]=0,1,0)) +
IIf([06]>1410,1,IIf([06]=0,1,0)) +
IIf([07]>1410,1,IIf([07]=0,1,0)) +
IIf([08]>1410,1,IIf([08]=0,1,0)) +
IIf([09]>1410,1,IIf([09]=0,1,0)) +
IIf([10]>1410,1,IIf([10]=0,1,0)) +
IIf([11]>1410,1,IIf([11]=0,1,0)) +
IIf([12]>1410,1,IIf([12]=0,1,0)) +
IIf([13]>1410,1,IIf([13]=0,1,0)) +
IIf([14]>1410,1,IIf([14]=0,1,0)) +
IIf([15]>1410,1,IIf([15]=0,1,0)) +
IIf([16]>1410,1,IIf([16]=0,1,0)) +
IIf([17]>1410,1,IIf([17]=0,1,0)) +
IIf([18]>1410,1,IIf([18]=0,1,0)) +
IIf([19]>1410,1,IIf([19]=0,1,0)) +
IIf([20]>1410,1,IIf([20]=0,1,0)) +
IIf([21]>1410,1,IIf([21]=0,1,0)) +
IIf([22]>1410,1,IIf([22]=0,1,0)) +
IIf([23]>1410,1,IIf([23]=0,1,0)))/24.0)*100.0,0) AS int)
FROM
ON_2
WHERE
((([ELEMENT ID])=110) AND
(([STATION ID])='6106000') AND
((Day([DATE])) BETWEEN 1 AND 15) AND
((DatePart("m",[DATE]))=12))) AS BaseData
PIVOT
(MAX([Probability])
FOR [DATE]
IN ([2000],[2001],[2002],[2003],[2004],[2005],[2006],[2007],[2008],[2009],[2010],[2011])) AS PivotTable
``````

``````Day     2000  2001  2002  2003....2011
Dec 01  NULL  NULL  NULL  NULL....NULL
Dec 02  NULL  NULL  NULL  NULL....NULL
Dec 03  NULL  NULL  NULL  NULL....NULL
.
.
Dec 15  NULL  NULL  NULL  NULL....NULL
``````

## 1 个回复1

### #1楼 票数：2 已采纳

``````  datepart(year, date)
``````

``````  Convert(char(15),[DATE],107) AS 'Date'
``````

1回复

1回复

3回复

1回复

1回复

2回复

2回复

3回复