I am trying to combine to queries with union all:
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
But I am getting this error:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'union'.
What am I doing wrong?
Your order by
can only go on the last statement:
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
This is because the ordering happens after the result set is attained, which is post union. You can't order a set before it finally returns.
remove the first
order by COUNT([specimen id]) desc
or do this:
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
You can also say something like
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], ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With