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?
 
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
 
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.