Hello - I'm looking for a little advanced sql advice on a project i'm working on. Here's the lowdown:
I have two tables - we'll call them A and B. Table A has one row for every customer in the database and fifteen columns with a number in them specific to the customer. Table B has a hard coded set of decimal numbers that correspond to the columns of Table A. There is no common "join" between the two tables other than the column headers of Table A and a possible field in table B.
What I need to do is perform a rather lengthy math calculation using one field from table A multiplied to the variable in table B. It would be easy to do if I could join the tables together on something - but I'm not aware of a way to join tables from one field to another's column headers. I thought about pulling all the values into a temp table and doing the math from there, but the tables may change structure over time and according to other math formulas.
Is there a better way to get the math done than writing out, say, "select (tablea.field1 * tableb.field1)+(tablea.field2 * tableb.field2)+... on down the line?
Any advice or guidance is appreciated. Thanks!
RJ
Hi Richard,
Are you not able to join Table B to another table which can then be joined to table A? For example:
Select A.SomeField, B.SomeField from A
Join C on A.SomeField = C.SomeField
Join B on C.SomeField = B.someField
and use this to create a SQL view so you can use that for referencing and calculations - it's then like working with just one table.
Cheers,
Kevin
Hi Kevin,
You should be able to pick up the column name from sysobjects or syscolumns. They link on the id field.
Debbie
Hi RJ,
Could you not create a local system table with the following columns?
id, tableA_field_name, tableB_field_name
Then you could create a view that joined something like this:
Select *
from
Table_A a
Join ltr_table_xref x on a.field_1 = x.tableA_field_name
join Table_B b on x.tableB_field_name = b.field1
You could create multiple views or CTEs joining on different entries in your local table depending on your needs. If the calculation is always the same between fields, you could potentially add a formiula column to your table as well: ( (tableA_field1 * tableB field1)/100) and use some dynamic sql to generate your calculated amounts.
In the example here, I added a row to tr_gooesoft_dropdown with a new code for testing purposes, the description field has my formula (cont_amt * 10).
declare @select_statement varchar(1000)
select @select_statement = 'select top 100 customer_no,cont_amt,' + description + ' as new_amt from t_contribution c' from tr_gooesoft_dropdown g where code = 1178
print @select_statement
exec (@select_statement)
You could do the same by substituting your join to the custom system table and using the formula field. Then if formulas ever needed changing, you could do it in the table (1 location) rather than in all referenced views and/or procedures.
Best,
lisa
Thanks! I may give this a shot as right now the “derived” table is rather large. It’s functional, but I’m looking for something more efficient as once we get this working, we will be applying the same logic so far as the build of the procedures to a number of different areas in Tessitura.
Thanks all for the assist!
From: Tessitura Technical Forum [mailto:forums-technical@tessituranetwork.com] On Behalf Of Debbie Harland Sent: Tuesday, March 03, 2015 7:08 AM To: Richard Jackson Subject: Re: [Tessitura Technical Forum] math between tables with no join
From: Kevin Madeira <bounce-kevinmadeira9286@tessituranetwork.com> Sent: 2/22/2015 8:32:55 PM
This message was sent automatically to you by www.tessituranetwork.com because you subscribed to the Tessitura Technical Forum. You may reply to this message to post to the Technical forum or visit the site to search, read and post to the forums. In the interest of keeping the forum posts from becoming cluttered, we encourage you to delete previous message text from your reply before sending. Thank you!
Hi Lisa –
I had thought about that route – and I’m using it on a couple of other tables that I tie to for math. However, the number I’ll be doing the math on this will change (possibly) from 3-6 months and further – depending on the raw data – the actual variables that would be in that system table may change. For example, one calculation looks at, say, column A. However, depending on the outcome of the raw data, we may cease looking at column A and look now at column Z – so it would be a rather large system table with not all fields in use at the same time and I thought maybe that wasn’t the best use of a system table. I could be wrong? It may make the proc as a whole more efficient? I’ll give it a shot in our TEST system and see what I can find out.
From: Tessitura Technical Forum [mailto:forums-technical@tessituranetwork.com] On Behalf Of Lisa Lindvall Sent: Wednesday, March 04, 2015 12:20 PM To: Richard Jackson Subject: Re: [Tessitura Technical Forum] math between tables with no join
From: Richard Jackson <bounce-richardjackson6492@tessituranetwork.com> Sent: 2/19/2015 3:26:31 PM
If you took the table approach, you could just change the table entry from pointing at Column A to Column Z at any time. If you used “inactive” columns, you could retain what you did historically.
I am not sure why it would need to be a large table, if you only needed to join via a few columns at a time.
From: Tessitura Technical Forum [mailto:forums-technical@tessituranetwork.com] On Behalf Of Richard JacksonSent: Thursday, March 05, 2015 11:33 AMTo: llindvall@cfl.rr.comSubject: RE: [Tessitura Technical Forum] math between tables with no join
From: Tessitura Technical Forum [mailto:forums-technical@tessituranetwork.com] On Behalf Of Lisa LindvallSent: Wednesday, March 04, 2015 12:20 PMTo: Richard JacksonSubject: Re: [Tessitura Technical Forum] math between tables with no join
From: Richard Jackson <bounce-richardjackson6492@tessituranetwork.com>Sent: 2/19/2015 3:26:31 PM