Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Query to find items purchased from multiple vendors Expand / Collapse
Author
Message
Posted Thursday, March 7, 2013 4:02 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, December 31, 2013 2:00 AM
Points: 21, Visits: 150
Hi All,

I want to find item that purchased from more than one supplier, so i am running following query :-

SELECT A.Item
FROM Purch_Inv_Line A, Purch_Inv_Header B
WHERE A."Document No_"=B.Item and B."Posting Date" BETWEEN '2010-01-01' AND '2010-12-31'
GROUP BY A.Item
having count(distinct Vendor_Code)>1

but it is displaying only items if i run it with vendor code then it is displaying nothing in result.

Pls help me tell me how i can get both fields i will very thankful to you.
Post #1427887
Posted Thursday, March 7, 2013 4:06 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:23 AM
Points: 5,216, Visits: 5,107
Can you please provide DDL and sample data as per the second link in my signature so that we can help you out better.



Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1427889
Posted Thursday, March 7, 2013 5:57 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:44 AM
Points: 6,719, Visits: 13,828
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1427925
Posted Thursday, March 7, 2013 7:18 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 12, 2014 8:53 AM
Points: 6,917, Visits: 6,978
Which table contains Vendor_Code ?


Far away is close at hand in the images of elsewhere.

Anon.

Post #1427965
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse