Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect syntax for Union All query

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?

like image 506
Alex Gordon Avatar asked Dec 29 '11 21:12

Alex Gordon


2 Answers

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.

like image 196
Eric Avatar answered Oct 14 '22 20:10

Eric


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], ...
like image 39
Hogan Avatar answered Oct 14 '22 21:10

Hogan