Union All查询语句语法错误。

5

我正在尝试使用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

但是我遇到了这个错误:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'union'.

我做错了什么?
2个回答

11

您的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

这是因为排序发生在结果集合并之后,即在联合操作之后。你无法在最终返回结果之前对集合进行排序。


非常感谢。我刚试了一下,得到了“Msg 156,Level 15,State 1,Line 1 Incorrect syntax near the keyword 'order'”的错误提示。 - Alex Gordon

8
删除第一个

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], ...

非常感谢。如果我按照这种方式进行(11,12)...我想区分11中的数据和12中的数据,我该如何做?您能向我展示如何使用CASE语句并添加一个额外的输出列来表示11或12吗? - Alex Gordon
它在分组中。我会添加这一列。 - Hogan
只是出于好奇,当我按照你和Eric的建议仅在最后一行放置ORDER BY时,为什么使用UNION ALL无法正常工作? - Alex Gordon
Msg 156,级别 15,状态 1,行 1 关键字 'order' 附近语法不正确。 - Alex Gordon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接