select DISTINCT a.ItemCode, a.ItemName, a.OnHand, b.ItmsGrpNam , a.InvntryUom, a.BuyUnitMsr
, a.createDate as [Fecha de producción]
--------2019--------
—MAX----
, ( Select Max(Currency) From OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) And Createdate = (select MAX(createDate) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) AND createDate between ‘20190101’ and ‘20191231’) )as [MAX Moneda 2019]
, ( Select Max(CardCode) From OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) And Createdate = (select MAX(createDate) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) AND createDate between ‘20190101’ and ‘20191231’) )as [MAX SN 2019]
, (select MAX(createDate) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) AND createDate between ‘20190101’ and ‘20191231’) as [MAX Fecha 2019]
, (select MAX(Price) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) and createDate = ( select MAX(createDate) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) AND createDate between ‘20190101’ and ‘20191231’) ) as [MAX Price 2019]
----MIN----
, ( Select Min(CardCode) From OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) And Createdate = (select MAX(createDate) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) AND createDate between ‘20190101’ and ‘20191231’) )as [MIN SN 2019]
, (select MIN(createDate) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) AND createDate between ‘20190101’ and ‘20191231’ ) as [MIN Fecha 2019]
, (select MIN(Price) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) and createDate = ( select MAX(createDate) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) AND createDate between ‘20190101’ and ‘20191231’) ) as [MIN Price 2019]
-----2018----
, ( Select Max(Currency) From OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) And Createdate = (select MAX(createDate) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) AND createDate between ‘20180101’ and ‘20181231’) )as [MAX Moneda 2018]
, ( Select Max(CardCode) From OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) And Createdate = (select MAX(createDate) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) AND createDate between ‘20180101’ and ‘20181231’) )as [MAX SN 2018]
, (select MAX(createDate) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) AND createDate between ‘20180101’ and ‘20181231’) as [MAX fecha 2018]
, (select MAX(Price) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) and createDate = ( select MAX(createDate) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) AND createDate between ‘20180101’ and ‘20181231’) ) as [MAX Price 2018]
----MIN----
, ( Select Min(CardCode) From OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) And Createdate = (select MAX(createDate) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) AND createDate between ‘20180101’ and ‘20181231’) )as [MAX SN 2018]
, (select MIN(createDate) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) AND createDate between ‘20180101’ and ‘20181231’ ) as [MIN Fecha 2018]
, (select MIN(Price) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) and createDate = ( select MAX(createDate) from OINM where ItemCode = a.ItemCode and TransType in (20,69,10000071) AND createDate between ‘20180101’ and ‘20181231’) ) as [MIN Price 2018]
, CASE
WHEN a.validFor = ‘Y’ and a.frozenFor = ‘N’ THEN ‘Activo’
WHEN a.validFor = ‘N’ and a.frozenFor = ‘Y’ THEN ‘Inactivo’
EnD as Activo
from OITM a
inner join OITB b on a.ItmsGrpCod = b.ItmsGrpCod
inner join OITW c on a.ItemCode = c.ItemCode