Author |
Topic |
Timmy6
Starting Member
8 Posts |
Posted - 2009-01-19 : 09:54:30
|
I am relatively new to SQL but have a long background in Excel, and have been running a calculation in Excel for sometime now that is becomming too labour intensiveTherefore I would like to explore another method of calculating the numbers I needIn its simplest form I need to take one table of numbers, cross reference against another table which holds percentages and split the original table onto new lines by applying the percentages from the second tableI am assuming this can be done in SQL but I cannot find any examplesPlease let me know if the request is unclear |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-19 : 09:56:57
|
can you provide some sample data to illustrate your scenario and explain what you want with reqd output |
|
|
Timmy6
Starting Member
8 Posts |
Posted - 2009-01-19 : 10:00:59
|
No problemTable 1Division, ValueD1, 160D2, 200Table 2Division, Region, Regional ApportionmentD1, R1, 50%D1, R2, 30%D1, R3, 20%Then using this data to create a new TableTable NewRegion, ValueR1, 80R2, 48R3, 32Hope thats clearer |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-19 : 10:51:06
|
[code]INSERT TableNew(Region,[Value])SELECT t2.Region,t1.Value * t2.[Regional Apportionment]/100FROM Table1 t1INNER JOIN Table2 t2ON t2.Division=t1.Division[/code] |
|
|
Timmy6
Starting Member
8 Posts |
Posted - 2009-01-19 : 12:06:07
|
Thanks for that I will try when I get homeJust to clarify what are t1 and t2 referring too are they new objects or have you just abbreviated Table 1 and Table 2 which I used in my example |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2009-01-19 : 12:19:29
|
T1 & T2 are "aliases" for Table1 & Table2...it's a shorthand notation that becomes useful when you work with long table names i.e....mytablenameisverylongandanythingthislongis veryhardtoreadwhenembeddedinstoredprocedures |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-19 : 12:40:31
|
quote: Originally posted by AndrewMurphy T1 & T2 are "aliases" for Table1 & Table2...it's a shorthand notation that becomes useful when you work with long table names i.e....mytablenameisverylongandanythingthislongis veryhardtoreadwhenembeddedinstoredprocedures
also when you want to use same table more than once in same query, they help you to distinguish each instance. |
|
|
Timmy6
Starting Member
8 Posts |
Posted - 2009-01-19 : 16:26:08
|
Can I use these aliases in the actual SQL statement though or do I have to replace them with the proper namesI would of thought id have to define the aliases as in VBA before I can use them |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-20 : 02:57:39
|
yup...you can. nope you dont need to define aliases in t-sql seperately. you can just use them in selects,updates,deletes,... |
|
|
|