连接2个没有共同点的选择语句

时间:2018-12-10 09:38:23

标签: sql-server join select left-join outer-join

我有执行不同任务的存储过程,但是我想将其中一些组合在一起,以减少在数据库中创建的存储过程的数量。 许多程序都基于年份统计,这是一个示例:

Select 
DATENAME(MONTH, DATE_) AS Date,
 count(distinct VATNUMBER) As Count
from 
   (
    select VATNUMBER, DATE_ = min(DATE_)
    from
    ACCOUNTS a inner join CUSTTABLE b
    on a.ID= b.ID
    where ITEMNUMBER in(7055,7056,7059) AND YEAR(a.DATE_) = '2017'
    AND VATNUMBER NOT IN (
    select VATNUMBER
    from
    ACCOUNTS a inner join CUSTTABLE b
    on a.ID= b.ID
    where ITEMNUMBER in(7055,7056,7059)
    AND DATE_ < '2017-01-01'
)
group by VATNUMBER
) d
group by MONTH(DATE_), DATENAME(MONTH, DATE_)
Order by MONTH(DATE_)

2017年的结果:

    Date     |   Count
    January  |   61
    February |   43
    March    |   23
    April    |   48
    etc....

现在我对2018年有相同的程序:

 Select 
    DATENAME(MONTH, DATE_) AS Date,
     count(distinct VATNUMBER) As Count
    from 
       (
        select VATNUMBER, DATE_ = min(DATE_)
        from
        ACCOUNTS a inner join CUSTTABLE b
        on a.ID= b.ID
        where ITEMNUMBER in(7055,7056,7059) AND YEAR(a.DATE_) = '2018'
        AND VATNUMBER NOT IN (
        select VATNUMBER
        from
        ACCOUNTS a inner join CUSTTABLE b
        on a.ID= b.ID
        where ITEMNUMBER in(7055,7056,7059)
        AND DATE_ < '2018-01-01'
    )
    group by VATNUMBER
    ) d
    group by MONTH(DATE_), DATENAME(MONTH, DATE_)
    Order by MONTH(DATE_)

2018年的结果:

    Date     |   Count
    January  |   119
    February |   200
    March    |   112
    April    |   87
    etc....

我想做的是将两个select语句放在同一存储过程下。我想看到的结果是:

    Date 2017|   Count  | Date 2018|   Count
    January  |   61     | January  |   119
    February |   43     | February |   200
    March    |   23     | March    |   112
    April    |   48     | April    |   87
    etc....

0 个答案:

没有答案