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.
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 -
SELECT TOP (100) PERCENT
-
master.blogCategoryName,
-
COUNT(subQuery.blogCounterID) AS Count,
-
master.blogCategoryID,
-
master.blogViewType
-
FROM dbo.tblBlogCategories master
-
LEFT OUTER JOIN
-
(SELECT * FROM dbo.tblBlogCounter
-
WHERE (blogCounterDate > (GETDATE() - .001)))
-
subQuery
-
ON master.blogCategoryID =
-
subQuery.blogCategoryID
-
GROUP BY master.blogCategoryName,
-
master.blogCategoryID,
-
master.blogViewType
-
HAVING (master.blogViewType IN (1, 4))
-
ORDER BY master.blogViewType DESC,
-
master.blogCategoryName ASC
-
4 1787
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 -
SELECT TOP (100) PERCENT
-
master.blogCategoryName,
-
COUNT(subQuery.blogCounterID) AS Count,
-
master.blogCategoryID,
-
master.blogViewType
-
FROM dbo.tblBlogCategories master
-
LEFT OUTER JOIN
-
(SELECT * FROM dbo.tblBlogCounter
-
WHERE (blogCounterDate > (GETDATE() - .001)))
-
subQuery
-
ON master.blogCategoryID =
-
subQuery.blogCategoryID
-
GROUP BY master.blogCategoryName,
-
master.blogCategoryID,
-
master.blogViewType
-
HAVING (master.blogViewType IN (1, 4))
-
ORDER BY master.blogViewType DESC,
-
master.blogCategoryName ASC
-
Thanks I'll have a play and let you know how I get on.
Regards
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?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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'...
|
by: Notgiven |
last post by:
I have three tables:
table1:
table2_ID
table3_ID
complete
table3:
table3_ID
name
|
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...
|
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
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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,...
|
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...
| |