foxpro - SQL query to select a column with expression of non-aggregate value and aggregate function -
tables used:
1) v(date d, name c(25), desc c(50), debit n(7), credit n(7))
name in 'v' refers name in vn table
2) vn(date d, name c(25), type c(25), obal n(7))
name in 'vn' primary key , different names grouped type
ex: names abc, def, ghi belongs type 'bank', names xyz, pqr belongs type 'ledger', ...
i've query this:
select vn.type, sum(vn.obal + iif(v.date < sd, v.credit-v.debit, 0)) opbal, ; sum(iif(between(v.date, sd, ed), v.credit-v.debit, 0)) curbal ; v, vn v.name = vn.name group vn.type ; order vn.type having opbal + curbal != 0
it works fine problem is, obal value entered once per name in table 'vn' query every calculation of credit-debit in table 'v', obal added multiple times , displayed under opbal. when query modified below:
select vn.type, vn.obal + sum(iif(v.date < sd, v.credit-v.debit, 0)) opbal, ; sum(iif(between(v.date, sd, ed), v.credit-v.debit, 0)) curbal ; v, vn v.name = vn.name group vn.type ; order vn.type having opbal + curbal != 0
it shows error message 'group clause missing or invalid'!
rdbms used ms visual foxpro 9. sd , ed date type variables used purpose of query sd < ed.
please me out getting expected result. lot.
i saw sql syntax sql vfp first time few minutes ago, full of errors, 'guessful hunch':
select vn.type, sum(vn.obal + (select sum(iif(v.date < sd, v.credit-v.debit, 0)) v v.name = vn.name)) opbal, sum(select sum(iif(between(v.date, sd, ed), v.credit-v.debit, 0)) v v.name = vn.name) curbal vn group vn.type order vn.type having opbal + curbal != 0
basically, i've turned selection v subselects avoid vn.obal repeated. shouldn't matter v first gets sum individual person before summing them together.
Comments
Post a Comment