sql - applying DISTINCT SUM -
i'm working on code me sum amount of gl sub heads (that's accounting term) in table. things is, there multiple rows same gl sub head different amounts. need sum amounts of table per gl sub head. of course, involves use of sum , distinct clauses. here's have far:
select gstt.bal_date, sum(gstt.tot_dr_bal) totaldr, sum(gstt.tot_cr_bal) totalcr from( select distinct gam.gl_sub_head_code, gstt.tot_dr_bal, gstt.tot_cr_bal tbaadm.gam left outer join tbaadm.eab on gam.acid = eab.acid , gam.bank_id = eab.bank_id left outer join tbaadm.gstt on gam.sol_id = gstt.sol_id , gam.bank_id = gstt.bank_id , gam.gl_sub_head_code = gstt.gl_sub_head_code , gam.acct_crncy_code = gstt.crncy_code gam.acct_ownership = 'o' --and eab.eod_date = to_date('3/24/2014', 'mm/dd/yyyy') , gstt.bal_date = to_date('3/24/2014', 'mm/dd/yyyy') ) group gstt.bal_date, totaldr, totalcr can't make bloody thing work, though. know i'm missing something, can't put finger on is, , should put in code. appreciated. if need further clarifications, ask.
edit
i've got code work now, somewhat. because there's problem.
select bal_date, gl_sub_head_code, sum(tot_dr_bal), sum(tot_cr_bal) from( select distinct gam.gl_sub_head_code, gstt.tot_dr_bal, gstt.tot_cr_bal, gstt.bal_date tbaadm.gam left outer join tbaadm.eab on gam.acid = eab.acid , gam.bank_id = eab.bank_id left outer join tbaadm.gstt on gam.sol_id = gstt.sol_id , gam.bank_id = gstt.bank_id , gam.gl_sub_head_code = gstt.gl_sub_head_code , gam.acct_crncy_code = gstt.crncy_code gam.acct_ownership = 'o' --and eab.eod_date = to_date('3/24/2014', 'mm/dd/yyyy') , gstt.bal_date = to_date('3/24/2014', 'mm/dd/yyyy') ) group bal_date, gl_sub_head_code as can see, there distinct clause @ beginning of subquery. whenever remove that, toad outputs same set of results. same results regardless if have distinct clause or not. have feeling there's wrong code, i'm expecting distinct should make difference.
"i same results regardless if have distinct clause or not."
this means query returns unique set of rows.
i have feeling there's wrong code, i'm expecting distinct should make difference.
why think that?
distinct made difference in first query because hadn't included bal_date in sub-query's projection. have gl_sub_head instances balances same multiple days.
except filtering on bal_date should 1 value anyway. hmmmm....
clearly awry in query. there 2 obvious puzzlers.
why have left outer join
gstt? hard filter ongstt.bal_datemeans return recordsgstt.bal_datenot null, result set still same if inner join.why bother querying
eab@ all? don't use values , left outer join means doesn't restrict values retrievedgamin anyway. plus fact have commented out filter oneab.eod_datesuggests produces cross join might generate multiple duplicate values think need distinct eradicate.
so, don't have actual answer you, think need re-visit business logic , figure out query needs do.
Comments
Post a Comment