2011-12-29 11 views
3

私はすべての労働組合とのクエリに結合しようとしています:不適切な構文のすべてのクエリ

use SalesDWH 
go 


select cast(COUNT([specimen id]) as varchar) +'|'+[practice name]+'|'+b.[mlis practice id]+'|'+[practice code]+'|'+[Requesting Physician]+'|'+c.salesrep+'|'+ 
    cast(c.dateestablished as varchar)+'|'+ c.practicecity+'|'+c.practicestate 
from quicklabdump a 
    inner join qlmlismapping b 
    on (b.[quiklab practice code] = a.[practice code]) 
    inner join PracticeandPhysician c 
    on (a.[Requesting Physician]=c.doctorfirstname+' '+c.DOCTORLASTNAME 
     and a.[practice code]=c.practicecode) 
where DATEPART(yy, [DATE entered]) = 2011 
    and DATEPART(mm, [DATE entered]) = 12 
group by a.[practice name],b.[mlis practice id],a.[practice code], 
    a.[Requesting Physician],c.salesrep,c.dateestablished, c.practicecity,c.practicestate 
order by COUNT([specimen id]) desc 

union all 

select cast(COUNT([specimen id]) as varchar) +'|'+[practice name]+'|'+b.[mlis practice id]+'|'+[practice code]+'|'+[Requesting Physician]+'|'+c.salesrep+'|'+ 
    cast(c.dateestablished as varchar)+'|'+ c.practicecity+'|'+c.practicestate 
from quicklabdump a 
    inner join qlmlismapping b 
    on (b.[quiklab practice code] = a.[practice code]) 
    inner join PracticeandPhysician c 
    on (a.[Requesting Physician]=c.doctorfirstname+' '+c.DOCTORLASTNAME 
     and a.[practice code]=c.practicecode) 
where DATEPART(yy, [DATE entered]) = 2011 
    and DATEPART(mm, [DATE entered]) = 11 
group by a.[practice name],b.[mlis practice id],a.[practice code], 
    a.[Requesting Physician],c.salesrep,c.dateestablished, c.practicecity,c.practicestate 
order by COUNT([specimen id]) desc 

しかし、私はこのエラーを取得しています:私は間違って

Msg 156, Level 15, State 1, Line 2 
Incorrect syntax near the keyword 'union'. 

何をしているのですか?

答えて

3

は最初

order by COUNT([specimen id]) desc 

またはこれを行うを削除します。

select cDATEPART(mm, [DATE entered]) as Month, cast(COUNT([specimen id]) as varchar) +'|'+[practice name]+'|'+b.[mlis practice id]+'|'+[practice code]+'|'+[Requesting Physician]+'|'+c.salesrep+'|'+ 
    cast(c.dateestablished as varchar)+'|'+ c.practicecity+'|'+c.practicestate 
from quicklabdump a 
    inner join qlmlismapping b 
    on (b.[quiklab practice code] = a.[practice code]) 
    inner join PracticeandPhysician c 
    on (a.[Requesting Physician]=c.doctorfirstname+' '+c.DOCTORLASTNAME 
     and a.[practice code]=c.practicecode) 
where DATEPART(yy, [DATE entered]) = 2011 
    and DATEPART(mm, [DATE entered]) in (11,12) 
group by DATEPART(mm, [DATE entered]), a.[practice name],b.[mlis practice id],a.[practice code], 
    a.[Requesting Physician],c.salesrep,c.dateestablished, c.practicecity,c.practicestate 
order by COUNT([specimen id]) desc 

またそんなに

select CASE WHEN cDATEPART(mm, [DATE entered]) = 11 THEN 'The 11th Month' 
      WHEN cDATEPART(mm, [DATE entered]) = 12 THEN 'The 12th Month' END as [when], ... 
+0

ありがとうございました。もし私がこのようにしたら(11,12)...私は11のデータと12のデータを区別したいと思いますが、どうすればいいですか? CASE文をどのようにして11または12と言う余分な出力列を追加することができますか? –

+0

グループ別です。私は列を追加します。 – Hogan

+1

これは大変おかげで天才です –

9

あなたorder byは最後のステートメントだけに行くことができます:

use SalesDWH 
go 


select cast(COUNT([specimen id]) as varchar) +'|'+[practice name]+'|'+b.[mlis practice id]+'|'+[practice code]+'|'+[Requesting Physician]+'|'+c.salesrep+'|'+ 
    cast(c.dateestablished as varchar)+'|'+ c.practicecity+'|'+c.practicestate 
from quicklabdump a 
    inner join qlmlismapping b 
    on (b.[quiklab practice code] = a.[practice code]) 
    inner join PracticeandPhysician c 
    on (a.[Requesting Physician]=c.doctorfirstname+' '+c.DOCTORLASTNAME 
     and a.[practice code]=c.practicecode) 
where DATEPART(yy, [DATE entered]) = 2011 
    and DATEPART(mm, [DATE entered]) = 12 
group by a.[practice name],b.[mlis practice id],a.[practice code], 
    a.[Requesting Physician],c.salesrep,c.dateestablished, c.practicecity,c.practicestate 

union all 

select cast(COUNT([specimen id]) as varchar) +'|'+[practice name]+'|'+b.[mlis practice id]+'|'+[practice code]+'|'+[Requesting Physician]+'|'+c.salesrep+'|'+ 
    cast(c.dateestablished as varchar)+'|'+ c.practicecity+'|'+c.practicestate 
from quicklabdump a 
    inner join qlmlismapping b 
    on (b.[quiklab practice code] = a.[practice code]) 
    inner join PracticeandPhysician c 
    on (a.[Requesting Physician]=c.doctorfirstname+' '+c.DOCTORLASTNAME 
     and a.[practice code]=c.practicecode) 
where DATEPART(yy, [DATE entered]) = 2011 
    and DATEPART(mm, [DATE entered]) = 11 
group by a.[practice name],b.[mlis practice id],a.[practice code], 
    a.[Requesting Physician],c.salesrep,c.dateestablished, c.practicecity,c.practicestate 
order by COUNT([specimen id]) desc 

順序が結果セットがポスト組合である、達成された後に発生したためです。あなたが最終的に戻る前にセットを注文することはできません。

+0

感謝のようなものを言うことができます。ちょうどこれを試み、メッセージ156、レベル15、状態1、行1を得ました キーワード 'order'の近くに構文が正しくありません。 –