use [test1_AlplaPROD2.0_Read]

select
ArticleHumanReadableId as article
,ArticleAlias as alias
,round(sum(QuantityLoadingUnits),2) total_pallets
,round(sum(Quantity),2) as total_palletQTY
,round(sum(case when State = 0 then QuantityLoadingUnits  else 0 end),2) available_Pallets
,round(sum(case when State = 0 then Quantity  else 0 end),2) available_QTY
,round(sum(case when b.HumanReadableId = 864 then QuantityLoadingUnits else 0 end),2) as coa_Pallets
,round(sum(case when b.HumanReadableId = 864 then Quantity else 0 end),2) as coa_QTY
,round(sum(case when b.HumanReadableId <> 864 then QuantityLoadingUnits else 0 end),2) as held_Pallets
,round(sum(case when b.HumanReadableId <> 864 then Quantity else 0 end),2) as held_QTY
,round(sum(case when w.type = 7 then QuantityLoadingUnits else 0 end),2) as consignment_Pallets
,round(sum(case when w.type = 7 then Quantity else 0 end),2) as consignment_qty
--,l.RunningNumber

/** datamart include lot number  **/
--,l.MachineLocation,l.MachineName,l.ProductionLotRunningNumber as lot

/** data mart include location data  **/
--,l.WarehouseDescription,l.LaneDescription

,articleTypeName

FROM [warehousing].[WarehouseUnit] as l (nolock)
left join
(
SELECT [Id]
      ,[HumanReadableId]
      ,d.[Description]
      ,[DefectGroupId]
      ,[IsActive]
  FROM [blocking].[BlockingDefect] as g (nolock)
  
  left join 
  [AlplaPROD_test1].dbo.[T_BlockingDefects]  as d (nolock) on
  d.IdGlobalBlockingDefect = g.HumanReadableId
) as b on
b.id = l.MainDefectId

left join
[warehousing].[warehouse] as w (nolock) on
w.id = l.warehouseid

where LaneHumanReadableId not in (20000,21000)
group by ArticleHumanReadableId,
ArticleAlias,
ArticleTypeName
--,l.RunningNumber

/** datamart include lot number  **/
--,l.MachineLocation,l.MachineName,l.ProductionLotRunningNumber

/** data mart include location data  **/
--,l.WarehouseDescription,l.LaneDescription

order by ArticleHumanReadableId