Join on a view without the view

Reply

Join Date: Apr 2009
Posts: 4
Reputation: mr_vodka is an unknown quantity at this point 
Solved Threads: 0
mr_vodka mr_vodka is offline Offline
Newbie Poster

Join on a view without the view

 
0
  #1
Apr 21st, 2009
Hi I was wondering what the syntax would be to have a left join from one table to a resulting query without the view. Currently I use a view and it works fine but I was wondering how to achieve this without creating the view.

Here is the current view's SQL statement ( without the create view etc)

VIEW NAME: vw_2009
  1. SELECT
  2. fundinfo.fk_budget_ID,
  3. fundinfo.fk_tiertwo_ID,
  4. Sum(fundlink.fundlink_amt)
  5. FROM (fundinfo LEFT JOIN fundlink on((fundinfo.pk_fund_ID = fundlink.fk_fund_ID`)))
  6. WHERE ((fundlink.date_create <= '2009-03-15') AND (fundlink.date_create >= '2009-01-01') AND (fundinfo.fundinfo_budg_yr = '2009'))
  7. GROUP BY fundinfo.fk_tiertwo_ID



Here is the query that is using that view on the right side.

  1. SELECT
  2. budget.budget_name,
  3. vw_2009.Sum(fundlink.fundlink_amt)
  4. FROM
  5. budget
  6. LEFT JOIN vw_2009 ON budget.pk_budget_ID = vw_2009.fk_budget_ID


So how do I combine them into one statement? I tried creating a subquery but I couldnt get it working correctly.

TIA.
Last edited by mr_vodka; Apr 21st, 2009 at 2:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 793
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Join on a view without the view

 
0
  #2
Apr 21st, 2009
Hi mr_vodka and welcome to DaniWeb

Love the username btw!

If all you want from the budget table is the budget_name field, you could just try this:
  1. SELECT
  2. fundinfo.fk_budget_ID,
  3. fundinfo.fk_tiertwo_ID,
  4. Sum(fundlink.fundlink_amt),
  5. budget.budget_name
  6. FROM fundinfo
  7. LEFT JOIN fundlink
  8. ON fundinfo.pk_fund_ID = fundlink.fk_fund_ID
  9. LEFT JOIN budget
  10. ON budget.pk_budget_ID = fundinfo.fk_budget_ID
  11. WHERE fundlink.date_create <= '2009-03-15' AND fundlink.date_create >= '2009-01-01' AND fundinfo.fundinfo_budg_yr = '2009'
  12. GROUP BY fundinfo.fk_tiertwo_ID, budget.budget_name

Hope this helps,
darkagn
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 4
Reputation: mr_vodka is an unknown quantity at this point 
Solved Threads: 0
mr_vodka mr_vodka is offline Offline
Newbie Poster

Re: Join on a view without the view

 
0
  #3
Apr 22nd, 2009
Originally Posted by darkagn View Post
Hi mr_vodka and welcome to DaniWeb
Thanks for the welcome and assistance darkagn.

I tried your suggestion but its a no go. It still will not return all the records in the budget table.

I even tried to switch the order of the joins by putting the budget to Fund LEFTJOIN first but it didnt help.


Currently, when I am using the simple LEFTJOIN with the view it will result in 15 records. If I use the statement that you provided, it will only show 3.

I figure that it was a matter or order or parathensis but I just seem to get it correct. I dont want to have to create a temp view just to achieve this. I am hoping that there is a way to do it with one SQL statement.

Thanks again.
Last edited by mr_vodka; Apr 22nd, 2009 at 11:38 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 4
Reputation: mr_vodka is an unknown quantity at this point 
Solved Threads: 0
mr_vodka mr_vodka is offline Offline
Newbie Poster

Re: Join on a view without the view

 
0
  #4
Apr 24th, 2009
Bump... Anyone?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 79
Reputation: varmadba is an unknown quantity at this point 
Solved Threads: 8
varmadba varmadba is offline Offline
Junior Poster in Training

Re: Join on a view without the view

 
0
  #5
Apr 29th, 2009
Try this

  1. SELECT
  2. budget.budget_name,
  3. total.fundlink.fundlink_amt
  4. FROM
  5. budget
  6. LEFT JOIN
  7. (
  8. SELECT
  9. fundinfo.fk_budget_ID,
  10. fundinfo.fk_tiertwo_ID,
  11. Sum(fundlink.fundlink_amt)
  12. FROM (fundinfo LEFT JOIN fundlink on((fundinfo.pk_fund_ID = fundlink.fk_fund_ID`)))
  13. WHERE ((fundlink.date_create <= '2009-03-15') AND (fundlink.date_create >= '2009-01-01') AND (fundinfo.fundinfo_budg_yr = '2009'))
  14. GROUP BY fundinfo.fk_tiertwo_ID
  15. )total
  16. ON budget.pk_budget_ID = total.fk_budget_ID
Last edited by varmadba; Apr 29th, 2009 at 8:43 am.
:- Varma

We are Happy to inform launch of a new site with loads of database related information Site offers wide range of functionality Forums,Blogs,Articles,Editorials and much more
http://www.sqllibrarian.info/
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 4
Reputation: mr_vodka is an unknown quantity at this point 
Solved Threads: 0
mr_vodka mr_vodka is offline Offline
Newbie Poster

Re: Join on a view without the view

 
0
  #6
May 28th, 2009
Great. Much appreciated. I couldnt get passed the setting of the "total" alias. Thanks for helping me out.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC