SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select Query Returning One Record Per PK
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

StacyOW
Yak Posting Veteran

USA
87 Posts

Posted - 11/30/2012 :  10:34:49  Show Profile  Reply with Quote
I have this query (below) that I am trying to have only return one record for each ComponentID. But since there are sometimes several part#'s that for the same ComponentID that match my search value it is returning the ComponentID several times. ComponentID is my Primary Key for the Sparts table. Is there a way to have it only return the componentID once? I hope that makes sense.

Thanks for your help!
Stacy
SELECT        sparts.ComponentID, sparts.Component, sparts.Specification, sparts.GraphicFilename, sparts.Priority, sparts.QtyonHand, sparts.PointofReorder, 
                         sparts.PrintonWO, sparts.OrderedAmnt, sparts.Choosen, sparts.Bin
FROM            sparts INNER JOIN
                         spartven ON sparts.ComponentID = spartven.ComponentID
WHERE        (spartven.PartNum LIKE '%' + @SearchValue + '%')

Edited by - StacyOW on 11/30/2012 10:35:56

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 11/30/2012 :  10:39:59  Show Profile  Reply with Quote
SELECT *
from
(
SELECT        sparts.ComponentID, sparts.Component, sparts.Specification, sparts.GraphicFilename, sparts.Priority, sparts.QtyonHand, sparts.PointofReorder, 
                         sparts.PrintonWO, sparts.OrderedAmnt, sparts.Choosen, sparts.Bin,ROW_NUMBER() OVER (PARTITION BY sparts.ComponentID Order by sparts.ComponentID) as Seq
FROM            sparts INNER JOIN
                         spartven ON sparts.ComponentID = spartven.ComponentID
WHERE        (spartven.PartNum LIKE '%' + @SearchValue + '%')
)P
WHERE P.Seq = 1
Go to Top of Page

StacyOW
Yak Posting Veteran

USA
87 Posts

Posted - 11/30/2012 :  11:16:57  Show Profile  Reply with Quote
Awesome!
Thank you very much. It works perfectly.
Stacy
Go to Top of Page

Bustaz Kool
Flowing Fount of Yak Knowledge

USA
1642 Posts

Posted - 11/30/2012 :  11:33:23  Show Profile  Reply with Quote
SELECT DISTINCT      sparts.ComponentID, sparts.Component, sparts.Specification, sparts.GraphicFilename, sparts.Priority, sparts.QtyonHand, sparts.PointofReorder, 
                         sparts.PrintonWO, sparts.OrderedAmnt, sparts.Choosen, sparts.Bin
FROM            sparts INNER JOIN
                         spartven ON sparts.ComponentID = spartven.ComponentID
WHERE        (spartven.PartNum LIKE '%' + @SearchValue + '%')



=================================================
Hear the sledges with the bells - silver bells!
What a world of merriment their melody foretells!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000