2017-03-19 5 views
0

私はパーティション内から行番号を使用していて、CONCATを使用してフルネームを構成するためにいくつかのフィールドを結合したいと思っています。私はちょうど一緒にフィールドを追加する+それはすべて良いです。 CONCAT関数を試してみると、エラーメッセージが表示されます。なぜ私はそれほど理解できないのですか?誰かが集合体内で許可されていないかどうか教えてください。Concat inside集約エラーメッセージ

USE [AdventureWorks2012] 
SELECT 
    count([BusinessEntityID])as NumPeople 
    ,[PersonType] 
    ,max(case when rnum = 1 then [BusinessEntityID] end) as '1st ID' 
    ,max(case when rnum = 2 then [BusinessEntityID] end) as '2nd ID' 
    ,max(case when rnum = 1 then [FirstName]+' '+[LastName] end) as '1st FullName' 
    ,max(case when rnum = 2 then [FirstName]+' '+[LastName] end) as '2nd FullName' 

from 
    ( 
     Select *, row_number() over (partition by [PersonType] order by [BusinessEntityID]) as rnum 
     FROM 
     [Person].[Person] 
    ) x 
group by [PersonType] 

、それは出力です::ここで

は正常に動作コードがある

+-----------+------------+--------+--------+----------------+------------------+ 
| NumPeople | PersonType | 1st ID | 2nd ID | 1st FullName | 2nd FullName  | 
+-----------+------------+--------+--------+----------------+------------------+ 
| 273  | EM   | 1  | 2  | Ken Sánchez | Terri Duffy  | 
| 289  | GC   | 2091 | 2092 | David Ortiz | Qiang Wang  | 
| 18484  | IN   | 1699 | 1700 | David Robinett | Rebecca Robinson | 
| 753  | SC   | 291 | 293 | Gustavo Achong | Catherine Abel | 
| 17  | SP   | 274 | 275 | Stephen Jiang | Michael Blythe | 
| 156  | VC   | 1491 | 1493 | Paula Moberly | Suchitra Mohan | 
+-----------+------------+--------+--------+----------------+------------------+ 

これはエラーを与えるコードです:

USE [AdventureWorks2012] 
SELECT 
    count([BusinessEntityID])as NumPeople 
    ,[PersonType] 
    ,max(case when rnum = 1 then [BusinessEntityID] end) as '1st ID' 
    ,max(case when rnum = 2 then [BusinessEntityID] end) as '2nd ID' 
    ,max(case when rnum = 1 then (concat(p.[FirstName], stuff(p.[MiddleName],1,0,' '), stuff(p.[LastName],1,0,' ') end) as '1st FullName' 
    ,max(case when rnum = 2 then (concat(p.[FirstName], stuff(p.[MiddleName],1,0,' '), stuff(p.[LastName],1,0,' ')end) as '2nd FullName' 
from 
    ( 
     Select *, row_number() over (partition by [PersonType] order by [BusinessEntityID]) as rnum 
     FROM 
     [Person].[Person] 
    ) x 
group by [PersonType] 

そして、ここにありますエラー:

Msg 156, Level 15, State 1, Line 9 
Incorrect syntax near the keyword 'end'. 
Msg 102, Level 15, State 1, Line 17 
Incorrect syntax near 'x'. 

これは単にMicrosoft SQL Serverが許可しないものだと確信していますが、私はそれができないことを知りたいのですが、私が必要な時にそれを避けることができます。またはこれを行うにはいくつかの方法がある場合は、それも素晴らしいだろう...

答えて

1

私は括弧がここで唯一の問題だと思う。 concatが使用されている2行は、...) end) as '...のようになります。このときは、...))) end) as '...のようになります。完全な質問は以下の通りです。

USE [AdventureWorks2012] 
SELECT 
    count([BusinessEntityID])as NumPeople 
    ,[PersonType] 
    ,max(case when rnum = 1 then [BusinessEntityID] end) as '1st ID' 
    ,max(case when rnum = 2 then [BusinessEntityID] end) as '2nd ID' 
    ,max(case when rnum = 1 then (concat(p.[FirstName], stuff(p.[MiddleName],1,0,' '), stuff(p.[LastName],1,0,' '))) end) as '1st FullName' 
    ,max(case when rnum = 2 then (concat(p.[FirstName], stuff(p.[MiddleName],1,0,' '), stuff(p.[LastName],1,0,' '))) end) as '2nd FullName' 
from 
    ( 
     Select *, row_number() over (partition by [PersonType] order by [BusinessEntityID]) as rnum 
     FROM 
     [Person].[Person] 
    ) x 
group by [PersonType] 
+0

ありがとうございます!これはトリックでした。私はこのような単純な間違いをするのはかなり愚かだと感じています。しかし、あなたが間違いを犯していないなら、あなたは学んでいないと思います... – kiltannen