Sql 不使用分组依据对行进行分组

Sql 不使用分组依据对行进行分组,sql,sql-server,sql-server-2005,Sql,Sql Server,Sql Server 2005,我已经为一些数据构建了一个SQL FIDLE,以便更清楚地了解我想要什么 我的问题是 SELECT c1.ID ,c1.Value Ian ,c2.Value Feb ,c3.Value Mar ,c4.Value Apr ,c5.Value Mai ,c6.Value Iun ,c7.Value Iul ,c8.Value Aug ,c9.Value Sept ,c10.Value Oct ,c11.Value Noe ,c12.Value Dec FROM A a left out

我已经为一些数据构建了一个SQL FIDLE,以便更清楚地了解我想要什么

我的问题是

SELECT c1.ID 
,c1.Value Ian
,c2.Value Feb
,c3.Value Mar
,c4.Value Apr 
,c5.Value Mai 
,c6.Value Iun
,c7.Value Iul
,c8.Value Aug
,c9.Value Sept
,c10.Value Oct
,c11.Value Noe
,c12.Value Dec
 FROM A a
left outer join B as c1 on a.ID=c1.IDA and MONTH(a.ADate) = 1
left outer join B as c2 on a.ID=c2.IDA and MONTH(a.ADate) = 2
left outer join B as c3 on a.ID=c3.IDA and MONTH(a.ADate) = 3
left outer join B as c4 on a.ID=c4.IDA and MONTH(a.ADate) = 4
left outer join B as c5 on a.ID=c5.IDA and MONTH(a.ADate) = 5
left outer join B as c6 on a.ID=c6.IDA and MONTH(a.ADate) = 6
left outer join B as c7 on a.ID=c7.IDA and MONTH(a.ADate) = 7
left outer join B as c8 on a.ID=c8.IDA and MONTH(a.ADate) = 8
left outer join B as c9 on a.ID=c9.IDA and MONTH(a.ADate) = 9
left outer join B as c10 on a.ID=c10.IDA and MONTH(a.ADate) = 10
left outer join B as c11 on a.ID=c11.IDA and MONTH(a.ADate) = 11
left outer join B as c12 on a.ID=c11.IDA and MONTH(a.ADate) = 12
WHERE YEAR(a.ADate) = 2014  
所以,如果你运行这个小提琴,你会得到这样的数据

ID   Ian   Feb  Mar  Apr ..
1    10   NULL NULL NULL
2    10   NULL NULL NULL 
3    10   NULL NULL NULL
4    10   NULL NULL NULL 
5    10   NULL NULL NULL
6    10   NULL NULL NULL 
7    10   NULL NULL NULL
8    10   NULL NULL NULL 
NULL NULL  9   NULL NULL
NULL NULL  9   NULL NULL 
NULL NULL  9   NULL NULL
NULL NULL  9   NULL NULL 
NULL NULL  9   NULL NULL
NULL NULL  9   NULL NULL 
NULL NULL  9   NULL NULL
NULL NULL  9   NULL NULL 
等等。。。我想做一张像这样的桌子

ID   Ian   Feb  Mar  Apr ..
1    10     9   8    6
2    10     9   8    5
3    10     9   8    6
4    10     9   7    5 
5    10     9   7    6
6    10     9   8    5 
7    10     9   8    6
8    10     9   7    5 
我怎样才能得到这个结果?我不知道。。。任何帮助都很好。此外,查询必须在sql server 2005上运行

对于表A中的每个ID,表B中总是有8个值,我希望以某种方式将它们组合在一起,这样我的结果表应该有8行13列。表B中的每个ID对应一行,表a中的每个ID对应一列+ID列

希望现在一切都清楚了。

尝试使用以下方法:


PIVOT和UNPIVOT用于升级到SQL Server 2005或更高版本的数据库。

SQL FIDLE很好,也很有用,但它是额外的。放置并确保在问题中包含相关细节。您确定ID列应为c1.ID而不是A.ID吗?一些关于这些数据的信息也会有所帮助。现在,它只是一大桶毫无意义的抽象别名和抽象数字。a.ID是月号吗?看起来是这样。我不想把所有的建筑模式细节都加载到这个问题中,是的,因为每个c1.ID在我真实数据库的另一个表中都有一个唯一的名称。PIVOT在SQL 2005中工作吗?如果是这样,这就是我想要的,谢谢你的帮助。因为我真正的数据库工作不正常,但这是一个起点,我会解决它。
SELECT 
   ID, 
   [1] as Ian, 
   [2] as Feb, 
   [3] as Mar, 
   [4] as Apr,
   [5] as Mai,
   [6] as Iun,
   [7] as Iul,
   [8] as Aug,
   [9] as Sept,
   [10] as Oct,
   [11] as Noe,
   [12] as Dec
FROM b
PIVOT
(
   SUM(Value) 
   FOR IDA IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])
)  AS ResultTable