Solved

SQL Sub-Select

Posted on 2013-05-28
2
542 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

624 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