使用union运行查询时收到错误,尽管单独运行它们 - SQL Server 2008

时间:2016-04-13 16:25:41

标签: sql sql-server sql-server-2008

我有一个SQL查询,如下所示:

Select
    CPrimaryGroup = t.PrimaryGroup,
    CSecondaryGroup = t.SecondaryGroup, 
    CMed = dbo.median(t.Days),
    CPerc90th = dbo.Percentile90(t.Days)
from    
    myTable t
group by 
    t.PrimaryGroup, t.SecondaryGroup

UNION

Select
    CPrimaryGroup = 'TOTAL',
    CSecondaryGroup = 'TOTAL', 
    CMed = dbo.median(t.Days),
    CPerc90th = dbo.Percentile90(t.Days)
from    
    myTable t

UNION

Select
    CPrimaryGroup = t.PrimaryGroup,
    CSecondaryGroup = 'TOTAL', 
    CMed = dbo.median(t.Days),
    CPerc90th = dbo.Percentile90(t.Days)
from    
    myTable t
group by 
    t.PrimaryGroup

当我单独执行所有查询时,它们正在工作并返回正确的结果。但是,当我通过在它们之间使用union关键字一起运行它们时,它不起作用并且给我这个错误:

  

Msg 6522,Level 16,State 2,Line 1
  在执行用户定义的例程或聚合“Percentile90”期间发生.NET Framework错误:
  System.NullReferenceException:未将对象引用设置为实例   一个物体   System.NullReferenceException:
  at Percentile90.Accumulate(SqlDouble value)

我的目标是获得一个输出(数字刚刚假定,不依赖):

primaryGroup secondaryGroup median percentile
X            Y              10     12
X            Z              15     22
Total        Total          12.5   18
X            Total          12.5   20
...
...

我在这里看不到我的错误。我该如何解决?任何帮助将不胜感激

0 个答案:

没有答案