SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query to find items purchased from multiple vendors


Query to find items purchased from multiple vendors

Author
Message
neellotus07
neellotus07
Say Hey Kid
Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)Say Hey Kid (665 reputation)

Group: General Forum Members
Points: 665 Visits: 220
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.
anthony.green
anthony.green
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62558 Visits: 8594
Can you please provide DDL and sample data as per the second link in my signature so that we can help you out better.



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
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)

Group: General Forum Members
Points: 96405 Visits: 20667
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
David Burrows
David Burrows
SSC-Dedicated
SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)

Group: General Forum Members
Points: 31696 Visits: 10698
Which table contains Vendor_Code ?


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

Anon.


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search