Solved

SQL Sub-Select

Posted on 2013-05-28
2
531 Views
Last Modified: 2013-05-28
I am trying to select a count from another table in my query and I cant stop it from duplicating a row when I include the sub selection.  I'm guessing it's because its finding two records in the table it's counting from but I'm expecting just one record to be returned - the count of these records, as there is only one record in the outer selection:

SELECT c.Name, e.EventID, e.DateTime, e.AdditionalComments, 
  (SELECT COUNT(*) FROM AlertedEvents WHERE EventID = e.EventID) AS 'RequestedJoin'
  FROM Events e LEFT JOIN Clubs c ON e.ClubID = c.ClubId
  LEFT JOIN AlertedEvents a ON e.EventID = a.EventID
  WHERE e.UserID = 'blah blah'
  AND e.DateTime > GETDATE()
  AND e.Archived = 'False'

Open in new window


All I want is one field counting the records in AlertedEvents where the EventID is matched (should be 2), instead of replicating a row in the overall query as the outer select query only returns one row.  Any help appreciated!
0
Comment
Question by:Alex_W
2 Comments
 
LVL 15

Accepted Solution

by:
tim_cs earned 500 total points
ID: 39201558
Doesn't look like you are using the AlertedEvents in the results other than the sum so why not just remove the LEFT JOIN to it?


SELECT c.Name, e.EventID, e.DateTime, e.AdditionalComments,
  (SELECT COUNT(*) FROM AlertedEvents WHERE EventID = e.EventID) AS 'RequestedJoin'
  FROM Events e LEFT JOIN Clubs c ON e.ClubID = c.ClubId
   WHERE e.UserID = 'blah blah'
  AND e.DateTime > GETDATE()
  AND e.Archived = 'False'
0
 
LVL 7

Author Closing Comment

by:Alex_W
ID: 39201571
Joining to that table and including the count must have produced the replicated row.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now