I need to create a single record for unique item in the tblInventory_Hist table, selected to have the most recent DateRecvd.
I can get a single item as in:
SELECT d.ItemId
,MAX(h.DateRecvd)
FROM tblInventory_Hist h
JOIN tblPurchaseOrderItems d ON h.PODetailID = d.ID
JOIN tblItem i ON d.ItemID = i.ID
GROUP BY d.ItemID
ORDER BY d.ItemID
but I cannot figure out how to get back to tblInventory to join to tblPurchaseOrder... and tblItem. Then I can include columns like tblInventory_Hist.InvoiceNum, tblVendor.Name, tblItem.Number and tblItem.Description.
Can anyone help this sometime SQL guy?
CREATE TABLE [dbo].[tblItem](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Number] nvarchar NULL,
[Description] nvarchar NULL,
...
CREATE TABLE [dbo].[tblInventory_Hist](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[PODetailID] [int] NULL,
[QTYRecvd] [int] NULL,
[DateRecvd] [datetime] NULL,
[InvoiceNum] [nvarchar](50) NULL,
[InvoiceDate] [datetime] NULL,
...
CREATE TABLE [dbo].[tblPurchaseOrders](
[ID] [int] IDENTITY(1,1) NOT NULL,
[VendorID] [int] NULL,
[OrderDate] [datetime] NULL,
...
CREATE TABLE [dbo].[tblPurchaseOrderItems](
[ID] [int] IDENTITY(1,1) NOT NULL,
[OrderID] [int] NULL,
[ItemID] [int] NULL,
[QTY] [int] NULL,
[Price] [money] NULL,
...
CREATE TABLE [dbo].[tblVendor](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL
...