2011-12-29 75 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? – 2011-12-29 21:38:42

+0

它在組中。我將添加該列。 – Hogan 2011-12-29 21:39:38

+1

這真是天才怪,非常感謝 – 2011-12-29 21:41:27

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 關鍵字「訂單」附近的語法不正確。 – 2011-12-29 21:35:27