Need an efficient SQL query

I have a table of parts and a table of orders. I need to construct a query that pulls a subset of parts needed for an assembly and the last order placed for each of those parts. These are huge tables so this needs to be an efficient query. I am working in MS SQL 2012

CREATE TABLE [dbo].[Parts_Master](
      [PartNo] [bigint] NOT NULL,
      [Nomenclature] [varchar](255) NOT NULL,
      [bldg] [int] NULL,
      [room] [int] NULL,
      [unit] [int] NULL,
      [row] [int] NULL,
      [shelf] [int] NULL,
      [slot] [int] NULL,
      [IsSubAssy] [bit] NOT NULL,
      [IsAssy] [bit] NOT NULL,
      [Overflow] [int] NOT NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[Parts_Orders](
      [UID] [bigint] IDENTITY(1,1) NOT NULL,
      [VendorID] [int] NOT NULL,
      [VendorOrderID] [varchar](50) NOT NULL,
      [OrderDate] [datetime] NOT NULL,
      [DetailLine] [int] NOT NULL,
      [PartNo] [bigint] NOT NULL,
      [Quantity] [int] NOT NULL
) ON [PRIMARY]
LVL 1
lippertDeveloperAsked:
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Tell us how you would search for 'an assembly', as the only Assembly-looking columns I see are bit values (i.e. Yes or No) and not for a specific assembly.

If that's all it is, then give this a whirl..
SELECT pm.partNo, Max(OrderDate) as last_order_placed
FROM Parts_Master pm
   JOIN Parts_Orders o ON pm.PartNo = o.PartNo
WHERE IsAssy = 1 
GROUP BY pm.partNo
ORDER BY pm.partNo

Open in new window

0
lippertDeveloperAuthor Commented:
I don't need the last order for assys or sub assys only for parts. Let me give this a try and see if it works.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>These are huge tables so this needs to be an efficient query.
Also, add a foreign key between these tables for more efficient JOINs.
This may mean you need to 'scrub' data that is in one table but not the other.

ALTER TABLE Parts_Orders
ADD CONSTRAINT FK_Parts_Master_Parts FOREIGN KEY (PartNo) 
   REFERENCES dbo.Parts_Master(PartNo) 

Open in new window

0

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
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
This also means you'll want to define PartNo as the primary key on Parts_Master

ALTER TABLE Parts_Orders
ADD CONSTRAINT PK_Parts_Orders PRIMARY KEY CLUSTERED (PartNo)
GO

Open in new window

0
lippertDeveloperAuthor Commented:
Foreign key already exists.
0
lippertDeveloperAuthor Commented:
This worked.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade, but I'm a little confused as to what worked for you, as you accepted a comment relating to a foreign key, but later stated that 'Foreign key already exists'.
0
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
Microsoft SQL Server

From novice to tech pro — start learning today.