If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Multiple tables/joins

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-12-10, 21:10
jenspark jenspark is offline
Registered User
 
Join Date: Oct 2010
Posts: 1
Multiple tables/joins

I have a table with time series data in it and I need to join the table to itself to get previous month information into a view for a reporting tool.

I am using a date reference table to get the date.

The problem is that even using a full outer join, my query drops rows where the data doesn't exist in both tables. I think this is happening because of using the date table, but I'm not sure how else I would do this for multiple time periods.

A simplified version of my SQL is below - Any help is greatly appreciated!

select region,
area, net_bk_amt, prev_m_net_bk_amt
from historytable hst
inner join datetable dt on hst.pd_end_dt = dt.pd_end_dt
full outer join historytable_1 hst1 on
hst.region= hst1.region and hst.area=hst1.area
and hst1.pd_end_dt = dt.prev_month_pd_end_dt
Reply With Quote
  #2 (permalink)  
Old 10-12-10, 21:38
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
you did not explain what the datetable is for, and it appears you have two different history tables

plus, you say you posted a "simplified" version of your query

so i'm not gonna try to rework what you have, i'll just give you some pseudo-code and you can take it from there

first, you can simulate a full outer join with a UNION, and then you can join the summed-up results of the union to some other table...
Code:
SELECT keycolumns
     , SUM(this_month) AS this_month
     , SUM(last_month) AS last_month
  FROM ( SELECT keycolumns
              , datacolumn AS this_month
              , 0          AS last_month
           FROM daTable
          WHERE conditions_for_this_month
         UNION ALL
         SELECT keycolumns
              , 0          AS this_month 
              , datacolumn AS last_month 
           FROM daTable
          WHERE conditions_for_last_month
       ) AS data
INNER
  JOIN someothertable
    ON someothertable.columns = data.keycolumns
helps?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Tags
join, multiple tables, sql join

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On