use [test1_AlplaPROD2.0_Read]

DECLARE @StartDate DATE = '[startDate]' -- 2025-1-1
DECLARE @EndDate DATE = '[endDate]' -- 2025-1-31
SELECT 
r.[ArticleHumanReadableId]
      ,[ReleaseNumber]
	  ,h.CustomerOrderNumber
	  ,x.CustomerLineItemNumber
      ,[CustomerReleaseNumber]
      ,[ReleaseState]
      ,[DeliveryState]
	  ,ea.JournalNummer as BOL_Number
      ,[ReleaseConfirmationState]
      ,[PlanningState]
      ,format(r.[OrderDate], 'yyyy-MM-dd HH:mm') as OrderDate
      --,r.[OrderDate]
      ,FORMAT(r.[DeliveryDate], 'yyyy-MM-dd HH:mm') as DeliveryDate
      --,r.[DeliveryDate]
      ,FORMAT(r.[LoadingDate], 'yyyy-MM-dd HH:mm') as LoadingDate
      --,r.[LoadingDate]
      ,[Quantity]
      ,[DeliveredQuantity]
      ,r.[AdditionalInformation1]
      ,r.[AdditionalInformation2]
      ,[TradeUnits]
      ,[LoadingUnits]
      ,[Trucks]
      ,[LoadingToleranceType]
      ,[SalesPrice]
      ,[Currency]
      ,[QuantityUnit]
      ,[SalesPriceRemark]
      ,r.[Remark]
      ,[Irradiated]
      ,r.[CreatedByEdi]
      ,[DeliveryAddressHumanReadableId]
	  ,DeliveryAddressDescription
      ,[CustomerArtNo]
      ,[TotalPrice]
      ,r.[ArticleAlias]

  FROM [order].[Release] (nolock) as r

 left join
 [order].LineItem as x on 

 r.LineItemId = x.id

 left join
 [order].Header as h on
 x.HeaderId = h.id

 --bol stuff
 left join 
 AlplaPROD_test1.dbo.V_LadePlanungenLadeAuftragAbruf (nolock) as zz 
 on zz.AbrufIdAuftragsAbruf = r.ReleaseNumber

 left join
(select * from (SELECT 
ROW_NUMBER() OVER (PARTITION BY IdJournal ORDER BY add_date DESC) AS RowNum
,*
  FROM [AlplaPROD_test1].[dbo].[T_Lieferungen] (nolock)) x

  where  RowNum = 1) as ea on 
 zz.IdLieferschein = ea.IdJournal

where 
--r.ReleaseNumber = 1452

r.DeliveryDate between @StartDate AND @EndDate
and DeliveredQuantity > 0
--and r.ArticleHumanReadableId in ([articles]) 
--and Journalnummer = 169386