添加两个查询的结果

时间:2015-12-23 11:14:10

标签: sql-server sql-server-2008

我正在使用MS-SQL 2008 R2。

我有2个查询返回所需的结果。 但是我需要从每个查询中添加两个结果以提供最终值[Enterprise Value]。我确信这是非常直接的,但是我已经围绕这个圈子,尝试合并SUM,我认为这是正确的方法?

以下是目前的完整查询:

declare @d1 datetime='2015-12-22'
(select 
c.fs_perm_sec_id,
((c.p_price * s.p_com_shs_out)/1000) as [Enterprise Value]
from fp_v1.fp_basic_bd c
left join edm_v1.edm_security_entity_map e 
on e.fs_perm_sec_id= c.fs_perm_sec_id
left join fp_v1.fp_basic_sho s 
on s.fs_perm_sec_id = c.fs_perm_sec_id
and c.date=@d1
where s."date" = 
    (
    select MAX(s2."date")
    from fp_v1.fp_basic_sho s2
    where s2.fs_perm_sec_id=c.fs_perm_sec_id
    and s2."date" <= c."date"
    )
and c."date"=@d1
and e.termination_date is null
and c.fs_perm_sec_id = 'GPHC8W-S-GB') 

UNION ALL

select 
ff.fs_perm_sec_id,
((FF_debt + ff_pfd_stk + ff_min_int_accum) - FF.ff_cash_st) as [Enterprise Value]
from ff_v2.ff_basic_af_v2 FF
where FF."date" = 
(   select MAX(FF2."date")
    from ff_v2.ff_basic_af_v2 FF2
    where FF2.fs_perm_sec_id=FF.fs_perm_sec_id
    and FF.date <= FF2.date
    ) 
and FF.fs_perm_sec_id =('GPHC8W-S-GB')

插入&#34; UNION ALL&#34;在两个查询之间,我得到以下结果:

fs_perm_sec_id  Enterprise Value
GPHC8W-S-GB     9270.5204655
GPHC8W-S-GB     835

我想要达到的是两行中的两个值的总和,即:

fs_perm_sec_id  Enterprise Value
GPHC8W-S-GB     10105.52

感谢您的帮助。

最终SQL:

declare @d1 datetime='2015-12-23'
Select  fs_perm_sec_id, SUM([Enterprise Value]) AS 'Enterprise Value'

from 
(
(select 
c.fs_perm_sec_id,
((c.p_price * s.p_com_shs_out)/1000) as [Enterprise Value]
from fp_v1.fp_basic_bd c
left join edm_v1.edm_security_entity_map e 
on e.fs_perm_sec_id= c.fs_perm_sec_id
left join fp_v1.fp_basic_sho s 
on s.fs_perm_sec_id = c.fs_perm_sec_id
and c.date=@d1
where s."date" = 
    (
    select MAX(s2."date")
    from fp_v1.fp_basic_sho s2
    where s2.fs_perm_sec_id=c.fs_perm_sec_id
    and s2."date" <= c."date"
    )
and c."date"=@d1
and e.termination_date is null
and c.fs_perm_sec_id in ('FT9TC5-S-GB','GPHC8W-S-GB','R85KLC-S-US'))

UNION ALL

select 
ff.fs_perm_sec_id,
((FF_debt + ff_pfd_stk + ff_min_int_accum) - FF.ff_cash_st) as [Enterprise Value]
from ff_v2.ff_basic_af_v2 FF
where FF."date" = 
(   select MAX(FF2."date")
    from ff_v2.ff_basic_af_v2 FF2
    where FF2.fs_perm_sec_id=FF.fs_perm_sec_id
    and FF.date <= FF2.date
    ) 
and FF.fs_perm_sec_id in ('FT9TC5-S-GB','GPHC8W-S-GB','R85KLC-S-US')) t
group by t.fs_perm_sec_id

3 个答案:

答案 0 :(得分:2)

只需使用派生表和分组

 Select  fs_perm_sec_id,
        SUM(Enterprise Value) EnterpriseValue 

    from (**your whole code**) 
GROUP BY  fs_perm_sec_id

答案 1 :(得分:0)

使用group by

How to use group by with union in t-sql

SELECT  id,sum(*)
FROM    ( SELECT    id,
                    time
          FROM      dbo.a
          UNION
          SELECT    id,
                    time
          FROM      dbo.b
        )
GROUP BY id

答案 2 :(得分:0)

DECLARE 
      @d1 DATE = '20151222'
    , @fs_perm_sec_id VARCHAR(100) = 'GPHC8W-S-GB'

SELECT @fs_perm_sec_id, SUM([Enterprise Value])
FROM (
    SELECT [Enterprise Value]
    FROM (
        SELECT
              c.fs_perm_sec_id
            , (c.p_price * s.p_com_shs_out) / 1000 AS [Enterprise Value]
            , RowNum = ROW_NUMBER() OVER (ORDER BY s.[date] DESC)
        from fp_v1.fp_basic_bd c
        join fp_v1.fp_basic_sho s on s.fs_perm_sec_id = c.fs_perm_sec_id
        left join edm_v1.edm_security_entity_map e on e.fs_perm_sec_id= c.fs_perm_sec_id
        where c.[date] = @d1
            and e.termination_date is null
            and c.fs_perm_sec_id = @fs_perm_sec_id
    ) t
    WHERE t.RowNum = 1

    UNION ALL

    SELECT FF_debt + ff_pfd_stk + ff_min_int_accum - ff_cash_st
    FROM (
        SELECT
              ff.fs_perm_sec_id
            , FF_debt
            , ff_pfd_stk
            , ff_min_int_accum
            , FF.ff_cash_st
            , RowNum = ROW_NUMBER() OVER (ORDER BY FF.[date] DESC)
        FROM ff_v2.ff_basic_af_v2 FF
        WHERE FF.[date] = 
            AND FF.fs_perm_sec_id = @fs_perm_sec_id
    ) t
    WHERE t.RowNum = 2
) t