UNION未在SQL / SSRS服务器2008中输出正确的组合数据

时间:2015-03-04 16:51:53

标签: sql reporting-services union

我是一名NEWBIE,自学成才的SQL创作者 我创建了一个报告,它使用一系列查询来创建需要连接在一起的2个临时表。

当我在SQL中运行报表时,我实际上得到了第一个临时表,然后我得到了我想要的组合(UNION)结果。

然后,当我将这个SQL导入SSRS并创建漂亮的报告时,我只得到FIRST临时表的结果。我该如何纠正?这是应该结合两个临时表的结果的union语句。

--declare @StartDate datetime
--declare @EndDate datetime
--declare @FirstGL nvarchar(9)
--declare @LastGL nvarchar(9)
--set @StartDate = '07-01-2014'
--set @EndDate = '02-01-2015'

--------------Temp Table #1 - Pulls invoice detail from AP for a selected time period and selected GL account numbers -------------------
select gl.acc_ext_id as [GL #]
       ,rtrim(gl.acc_ds) as [Account Descr]
   ,convert(varchar(10),ih.ivo_dt,101) as [Activity Date]
   ,((rtrim(vm.org_nm) + ' Inv#: ' + rtrim(ih.ivo_ext_id) + ' ' + (CASE WHEN ih.ivo_ds IS NULL THEN ' ' ELSE rtrim(ih.ivo_ds) END) + (CASE WHEN id.ivo_dtl_ds IS NULL THEN ' ' ELSE rtrim(id.ivo_dtl_ds) END))) as [Journal Descr]
  ,CAST(id.ivo_prc_at as decimal(12,2)) as [Inv Amt]

 into #APDetail

 from TAP600_INVOICE_HDR ih inner join TAP650_INVOICE_DTL id on id.ivo_int_id = ih.ivo_int_id
 inner join TAP300_VENDOR_MASTER vm on vm.vnd_int_id = ih.vnd_int_id
 inner join TGL910_CHART_OF_ACCOUNTS gl on gl.acc_int_id = id.acc_int_id

 where gl.acc_ext_id between @FirstGL and @LastGL
and ih.ivo_dt > @StartDate and ih.ivo_dt < @EndDate

 order by gl.acc_ext_id, ih.ivo_dt 

------------------Temp Table #2 - Takes temp table #APDetail and moves Inv Amt to either DR or CR field and limits length of description to 75 characters

 select distinct [GL #]
        , [Account Descr]
        , [Activity Date]
        , [Journal Descr] 
        , [Inv Amt]
 into #APDetailUpdate
 from #APDetail

 alter table #APDetailUpdate
 add Debit decimal(12,2)

 update apu

 set apu.Debit = ap.[Inv Amt]
 from #APDetailUpdate apu left join #APDetail ap on ap.[GL #] = apu.[GL #] and ap.[Account Descr] = apu.[Account Descr] and ap.[Activity Date] = apu.[Activity Date] and ap.[Journal Descr] = apu.[Journal Descr]

 where ap.[Inv Amt] > 0 or ap.[Inv Amt] = 0

 alter table #APDetailUpdate
 add Credit decimal(12,2)
 update apu
 set apu.Credit = ap.[Inv Amt]
 from #APDetailUpdate apu left join #APDetail ap on ap.[GL #] = apu.[GL #] and ap.[Account Descr] = apu.[Account Descr] and ap.[Activity Date] = apu.[Activity Date] and ap.[Journal Descr] = apu.[Journal Descr]

其中ap。[Inv Amt]&lt; 0

 select [GL #]
 , [Account Descr]
 , [Activity Date]
 , [Journal Descr]
  --, [Inv Amt]
 , [Debit]
 , [Credit]

 from #APDetailUpdate

 order by [GL #], [Activity Date]

 --------------Temp Table #3 - Pulls journal entry detail from GL for a selected time period and selected GL account numbers---------------------

select gl.acc_ext_id as [GL #]
,rtrim(gl.acc_ds) as [Account Descr]
,convert(varchar(10),jh.jnl_pst_dt,101) as [Activity Date]
,(CASE WHEN jd.jnl_dtl_ds  IS NULL THEN jh.sys_cd + ' - ' + jh.src_ds ELSE jd.jnl_dtl_ds END) as [Journal Descr]
,sum((CASE WHEN jd.jnl_pst_deb_at IS NULL THEN '0.00' ELSE jd.jnl_pst_deb_at END)) as [Debit]
,sum((CASE WHEN jd.jnl_pst_crd_at IS NULL THEN '0.00' ELSE jd.jnl_pst_crd_at END)) as [Credit]

into #GLDetail  

from TGL220_JE_HDR jh inner join TGL250_JE_DTL jd on jd.jnl_int_id = jh.jnl_int_id
inner join TGL910_CHART_OF_ACCOUNTS gl on gl.acc_int_id = jd.acc_int_id

where gl.acc_ext_id between @FirstGL and @LastGL
and jh.jnl_pst_dt > @StartDate
and sys_cd <> 'AP'

 group by gl.acc_ext_id, gl.acc_ds, jh.jnl_pst_dt,(CASE WHEN jd.jnl_dtl_ds  IS NULL THEN jh.sys_cd + ' - ' + jh.src_ds ELSE jd.jnl_dtl_ds END) 

 order by gl.acc_ext_id, jh.jnl_pst_dt

 -----------------------Final Report - Combines the two tables with a Union statement --------------------

select [GL #], 
    [Account Descr], 
    [Activity Date], 
    substring([Journal Descr],1,75),
    (CASE WHEN apu.[Debit] IS NULL then 0.00 else apu.[Debit] END) as Debit, 
    (CASE WHEN apu.[Credit] IS NULL then 0.00 else apu.[Credit] END)as Credit

from #APDetailUpdate apu

UNION

select  [GL #], 
      [Account Descr], 
      [Activity Date], 
      substring([Journal Descr],1,75),
      (CASE WHEN [Debit] IS NULL then 0 else [Debit] END), (CASE WHEN [Credit] IS NULL then 0 else [Credit] END)

 from #GLDetail

 order by [GL #], [Activity Date]

1 个答案:

答案 0 :(得分:1)

您的问题是您正在运行两个选择语句:

 select [GL #]
 , [Account Descr]
 , [Activity Date]
 , [Journal Descr]
  --, [Inv Amt]
 , [Debit]
 , [Credit]

 from #APDetailUpdate

 order by [GL #], [Activity Date]

select [GL #], 
    [Account Descr], 
    [Activity Date], 
    substring([Journal Descr],1,75),
    (CASE WHEN apu.[Debit] IS NULL then 0.00 else apu.[Debit] END) as Debit, 
    (CASE WHEN apu.[Credit] IS NULL then 0.00 else apu.[Credit] END)as Credit

from #APDetailUpdate apu

UNION

select  [GL #], 
      [Account Descr], 
      [Activity Date], 
      substring([Journal Descr],1,75),
      (CASE WHEN [Debit] IS NULL then 0 else [Debit] END), (CASE WHEN [Credit] IS NULL then 0 else [Credit] END)

 from #GLDetail

 order by [GL #], [Activity Date]

SSRS只会返回第一个选择的结果以用作数据集。您需要选择要作为数据集显示的那个,然后在另一个数据集中为另一个选择运行单独的查询。或者将它们联合起来或删除第一个选择语句。

希望这有帮助。