本文介绍了合并基于同一表的两个查询,并在单个查询中显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这两个查询,我想将它们合并为一个
即:我的第一个查询显示了当年的结果,第二个查询显示了Issues_pending去年的余额,我如何将它们结合起来.

i have these two queries and i want to merge them into one
i.e: my first query show result of current year and second query show issues_pending balance of last year how can i combine them.

SELECT year,director, sum(issues_received) as Newly_Received,sum(issues_solved) as Solved,sum(issues_pending) as Remaining FROM subdivision_master where director=''dhbvn'' group by director,year

select SUM(convert(int,issues_pending)) as previous_balance from subdivision_master where year=CONVERT(int,year)-1


感谢您的任何帮助


any help is appreciated

推荐答案

SELECT  year,
        director,
        sum(issues_received)    as Newly_Received,
        sum(issues_solved)      as Solved,
        sum(issues_pending)     as Remaining,
        -- previous_balance (may have NULLs)
        (
        select  SUM(convert(int,issues_pending)) as previous_balance
        from    subdivision_master
        where   CONVERT(int,year) = CONVERT(int,a.year)-1
        )                       as previous_balance
FROM    subdivision_master a
where   director='dhbvn'
group
by      director,
        year


SELECT year,director, sum(issues_received) as Newly_Received,sum(issues_solved) as Solved,sum(issues_pending) as Remaining FROM subdivision_master where director='dhbvn' group by director,year UNION
select SUM(convert(int,issues_pending)) as previous_balance from subdivision_master where year=CONVERT(int,year)-1



我认为这对您有用



I think this works for you


这篇关于合并基于同一表的两个查询,并在单个查询中显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 07:30