Home > Ask the SQL Server Experts > Questions & Answers > Joining derived tables
Ask The SQL Server Expert: Questions & Answers
EMAIL THIS

Joining derived tables

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other SQL Server Categories
Meet all SQL Server Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 10 December 2004
I have three tables -- table1, table2, and table3. All three contain information on a daily basis. I have written SQL statements to summarize the information of each table based on certain columns in the respective tables. Can I write an SQL statement where in it should perform the summarization of each of the three tables separately and then the summarized results be joined together using where clause? The SQL statements should not create any individual summary tables.

>

Use a derived table for each query, and join them on their common grouping column. For example, if the grouping column is userid, you would use this:

select dt1.userid
     , dt1.sumcol1 as table1_sumcol1
     , dt2.sumcol1 as table2_sumcol1
     , dt3.sumcol1 as table3_sumcol1
  from (
       select userid
            , sum(col1) as sumcol1
         from table1
       group
           by userid
       ) as dt1
inner
  join (
       select userid
            , sum(col1) as sumcol1
         from table2
       group
           by userid
       ) as dt2
    on dt1.userid = dt2.userid    
inner
  join (
       select userid
            , sum(col1) as sumcol1
         from table3
       group
           by userid
       ) as dt3
    on dt1.userid = dt3.userid

Notice how the derived tables each have an alias. Also, in the SELECT list of the outer query, notice that column aliases are needed to distinguish the columns from each derived table.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



SQL Solutions - SQL Database Design
HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts