l--*_*''' 4 sql sql-server sql-server-2008
我试图结合所有的联合查询:
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
Run Code Online (Sandbox Code Playgroud)
但是我收到了这个错误:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'union'.
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
Eri*_*ric 10
你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
Run Code Online (Sandbox Code Playgroud)
这是因为在达到结果集之后发生排序,即结合后.在最终返回之前,您无法订购一套.
删除第一个
order by COUNT([specimen id]) desc
Run Code Online (Sandbox Code Playgroud)
或者这样做:
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
Run Code Online (Sandbox Code Playgroud)
你也可以这样说
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], ...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20154 次 |
| 最近记录: |