Home Forums SQL Server 2005 T-SQL (SS2K5) Query to find items purchased from multiple vendors RE: Query to find items purchased from multiple vendors

  • SELECT d.Item

    FROM (

    SELECT

    a.Item,

    Vendor_Code

    FROM Purch_Inv_Line a

    INNER JOIN Purch_Inv_Header a

    ON a.[Document No_] = b.Item

    AND b.[Posting Date] BETWEEN '2010-01-01' AND '2010-12-31'

    GROUP BY a.Item, Vendor_Code

    ) d

    GROUP BY d.Item

    HAVING COUNT(*) > 1

    SELECT

    a.Item,

    Vendor_Code,

    vc = DENSE_RANK() OVER(PARTITION BY Item ORDER BY Vendor_Code)

    FROM Purch_Inv_Line a

    INNER JOIN Purch_Inv_Header a

    ON a.[Document No_] = b.Item

    AND b.[Posting Date] BETWEEN '2010-01-01' AND '2010-12-31'

    Edit: replaced double-quote identifier delimiters with square brackets for readability.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden