select
ArticleHumanReadableId as av
,ArticleAlias as alias
,round(sum(QuantityLoadingUnits),0) total_pallets
,round(sum(Quantity),0) as total_palletQTY
,round(sum(case when State = 0 then QuantityLoadingUnits  else 0 end),0) avalible_Pallets
,round(sum(case when State = 0 then Quantity  else 0 end),0) available_QTY
,sum(case when b.HumanReadableId = 864 then QuantityLoadingUnits else 0 end) as coa_Pallets
,sum(case when b.HumanReadableId = 864 then Quantity else 0 end) as coa_QTY
,sum(case when b.HumanReadableId <> 864 then QuantityLoadingUnits else 0 end) as held_Pallets
,sum(case when b.HumanReadableId <> 864 then Quantity else 0 end) as held_QTY
--,l.RunningNumber
/** should be in line  **/
--,l.MachineLocation,l.MachineName,l.ProductionLotRunningNumber
/** should be in line  **/
--,l.WarehouseDescription,l.LaneDescription
,articleTypeName

FROM [test1_AlplaPROD2.0_Read].[warehousing].[WarehouseUnit] as l
left join
(
SELECT [Id]
      ,[HumanReadableId]
      ,d.[Description]
      ,[DefectGroupId]
      ,[IsActive]
  FROM [test1_AlplaPROD2.0_Read].[blocking].[BlockingDefect] as g
  
  left join 
  [AlplaPROD_test1].dbo.[T_BlockingDefects]  as d (nolock) on
  d.IdGlobalBlockingDefect = g.HumanReadableId
) as b on
b.id = l.MainDefectId
where LaneHumanReadableId not in (20000,21000)
group by ArticleHumanReadableId,
ArticleAlias,
ArticleTypeName
--,l.RunningNumber
/** should be in line  **/
--,l.MachineLocation,l.MachineName,l.ProductionLotRunningNumber
/** should be in line  **/
--,l.WarehouseDescription,l.LaneDescription

order by ArticleHumanReadableId