I'm trying to convert this SQL statement to VBA in excel, but i can't seem to get the syntax right.
This is the Query:
with cte as (
select
[Job #]
,[Date]
,[Variance Amt]
,[Job QTY]
,[OpenQty]
,[Part #]
,[Material]
,[PCS #]
,[Matrl$$]
,[Date Last Issue]
,case when substring([PurchaseOrders],len([PurchaseOrders]),1) = '|' then substring([PurchaseOrders],1,len([PurchaseOrders])-1) else [PurchaseOrders] end [PurchaseOrders]
,[PO$$]
,[Date Last Rcvd]
,[Wip Total]
,[per pc]
,[Standard Cost]
,[DIFF]
,[% of Profit]
,ROW_NUMBER() OVER(PARTITION BY [Job #] ORDER BY [Job #]) AS rn
,count(*) over(partition by [Job #]) as maxrn
,sum([Matrl$$]) over(partition by [Job #]) as [Job Matrl$$]
,sum([PO$$]) over(partition by [Job #]) as [Job PO$$]
FROM [CompanyR].[dbo].[WIPVarianceRptView]
)
SELECT [Job #]
,[Date]
,[Variance Amt]
,[Job QTY]
,[OpenQty]
,[Part #]
,[Material]
,[PCS #]
,[Matrl$$]
,[Date Last Issue]
,case when substring([PurchaseOrders],len([PurchaseOrders]),1) = '|' then substring([PurchaseOrders],1,len([PurchaseOrders])-1) else [PurchaseOrders] end [PurchaseOrders]
,case when rn <> maxrn then 0 else [PO$$] end as [PO$$]
,[Date Last Rcvd]
,case when rn <> maxrn then 0 when rn = maxrn then ([Job PO$$] + [Job Matrl$$]) else 0 end as [Wip Total]
,case when rn <> maxrn then 0 else [per pc] end as [per pc]
,case when rn <> maxrn then 0 else [Standard Cost] end as [Standard Cost]
,case when rn <> maxrn then 0 else [DIFF] end as [DIFF]
,case when rn <> maxrn then 0 else [% of Profit] end as [% of Profit]
,maxrn as [MAX of ROW NO by JOB]
FROM cte
Order By [Job #]
And here is what I'm trying to put into Excel VBA
sSQL = "with cte as" & _
"(" & _
" SELECT [Job #]" & _
" [Date], [Variance Amt], [Job QTY], [OpenQty], [Part #], [Material], [PCS #], [Mtrl$$], [Date Last Issue]" & _
" ,case when substring([PurchaseOrders],len([PurchaseOrders]),1) = '|' then substring([PurchaseOrders],1,len([PurchaseOrders])-1) else [PurchaseOrders] end [PurchaseOrders]" & _
" ,[PO$$], [Date Last Rcvd], [Wip Total], [per pc], [Standard Cost], [DIFF], [% of Profit]" & _
" ,ROW_NUMBER() OVER(PARTITION BY [Job #] ORDER BY [Job #]) AS rn" & _
" ,count(*) over(partition by [Job #]) as maxrn" & _
" ,sum([Matrl$$]) over(partition by [Job #]) as [Job Matrl$$]" & _
" ,sum([PO$$]) over(partition by [Job #]) as [Job PO$$]" & _
" FROM [CompanyR].[dbo].[WIPVarianceRptView]" & _
")" & _
" SELECT [Job #]" & _
" ,[Date], [Variance Amt], [Job QTY], [OpenQty], [Part #], [Material], [PCS #], [Mtrl$$], [Date Last Issue]" & _
" ,case when substring([PurchaseOrders],len([PurchaseOrders]),1) = '|' then substring([PurchaseOrders],1,len([PurchaseOrders])-1) else [PurchaseOrders] end [PurchaseOrders]" & _
" ,case when rn <> maxrn then 0 else [PO$$] end as [PO$$]" & _
" ,[Date Last Rcvd]" & _
" ,case when rn <> maxrn then 0 when rn = maxrn then ([Job PO$$] + [Job Matrl$$]) else 0 end as [Wip Total]" & _
" ,case when rn <> maxrn then 0 else [per pc] end as [per pc]" & _
" ,case when rn <> maxrn then 0 else [Standard Cost] end as [Standard Cost]" & _
" ,case when rn <> maxrn then 0 else [DIFF] end as [DIFF]" & _
" ,case when rn <> maxrn then 0 else [% of Profit] end as [% of Profit]" & _
" FROM cte" & _
" Order By [Job #]"
You'll notice I also condensed some lines because i got a warning about too many line continuations. Where am I going wrong in this conversion?