与paoluo(一天到晚游泳的鱼)有关的一个问题,希望解释一下
小弟正在学T-SQL,看了一下论坛的一些老帖,有一些疑问想问一下
问题来自:
http://community.csdn.net/Expert/topic/5393/5393540.xml?temp=.6558954
--paoluo(一天到晚游泳的鱼) ( )
Create Table table1
(id Int,
shuyu Varchar(10),
pinmin Int,
zhuangtai Varchar(10))
Insert table1 Select 1, 'A ', 11, '@ '
Union All Select 2, 'B ', 11, '@ '
Union All Select 3, 'A ', 22, '# '
Union All Select 4, 'C ', 12, '$ '
Union All Select 5, 'C ', 44, '* '
Union All Select 6, 'B ', 11, '@ '
GO
Declare @S Varchar(8000)
Select @S = 'Select pinmin, shuyu '
Select @S = @S + ' , SUM(Case zhuangtai When ' ' ' + zhuangtai + ' ' ' Then 1 Else 0 End) As ' + QUOTENAME(zhuangtai)
From table1 Group By zhuangtai --我的主要问题在这里
Select @S = @S + ' , Count(*) As zongshu From table1 Group By pinmin, shuyu Order By pinmin, shuyu '
EXEC(@S)
GO
Drop Table table1
我想知道的是:
(Select @S = @S + ' , SUM(Case zhuangtai When ' ' ' + zhuangtai + ' ' ' Then 1 Else 0 End) As ' + QUOTENAME(zhuangtai)
From table1 Group By zhuangtai)
为什么不与
(Select pinmin, shuyu , SUM(Case zhuangtai When '# ' Then 1 Else 0 End) As [#] , SUM(Case zhuangtai When '$ ' Then 1 Else 0 End) As [$] , SUM(Case zhuangtai When '* ' Then 1 Else 0 End) As [*] , SUM(Case zhuangtai When '@ ' Then 1 Else 0 End) As [@]
From table1 Group By zhuangtai)
等价.
(Select @S = @S + ' , SUM(Case zhuangtai When ' ' ' + zhuangtai + ' ' ' Then 1 Else 0 End) &nb