Need help with a query

Hi Experts,

Need your help with  a query. This is my query. and the following is the output.

Select B.[BomMstID] ,B.[BomID],B.[OrderDate],B.[CreatedDate],B.[CustomerID],B.[RevisionLvl],
B.[JobNum],B.[JobDate],B.[PartID],B.[QtyRequired] as Quantity,B.[WearSparePartFlag],B.[PriceTotal],
P.ManufacturerPartNo as ManuPartNo,
P.PartDescription as Description,P.LastOrderedCost,P.LastOrderedCost*B.QtyRequired  as Cost,
P.ManuID,M.ManuName as Manufacturer,BPQ.[PONum]

          
FROM BOM As B
      LEFT OUTER JOIN  Parts As P
            on B.PartID=P.PartID
      LEFT OUTER JOIN  MANUFACTURER AS M
            on P.ManuID=M.manuID
      LEFT OUTER JOIN  BOMPORFQ  AS bpq
            on B.BomMstID=BPQ.BomMstID
            
Where B.CustomerID=73
      AND B.JobNum = '15041701'
        AND B.BomID = 20
      AND B.RevisionLvl=1
      order by ManuPartNo


Output
------------

ManuPartNo                   Description      Manufacturer   Last
                                                Ordered QtyRequired   Cost                              PONum
                                                               Cost
-------------------------     ------------     -------------  ------  --------     -------        --------
1.  HUB SHC1023 1/2 .375-.5      CORD GRIP             AIS        $6          1              $6          P15093001    
2.  HUB SHC1023 1/2 .375-.5      CORD GRIP             AIS        $6          1              $6              
3.  HUB SHC1023 1/2 .375-.5      CORD GRIP             AIS        $6          1              $6          P15092903    
4.  HUB SHC1023 1/2 .375-.5      CORD GRIP             AIS        $6          1              $6              
5.  Q#15-017-4312                RA Conveyor Extension AIS      $26618  1              $1         P15092903      
6.  Q#15-017-4312 rev2           SAOP50 Installation   AIS      $12507   1              
If the ManuPartNo is same then I need the ones with PONum and all other ManuPartNo  too. In this example I need record# 1,3,5 and 6.

Thanks in advance.
RadhaKrishnaKiJayaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RadhaKrishnaKiJayaAuthor Commented:
Sorry. When I posted the question the data displayed improperly so I have uploaded a text file with the query and the data. Sorry for the inconvenience.
Query1.txt
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I understand you want to have all rows with a unique ManuPartNo (only having one single row), plus those non-unique but only with PONum set?
Brian CroweDatabase AdministratorCommented:
I"m not understanding your request.  "If the ManuPartNo is the same" as what?  If this is the output of the query shouldn't it match the query and doesn't it already contain the rows your are asking for.  Please clarify.
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

RadhaKrishnaKiJayaAuthor Commented:
Hi Qlemo,

You are right.

Thanks.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
select *
from (
Select B.[BomMstID] ,B.[BomID],B.[OrderDate],B.[CreatedDate],B.[CustomerID],B.[RevisionLvl],
B.[JobNum],B.[JobDate],B.[PartID],B.[QtyRequired] as Quantity,B.[WearSparePartFlag],B.[PriceTotal],
P.ManufacturerPartNo as ManuPartNo,
P.PartDescription as Description,P.LastOrderedCost,P.LastOrderedCost*B.QtyRequired  as Cost,
P.ManuID,M.ManuName as Manufacturer,BPQ.[PONum],
count(*) over (partition by P.ManufacturerPartNo) as ManuPartNoCnt
FROM BOM As B 
      LEFT OUTER JOIN  Parts As P
            on B.PartID=P.PartID 
      LEFT OUTER JOIN  MANUFACTURER AS M 
            on P.ManuID=M.manuID
      LEFT OUTER JOIN  BOMPORFQ  AS bpq
            on B.BomMstID=BPQ.BomMstID 
            
Where B.CustomerID=73 
      AND B.JobNum = '15041701'
        AND B.BomID = 20 
      AND B.RevisionLvl=1
      order by ManuPartNo
) data
where ManuPartNoCnt = 1 or PONum is not null

Open in new window

This is just to show you how to do it. You'll have to change the * to something more suitable for your output, but it doesn't change anything about the logic.
RadhaKrishnaKiJayaAuthor Commented:
Hi Brian,

This is the output of the current query. But I want different output (Which I have already mentioned). I do not know how to modify it. So need help.

Thank u.
RadhaKrishnaKiJayaAuthor Commented:
Thank you for the query. What do I need to put instead of *. Because I need all the columns.

Thank u.
RadhaKrishnaKiJayaAuthor Commented:
I am getting error "The column 'JobDate' was specified multiple times for 'Data'."

What should I do.

Thanks.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
If you leave the *, you'll get all columns plus the count of rows for that particular ManuPartNo. If you need something else, just enumerate the columns with their result or alias names, like
Select BomMstID , BomID, 

Open in new window

and so on. The result columns need to be unique; the select you provided does not have two columns with name JobDate, but if that is the case change the name of one of them with as OtherJobDate or similar.
RadhaKrishnaKiJayaAuthor Commented:
Hi,

I changed the query. But still getting all the records. Any other idea?

Thank u.


select [BomMstID],[BomID],[JobNum],PartID,ManuPartNo,PONum from (
Select B.[BomMstID] ,B.[BomID],B.[OrderDate],B.[CreatedDate],B.[CustomerID],B.[RevisionLvl],
B.[JobNum],B.[JobDate],B.[PartID],B.[QtyRequired] as Quantity,B.[WearSparePartFlag],B.[PriceTotal],
P.ManufacturerPartNo as ManuPartNo,
P.PartDescription as Description,P.LastOrderedCost,P.LastOrderedCost*B.QtyRequired  as Cost,
P.ManuID,M.ManuName as Manufacturer,BPQ.[PONum],
count(*) over (partition by P.ManufacturerPartNo) as ManuPartNoCnt
FROM BOM As B
      LEFT OUTER JOIN  Parts As P
            on B.PartID=P.PartID
      LEFT OUTER JOIN  MANUFACTURER AS M
            on P.ManuID=M.manuID
      LEFT OUTER JOIN  BOMPORFQ  AS bpq
            on B.BomMstID=BPQ.BomMstID
           
Where B.CustomerID=73
      AND B.JobNum = '15041701'
        AND B.BomID = 20
      AND B.RevisionLvl=1
      order by ManuPartNo
) data
where ManuPartNoCnt = 1 or PONum is not null
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Probably the PONum is not null is not correct. Is the column populated with an empty string instead of being NULL? Then this should work:
where ManuPartNoCnt = 1 or (PONum is not null and PONum != '')

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RadhaKrishnaKiJayaAuthor Commented:
Thank you very much for your wonderful help!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.