Link to home
Start Free TrialLog in
Avatar of emi_sastra
emi_sastra

asked on

QUEYR USING TABLE DATA AS FILTER

Hi All,

I have below tables :

1. TMLEDGER
    AccNo
    Amount

2. TMFORM
    FormNo
    FormAccNo
    FromAccNo
    ToAccNo

3. TDFORMDATA
    FormNo
    FormAccNo
    FormAmount

I want to query using table 1 and 2 to get result into table 3.

How could I do it ?

Thank you.
Avatar of emi_sastra
emi_sastra

ASKER

More info.

TMFORM as the table for filtering data from TMLEDGER.

Thank you.

Avatar of Lowfatspread
what is the relation of the account numbers on the form table?

FromAccNo
    ToAccNo


how do they relate to

accno on tmledger ?
is this what you want ?

insert into tdformdata
 (formno,formaccno,formamount)
select formno,formaccno,sum(amount)
from tmledger as l
inner join tmform as f
on l.accno between f.fromaccno and f.toaccno
group by formno,formaccno

Open in new window

please specify the relationship between tables ....
ASKER CERTIFIED SOLUTION
Avatar of Lowfatspread
Lowfatspread
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi All,

I am sorry just back.

Thank you.
Hi All,


FromAccNo
    ToAccNo is related to TMLEDGER AccNo.

Thank you.
OK  Emi that's fine..
one more thing how do you want to calculate the amount in 3rd table ?
May be you want to subtract the FromAccNo amount to ToAccNo amount or addition , or may you want to directly insert or update the first table  amt to third table amt based upon FromAccNo or ToAccNo...?
I want to sum (addition) to the result table base on formaccno.

Thank you.

<< I want to sum (addition) to the result table base on formaccno. >>

ok here is your code..

insert into tdformdata
 (FormNo,FormAccNo,FormAmount)
select tm.FormNo,tm.FormAccNo,sum(tl.Amount)
from tmledger as tl
inner join tmform as tf
on tl.AccNo=tf.FormAccNo 
group by tf.FormNo,tf.FormAccNo

Open in new window

For example :

FormAccNo      FromAccNo   ToAccNo
100                 100.001       100.010
100                 100.020       100.090
200                 200.001       300.001



Thus Result is sum from AccNo 100 = 100.001  to  100.010 + 100.020  To  100.090

Thank you.
then my answer 36480926 should do that...

unless there is a problem with the datatype you have for account...

what data types are you using for the account number columns?
Yes, I've just tested it.

Thank you very much for your help.