Hi ASKTOM Team,
Are the two of the below snippets same semantically:
select sNo,
pk,
receipt_no,
GL_Account_code,
receipt_date,
sac_no,
msr_no,
sum(Amount) Amount,
FUND,
FCTR,
ba,
FUNCTIONCODE,
FUNDCODE,
CollDate,
created_at
from (select rr.receipt_no,
2 as sNo,
50 as pk,
rr.receipt_date,
a.sac_no,
sc.msr_no,
(case
when (crtd.tax_id = 1 and crtd.tax_colln_amt >= 0) then
'431100100'
when (crtd.tax_id = 2 and crtd.tax_colln_amt >= 0) then
'431190201'
end) GL_Account_code,
(case
when crtd.tax_id = 1 then
(sum(crtd.tax_colln_amt) * 26 / 30)
when crtd.tax_id = 2 then
sum(crtd.tax_colln_amt)
end) Amount,
CFC.FC_RRLOC_ID FUND,
CFC.FC_FUND_CENTER FCTR,
CFC.FC_BUSINESS_AREA BA,
'99100000000' FUNCTIONCODE,
(case
when crtd.tax_id = 1 then
'11'
when crtd.tax_id = 2 then
'40'
end) FUNDCODE,
rr.receipt_date as CollDate,
rr.created_at
from cv_receipt_sac_detail sc,
cv_receipt_master rr,
cv_assessment a,
cv_fund_collection cfc,
cv_receipt_tax_detail crtd
where rr.rct_id = sc.rct_id
and a.cvp_id = sc.cvp_id
and cfc.fc_rrloc_id = a.ward_id
and crtd.rct_det_id = sc.rcpt_det_id
and rr.status = 'A'
and sc.status = 'A'
and (a.status = 'A' or a.status = 'O')
and crtd.status = 'A'
group by CFC.FC_RRLOC_ID,
CFC.FC_FUND_CENTER,
rr.created_at,
CFC.FC_BUSINESS_AREA,
rr.receipt_date,
crtd.tax_id,
crtd.tax_colln_amt,
a.sac_no,
sc.msr_no,
rr.receipt_no,
rr.receipt_date
union all
select rr.receipt_no,
2 as sNo,
50 as pk,
rr.receipt_date,
a.sac_no,
sc.msr_no,
(case
when (crtd.tax_id = 1 and crtd.tax_colln_amt >= 0) then
'431100200'
end) GL_Account_code,
decode(crtd.tax_id, '1', ((sum(crtd.tax_colln_amt)) * 4 / 30)) Amount,
CFC.FC_RRLOC_ID FUND,
CFC.FC_FUND_CENTER FCTR,
CFC.FC_BUSINESS_AREA BA,
'99100000000' FUNCTIONCODE,
'11' FUNDCODE,
rr.receipt_date as CollDate,
rr.created_at
from cv_receipt_sac_detail sc,
cv_receipt_master rr,
cv_assessment a,
cv_fund_collection cfc,
cv_receipt_tax_detail crtd
where rr.rct_id = sc.rct_id
and a.cvp_id = sc.cvp_id
and cfc.fc_rrloc_id = a.ward_id
and crtd.rct_det_id = sc.rcpt_det_id
and crtd.tax_id = 1
and rr.status = 'A'
and sc.status = 'A'
and (a.status = 'A' or a.status = 'O')
and crtd.status = 'A'
group by CFC.FC_RRLOC_ID,
CFC.FC_FUND_CENTER,
rr.created_at,
CFC.FC_BUSINESS_AREA,
rr.receipt_date,
crtd.tax_id,
crtd.tax_colln_amt,
a.sac_no,
sc.msr_no,
rr.receipt_no,
rr.receipt_date)
where amount > 0
group by GL_Account_code,
FUND,
FCTR,
FUNCTIONCODE,
FUNDCODE,
CollDate,
sNo,
pk,
ba,
sac_no,
msr_no,
receipt_no,
receipt_date,
created_at;
And...
select sNo,
pk,
receipt_no,
GL_Account_code,
receipt_date,
sac_no,
msr_no,
sum(Amount) Amount,
FUND,
FCTR,
ba,
FUNCTIONCODE,
FUNDCODE,
CollDate,
created_at
from (select rr.receipt_no,
2 as sNo,
50 as pk,
rr.receipt_date,
a.sac_no,
sc.msr_no,
(case
when (crtd.tax_id = 1 and crtd.tax_colln_amt >= 0) then
'431100100'
end) GL_Account_code,
decode(crtd.tax_id, '1', ((sum(crtd.tax_colln_amt)) * 26 / 30)) Amount,
CFC.FC_RRLOC_ID FUND,
CFC.FC_FUND_CENTER FCTR,
CFC.FC_BUSINESS_AREA BA,
'99100000000' FUNCTIONCODE,
'11' FUNDCODE,
rr.receipt_date as CollDate,
rr.created_at
from cv_receipt_sac_detail sc,
cv_receipt_master rr,
cv_assessment a,
cv_fund_collection cfc,
cv_receipt_tax_detail crtd
where rr.rct_id = sc.rct_id
and a.cvp_id = sc.cvp_id
and cfc.fc_rrloc_id = a.ward_id
and crtd.rct_det_id = sc.rcpt_det_id
and crtd.tax_id = 1
and rr.status = 'A'
and sc.status = 'A'
and (a.status = 'A' or a.status = 'O')
and crtd.status = 'A'
group by CFC.FC_RRLOC_ID,
CFC.FC_FUND_CENTER,
rr.created_at,
CFC.FC_BUSINESS_AREA,
rr.receipt_date,
crtd.tax_id,
crtd.tax_colln_amt,
a.sac_no,
sc.msr_no,
rr.receipt_no,
rr.receipt_date
union all
select rr.receipt_no,
2 as sNo,
50 as pk,
rr.receipt_date,
a.sac_no,
sc.msr_no,
(case
when (crtd.tax_id = 1 and crtd.tax_colln_amt >= 0) then
'431100200'
end) GL_Account_code,
decode(crtd.tax_id, '1', ((sum(crtd.tax_colln_amt)) * 4 / 30)) Amount,
CFC.FC_RRLOC_ID FUND,
CFC.FC_FUND_CENTER FCTR,
CFC.FC_BUSINESS_AREA BA,
'99100000000' FUNCTIONCODE,
'11' FUNDCODE,
rr.receipt_date as CollDate,
rr.created_at
from cv_receipt_sac_detail sc,
cv_receipt_master rr,
cv_assessment a,
cv_fund_collection cfc,
cv_receipt_tax_detail crtd
where rr.rct_id = sc.rct_id
and a.cvp_id = sc.cvp_id
and cfc.fc_rrloc_id = a.ward_id
and crtd.rct_det_id = sc.rcpt_det_id
and crtd.tax_id = 1
and rr.status = 'A'
and sc.status = 'A'
and (a.status = 'A' or a.status = 'O')
and crtd.status = 'A'
group by CFC.FC_RRLOC_ID,
CFC.FC_FUND_CENTER,
rr.created_at,
CFC.FC_BUSINESS_AREA,
rr.receipt_date,
crtd.tax_id,
crtd.tax_colln_amt,
a.sac_no,
sc.msr_no,
rr.receipt_no,
rr.receipt_date
union all
select rr.receipt_no,
2 as sNo,
50 as pk,
rr.receipt_date,
a.sac_no,
sc.msr_no,
(case
when (crtd.tax_id = 2 and crtd.tax_colln_amt >= 0) then
'431190201'
end) GL_Account_code,
decode(crtd.tax_id, '2', ((sum(crtd.tax_colln_amt)))) Amount,
CFC.FC_RRLOC_ID FUND,
CFC.FC_FUND_CENTER FCTR,
CFC.FC_BUSINESS_AREA BA,
'99100000000' FUNCTIONCODE,
'40' FUNDCODE,
rr.receipt_date as CollDate,
rr.created_at
from cv_receipt_sac_detail sc,
cv_receipt_master rr,
cv_assessment a,
cv_fund_collection cfc,
cv_receipt_tax_detail crtd
where rr.rct_id = sc.rct_id
and a.cvp_id = sc.cvp_id
and cfc.fc_rrloc_id = a.ward_id
and crtd.rct_det_id = sc.rcpt_det_id
and crtd.tax_id = 2
and rr.status = 'A'
and sc.status = 'A'
and (a.status = 'A' or a.status = 'O')
and crtd.status = 'A'
group by CFC.FC_RRLOC_ID,
CFC.FC_FUND_CENTER,
rr.created_at,
CFC.FC_BUSINESS_AREA,
rr.receipt_date,
crtd.tax_id,
crtd.tax_colln_amt,
a.sac_no,
sc.msr_no,
rr.receipt_no,
rr.receipt_date)
where amount > 0
group by GL_Account_code,
FUND,
FCTR,
FUNCTIONCODE,
FUNDCODE,
CollDate,
sNo,
pk,
ba,
sac_no,
msr_no,
receipt_no,
receipt_date,
created_at;
1. What happens when there are some NULL crtd.tax_id
2. In a carefully conducted test like this:
create table test (NO number);
insert four different values 1,2,3 and NULL fifty times. Total 200 Rows
select NO from
(select (case when NO =1 then
'YES'
when no = 2 then
'NO'
end) NO
from test
);
Result : 200 Rows with 50 YES, 50 NO and rest NULL
select no from
(select (case when no =1 then
'YES'
end) NO
from test
where no = 1
union all
select (case when no =2 then
'NO'
end) NO
from test
where no = 2
);
Result : 100 Rows with 50 YES and 50 NO
But for the original problem above the results are identical. I am a bit confused over here. IS it because of Amount > 0 ?
3. What are your thoughts about rewriting this kind of queries.
Thanks for your time.