Solved

QUEYR USING TABLE DATA AS FILTER

Posted on 2011-09-04
13
209 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:emi_sastra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
13 Comments
 
LVL 1

Author Comment

by:emi_sastra
ID: 36480507
More info.

TMFORM as the table for filtering data from TMLEDGER.

Thank you.

0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 36480684
what is the relation of the account numbers on the form table?

FromAccNo
    ToAccNo


how do they relate to

accno on tmledger ?
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 36480753
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

0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 12

Expert Comment

by:Saurabh Bhadauria
ID: 36480820
please specify the relationship between tables ....
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 36480926
or perhaps this if you always want a row per form account ?

insert into tdformdata
 (formno,formaccno,formamount)
select formno,formaccno,coalesce(sum(amount),0)

from tmform as f
left outer join tmledger as l
on l.accno between f.fromaccno and f.toaccno
group by formno,formaccno

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 36481940
Hi All,

I am sorry just back.

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 36481942
Hi All,


FromAccNo
    ToAccNo is related to TMLEDGER AccNo.

Thank you.
0
 
LVL 12

Expert Comment

by:Saurabh Bhadauria
ID: 36482161
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...?
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 36482235
I want to sum (addition) to the result table base on formaccno.

Thank you.
0
 
LVL 12

Expert Comment

by:Saurabh Bhadauria
ID: 36482363

<< 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

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 36482465
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.
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 36483173
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?
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 36483303
Yes, I've just tested it.

Thank you very much for your help.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to import SQL 2000 database to SQL 2014 5 157
sql query help 2 62
Replace Dates in query 14 55
SSRS 2013 - Finding Datasets/StoredProcedures 4 35
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question