Solved

SQL Sub-Select

Posted on 2013-05-28
2
540 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

738 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