您好,同一个表有两个不同的查询,条件不同,但是我只想在查询2的右边带1列。
select a.company_code, a.fb_id, a.account_code, a.cost_center,
Round(sum(case when a.drcr_flag = 'Cr' then -1* a.base_amount else a.base_amount end),2) ytd_base_current,
Round(sum(case when b.drcr_flag = 'Cr' then -1* b.base_amount else b.base_amount end),2) ytd_base_Pervious
from scmdb..fbp_posted_trn_dtl (nolock) a, scmdb..fbp_posted_trn_dtl b
where a.company_code = 'b5'
and a.posting_date BETWEEN '2019-01-01' AND '2019-01-31'
and b.company_code = 'b5'
and b.posting_date BETWEEN '2018-01-01' AND '2018-12-31'
group by a.company_code, a.fb_id, a.account_code, a.cost_center
查询将永远执行。
答案 0 :(得分:0)
使用显式连接而不是逗号分隔的连接
select a.company_code, a.fb_id, a.account_code, a.cost_center,
Round(sum(case when a.drcr_flag = 'Cr' then -1* a.base_amount else a.base_amount end),2) ytd_base_current,
Round(sum(case when b.drcr_flag = 'Cr' then -1* b.base_amount else b.base_amount end),2) ytd_base_Pervious
from scmdb..fbp_posted_trn_dtl a inner join scmdb..fbp_posted_trn_dtl b
on a.company_code = b.company_code
where a.company_code ='b5'
and a.posting_date BETWEEN '2019-01-01' AND '2019-01-31'
group by a.company_code, a.fb_id, a.account_code, a.cost_center
答案 1 :(得分:-1)
尝试一下...
select a.company_code, a.fb_id, a.account_code, a.cost_center,
Round(sum(case when a.drcr_flag = 'Cr' then -1* a.base_amount else a.base_amount end),2) ytd_base_current,
Round(sum(case when b.drcr_flag = 'Cr' then -1* b.base_amount else b.base_amount end),2) ytd_base_Pervious
from scmdb..fbp_posted_trn_dtl (nolock) a
join scmdb..fbp_posted_trn_dtl b
on a.company_code = b.company_code
and a.fb_id = b.fb_id
and a.account_code = b.account_code
and a.cost_center = b.cost_center
where a.company_code = 'b5'
and a.posting_date BETWEEN '2019-01-01' AND '2019-01-31'
and b.posting_date BETWEEN '2018-01-01' AND '2018-12-31'
group by a.company_code, a.fb_id, a.account_code, a.cost_center