0

I have a query that uses a subquery and I am having a problem returning the expected results. The error I receive is..."Only one expression can be specified in the select list when the subquery is not introduced with EXISTS." How can I rewrite this to work?

SELECT
    a.Part,
    b.Location,
    b.LeadTime
FROM
    dbo.Parts a
    LEFT OUTER JOIN dbo.Vendor b ON b.Part = a.Part
WHERE
    b.Location IN ('A','B','C')
AND
    Date IN (SELECT Location, MAX(Date) FROM dbo.Vendor GROUP BY Location)
GROUP BY
    a.Part,
    b.Location,
    b.LeadTime
ORDER BY
    a.Part
5
  • 2
    just curious to know, you are using group by but no aggregate function in select! why?
    – sarwar026
    Jun 3, 2013 at 15:37
  • what are you trying to achieve?
    – John Woo
    Jun 3, 2013 at 15:38
  • 1
    removing of Location will work, example, Date IN (SELECT MAX(Date) FROM dbo.Vendor GROUP BY Location)
    – John Woo
    Jun 3, 2013 at 15:41
  • Each Location has more than one Date where there may have been maintenance done on the Part, so I need the last time the Part was updated by Location.
    – tsqln00b
    Jun 3, 2013 at 15:43
  • What version of SQL Server?
    – ErikE
    Jun 3, 2013 at 17:56

3 Answers 3

3

I think something like this may be what you're looking for. You didn't say what version of SQL Server--this works in SQL 2005 and up:

SELECT
    p.Part,
    p.Location, -- from *p*, otherwise if no match we'll get a NULL
    v.LeadTime
FROM
    dbo.Parts p
    OUTER APPLY (
       SELECT TOP (1) * -- * here is okay because we specify columns outside
       FROM dbo.Vendor v
       WHERE p.Location = v.Location -- the correlation part
       ORDER BY v.Date DESC
    ) v
WHERE
    p.Location IN ('A','B','C')
ORDER BY
    p.Part
;

Now, your query can be repaired as is by adding the "correlation" part to change your query into a correlated subquery as demonstrated in Kory's answer (you'd also remove the GROUP BY clause). However, that method still requires an additional and unnecessary join, hurting performance, plus it can only pull one column at a time. This method allows you to pull all the columns from the other table, and has no extra join.

Note: this gives logically the same results as Lamak's answer, however I prefer it for a few reasons:

  1. When there is an index on the correlation columns (Location, here) this can be satisfied with seeks, but the Row_Number solution has to scan (I believe).
  2. I prefer the way this expresses the intent of the query more directly and succinctly. In the Row_Number method, one must get out to the outer condition to see that we are only grabbing the rn = 1 values, then bop back into the CTE to see what that is.
  3. Using CROSS APPLY or OUTER APPLY, all the other tables not involved in the single-inner-row-per-outer-row selection are outside where (to me) they belong. We aren't squishing concerns together. Using Row_Number feels a bit like throwing a DISTINCT on a query to fix duplication rather than dealing with the underlying issue. I guess this is basically the same issue as the previous point worded in a different way.
  4. The moment you have TWO tables from which you wish to pull the most recent value, the Row_Number() solution blows up completely. With this syntax, you just easily add another APPLY clause, and it's crystal clear what you're doing. There is a way to use Row_Number for the multiple tables scenario by moving the other tables outside, but I still don't prefer that syntax.
  5. Using this syntax allows you to perform additional joins based on whether the selected row exists or not (in the case that no matching row was found). In the Row_Number solution, you can only reasonably do that NOT NULL checking in the outer query--so you are forced to split up the query into multiple, separated parts (you don't want to be joining to values you will be discarding!).

P.S. I strongly encourage you to use aliases that hint at the table they represent. Please don't use a and b. I used p for Parts and v for Vendor--this helps you and others make sense of the query more quickly in the future.

1

If I understood you corrrectly, you want the rows with the max date for locations A, B and C. Now, assuming SQL Server 2005+, you can do this:

;WITH CTE AS
(
    SELECT
        a.Part,
        b.Location,
        b.LeadTime,
        RN = ROW_NUMBER() OVER(PARTITION BY a.Part ORDER BY [Date] DESC)
    FROM
        dbo.Parts a
        LEFT OUTER JOIN dbo.Vendor b ON b.Part = a.Part
    WHERE
        b.Location IN ('A','B','C')
)
SELECT  Part, 
        Location,
        LeadTime
FROM CTE
WHERE RN = 1
ORDER BY Part
2
  • 1
    @ErikE Don't worry about the random downvotes....they happen. I upvoted your answer though, very thorough
    – Lamak
    Jun 3, 2013 at 18:10
  • 1
    Thanks! Your answer is perfectly good--I already gave you +1. :)
    – ErikE
    Jun 3, 2013 at 18:15
0

In your subquery you need to correlate the Location and Part to the outer query. Example:

Date = (SELECT MAX(Date) 
        FROM dbo.Vender v 
        WHERE v.Location = b.Location 
          AND v.Part = b.Part
       )

So this will bring back one date for each location and part

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.