USE [P21]
GO

/****** Object:  View [dbo].[nutech_item_last_invoice_view]    Script Date: 9/29/2024 9:41:22 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





CREATE   VIEW [dbo].[nutech_item_last_invoice_view]
AS
select last_invoice.last_inv_ship_to_id, last_invoice.last_inv_mast_uid,
       p21_view_invoice_line.item_id last_inv_item_id, p21_view_invoice_line.unit_price last_inv_unit_price,
       p21_view_invoice_line.date_created last_inv_date_created, p21_view_invoice_line.invoice_no last_invoice_no
from p21_view_invoice_line
       inner join (select p21_view_invoice_hdr.ship_to_id last_inv_ship_to_id,
                          p21_view_invoice_line.inv_mast_uid last_inv_mast_uid,
                          max(p21_view_invoice_line.invoice_no) last_invoice_no
                   from p21_view_invoice_hdr
                          inner join p21_view_invoice_line on
	                         p21_view_invoice_hdr.invoice_no = p21_view_invoice_line.invoice_no AND
                             p21_view_invoice_hdr.approved = 'Y'
                   where p21_view_invoice_line.qty_shipped > 0
                   group by p21_view_invoice_hdr.ship_to_id, p21_view_invoice_line.inv_mast_uid) last_invoice on
	     p21_view_invoice_line.invoice_no = last_invoice.last_invoice_no AND
	     p21_view_invoice_line.inv_mast_uid = last_invoice.last_inv_mast_uid

GO


