所以我有一系列摊还时间表,我想运行一个查询,以便在每个时间表上返回五月份的余额
我现在的查询,看起来像这样
select ace.date, ace.balance, aacs.date, aacs.balance,
axl.date, axl.balance, la.date, la.balance,
mrh.date, mrh.balance
from
(select *
from [Entity1]
where MONTH (date) = 05 AND YEAR (date) = 2017) as ACE
full join
(select *
from [Entity2]
where MONTH (date) = 05 AND YEAR (date) = 2017) as AACS
on ACE.GLCredited = AACS.GLCredited
full join
(select *
from [Entity3]
where MONTH (date) = 05 AND YEAR (date) = 2017) as AXL
on AXL.GLCredited = AACS.GLCredited
full join
(select *
from [Entity4]
where MONTH (date) = 05 AND YEAR (date) = 2017) as LA
on LA.GLCredited = AXL.GLCredited
full join
(select *
from [Entity5]
where MONTH (date) = 05 AND YEAR (date) = 2017) as MRH
on MRH.GLCredited = LA.GLCredited
这是它返回的内容
date | balance | date | balance | date | balance | date | balance | date | balance |
------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
NULL | NULL | 2017-05-31 | 563275.00 | NULL | NULL | NULL | NULL | NULL | NULL |
------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
2017-05-31 | 896337.00 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
NULL | NULL | NULL | NULL | 2017-05-31 | 3746167.00 | NULL | NULL | NULL | NULL |
------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
NULL | NULL | NULL | NULL | NULL | NULL | 2017-05-01 | 474774.00 | NULL | NULL |
------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2017-05-31 | 127987.00 |
有没有你知道的方法,那将返回没有空值的数据?
答案 0 :(得分:1)
您可以使用union而不是join来执行此操作。类似的东西:
select date, balance
from [Entity1]
where MONTH (date) = 05 AND YEAR (date) = 2017) as ACE
union
select date, balance
from [Entity2]
where MONTH (date) = 05 AND YEAR (date) = 2017) as AACS
on ACE.GLCredited = AACS.GLCredited
union
select date, balance
from [Entity3]
where MONTH (date) = 05 AND YEAR (date) = 2017) as AXL
on AXL.GLCredited = AACS.GLCredited
union
select date, balance
from [Entity4]
where MONTH (date) = 05 AND YEAR (date) = 2017) as LA
on LA.GLCredited = AXL.GLCredited
union
select date, balance
from [Entity5]
where MONTH (date) = 05 AND YEAR (date) = 2017) as MRH
on MRH.GLCredited = LA.GLCredited
答案 1 :(得分:1)
您可以使用UNION
:
Select *
FROM
(
select 'ACE' as ScheduleName, date, balance from [Entity1]
union
select 'AACS' as ScheduleName, date, balance from [Entity2]
union
select 'AXL' as ScheduleName, date, balance from [Entity3]
union
select 'LA' as ScheduleName, date, balance from [Entity4]
union
select 'MRH' as ScheduleName, date, balance from [Entity5]
)u
where MONTH (date) = 05 AND YEAR (date) = 2017
答案 2 :(得分:0)
您可以轻松地在所有子查询中添加IS NOT NULL。因此,从子查询中获得的返回值不包括NULL
....
from
from [Entity1]
where MONTH (date) = 05 AND YEAR (date) = 2017 and *DATE is not null*) as ACE
join
(select *
from [Entity2]
where MONTH (date) = 05 AND YEAR (date) = 2017 *and BALANCE is not null*) as AACS
on ACE.GLCredited = AACS.GLCredited
join