Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Sub-Select

Posted on 2013-05-28
2
Medium Priority
?
547 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 2000 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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

715 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