473,422 Members | 2,224 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,422 software developers and data experts.

Outer join behaving like inner?

jonpanky
Hi,

I'm new to this forum, so I hope I have the correct place to post this...

I have a query below that is not returning the results I need:

SELECT TOP (100) PERCENT dbo.tblBlogCategories.blogCategoryName,
COUNT(dbo.tblBlogCounter.blogCounterID) AS Count,
dbo.tblBlogCategories.blogCategoryID,
dbo.tblBlogCategories.blogViewType
FROM dbo.tblBlogCategories LEFT OUTER JOIN
dbo.tblBlogCounter ON
dbo.tblBlogCategories.blogCategoryID = dbo.tblBlogCounter.blogCategoryID
WHERE (dbo.tblBlogCounter.blogCounterDate > GETDATE() - .001) OR
(dbo.tblBlogCounter.blogCounterDate > GETDATE() - .001)
GROUP BY dbo.tblBlogCategories.blogCategoryName,
dbo.tblBlogCategories.blogCategoryID, dbo.tblBlogCategories.blogViewType
HAVING (dbo.tblBlogCategories.blogViewType = 1) OR
(dbo.tblBlogCategories.blogViewType = 4)
ORDER BY dbo.tblBlogCategories.blogViewType DESC,
dbo.tblBlogCategories.blogCategoryName

I would like this query to return all results from the left table and show results from the right table that have dates newer than a certain value.

The moment I add the getdate clause it will only return matched results like an inner join, any ideas how I force it to return all the results from the left table as well?

Even though I have used the Outer Left Join criteria it still only shows results that have a join with the right table.

Many thanks for any help.
Jul 20 '10 #1

✓ answered by Oralloy

This may sound stupid, but don't you have to do the LEFT OUTER JOIN to a subselect, instead of selecting from the joined records?

Unfortunately I'm not sure of the syntax, but it would be something like

Expand|Select|Wrap|Line Numbers
  1. SELECT TOP (100) PERCENT
  2.        master.blogCategoryName,
  3.        COUNT(subQuery.blogCounterID) AS Count,
  4.        master.blogCategoryID,
  5.        master.blogViewType
  6. FROM dbo.tblBlogCategories master
  7.      LEFT OUTER JOIN
  8.      (SELECT * FROM dbo.tblBlogCounter
  9.       WHERE (blogCounterDate > (GETDATE() - .001)))
  10.       subQuery
  11.      ON master.blogCategoryID =
  12.         subQuery.blogCategoryID
  13. GROUP BY master.blogCategoryName,
  14.          master.blogCategoryID,
  15.          master.blogViewType
  16. HAVING (master.blogViewType IN (1, 4))
  17. ORDER BY master.blogViewType DESC,
  18.          master.blogCategoryName ASC
  19.  

4 1787
Oralloy
988 Expert 512MB
This may sound stupid, but don't you have to do the LEFT OUTER JOIN to a subselect, instead of selecting from the joined records?

Unfortunately I'm not sure of the syntax, but it would be something like

Expand|Select|Wrap|Line Numbers
  1. SELECT TOP (100) PERCENT
  2.        master.blogCategoryName,
  3.        COUNT(subQuery.blogCounterID) AS Count,
  4.        master.blogCategoryID,
  5.        master.blogViewType
  6. FROM dbo.tblBlogCategories master
  7.      LEFT OUTER JOIN
  8.      (SELECT * FROM dbo.tblBlogCounter
  9.       WHERE (blogCounterDate > (GETDATE() - .001)))
  10.       subQuery
  11.      ON master.blogCategoryID =
  12.         subQuery.blogCategoryID
  13. GROUP BY master.blogCategoryName,
  14.          master.blogCategoryID,
  15.          master.blogViewType
  16. HAVING (master.blogViewType IN (1, 4))
  17. ORDER BY master.blogViewType DESC,
  18.          master.blogCategoryName ASC
  19.  
Jul 20 '10 #2
Thanks I'll have a play and let you know how I get on.

Regards
Jul 20 '10 #3
Wow it worked, thanks so much. :)

Learnt a lot from that, did not realise that you could use subqueries in that way.

Is the perfomance of a subquery better than using views?
Jul 21 '10 #4
Oralloy
988 Expert 512MB
Super. Did my SQL work verbatim, or did you have to make changes? Since I pulled that out of the ether, so to speak, I'd like to see the working code. (I'm pretty new with subqueries myself).

As for subqueries being faster than views, I can't say. Likely it's a database server issue, and I'm sure that we have folks on bytes who are much more knowledgable than I am.

I guess the choice of which to use is mostly philosophic - Views are persistent and sharable - Subqueries are local to a bit of logic.

Overriding all of this are your role and responsibilities within the organization - Do you have the authority to create a view within the database? Is there a process you have to follow to have a DBA create a view? Or, are you stuck maintaining SQL embedded in your programming logic?

It might be that the controlling issue is maintenence - where and who is going to maintain and update this bit of SQL? Is this a one-off query, or is there critical data which will flow through this query on a regular basis?

Sorry ... I digressed ...

Thanks for the feedback and have yourself an excellent afternoon.
Jul 21 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: mheydman | last post by:
I apologize if this has been asked before- I searched google but could not find a concrete answer. I recently inherited a database whose t-sql code is written in a format that I find difficult...
1
by: TeleTech1212 | last post by:
I am trying to select specific columns from multiple tables based on a common identifier found in each table. For example, the three tables: PUBACC_AC PUBACC_AM PUBACC_AN each have a...
8
by: Matt | last post by:
Hello I have to tables ar and arb, ar holds articles and a swedish description, arb holds descriptions in other languages. I want to retreive all articles that match a criteria from ar and...
5
by: jason.evans | last post by:
Hi there. I am having an intrigueing problem. I have a query which left joins another query to itself twice. The original query is derived from a linked table in SQLServer 2000. When I run...
5
by: kumar_rangan1976 | last post by:
I need the below sybase code to be migrated in UDB : select distinct c.partnumber as I_PART, case when d.IntegratorID = 'DCX05' then 'U' when d.IntegratorID = 'DCX04' then 'M'...
2
by: Notgiven | last post by:
I have three tables: table1: table2_ID table3_ID complete table3: table3_ID name
3
by: Doug | last post by:
Hi, I'm more familiar with MSSQL than Access syntax and have run into a problem with correctly putting ( )'s around the joins in a 3 table query. I want to INNER JOIN lenders and accounts and...
12
by: Chamnap | last post by:
Hello, everyone I have one question about the standard join and inner join, which one is faster and more reliable? Can you recommend me to use? Please, explain me... Thanks Chamnap
1
by: teneesh | last post by:
Here I have a code for a view that has been created by a developer on my team. I am trying to use the very same code to create a view for a different formid/quesid. But I cannot figure out how this...
1
pradeepjain
by: pradeepjain | last post by:
Hii guys, I might be silly to ask this ques..but seriously i did not understand the outer join and inner join in mysql site.i understood left and right join also. can some one explain...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.