Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2004
    Location
    Dundee, Scotland
    Posts
    45

    Question Unanswered: SubQuery in both SELECT and WHERE part. How to optimise?

    Ok I have the following SQL, I have a subquery in the SELECT part but also the same subquery in the WHERE part as well.

    What I'm trying to do is get all parents that have children OR get all parents with no children OR just get all parents regardless (@HasResponses is a BIT that can be 1, 0 or null). At the same time I want to count the total number of children in the select list, but I'm having to copy the same subquery in the SELECT and WHERE parts which doesn't seem terribly optimal to me (maybe it is, that's why I'm asking). I've tried referencing the column alias in the select list (AS [Responses]) for the where part (@HasResponses = 0 AND [Responses] = 0), but it doesn't seem to work.

    Is this the most optimal way to do it? Is there a better way? I'm working with SQL Server 2005.

    Code:
    SELECT
    	f.FeedbackText AS [Feedback Comment],
    	u.Name AS [Feedback Author],
    	f.CreatedDate AS [Created On],
    	(
    		SELECT COUNT(*)
    		FROM FeedbackResponse fr
    		WHERE fr.FeedbackID = f.ID
    	) AS [Responses]
    FROM Feedback f
    	INNER JOIN [User] u ON f.StaffID = u.StaffID
    WHERE f.CreatedDate >= @DateFrom
    	AND f.CreatedDate <= @DateTo
    	AND
    	(
    		@HasResponses IS NULL
    		OR
    		(
    			@HasResponses = 1 AND
    			(
    				SELECT COUNT(*)
    				FROM FeedbackResponse fr
    				WHERE fr.FeedbackID = f.ID
    			) > 0
    		)
    		OR
    		(
    			@HasResponses = 0 AND
    			(
    				SELECT COUNT(*)
    				FROM FeedbackResponse fr
    				WHERE fr.FeedbackID = f.ID
    			) = 0
    		)
    	)
    Sunday Ironfoot

    SQL Server 2000 n00b

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    looks like a candidate for a HAVING clause. I rewrote a data extract where some one wrote their own HAVING clause once it went from 45 minutes to 5 seconds.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    With 2005, try defining your subquery as a CTE (Common Table Expression).
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •