我正在尝试将此 SQL 语句转换为 Excel 中的 VBA,但我似乎无法获得正确的语法。
这是查询:
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 #]
这是我试图放入 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 #]"
您会注意到我还压缩了一些行,因为我收到了有关过多行延续的警告。我在这个转换中哪里出错了?
最佳答案
您的 SQL 没有问题 - VBA 编辑器根本不允许有超过 25 行继续。简单的解决方法:将作业分成几部分。
sSQL = "with cte as" & _
"(" & _
" SELECT [Job #]" & _
(...)
" FROM [CompanyR].[dbo].[WIPVarianceRptView]" & _
")"
sSQL = sSQL & _
"SELECT [Job #]" & _
" ,[Date], [Variance Amt], [Job QTY], [OpenQty], [Part #], [Material], [PCS #], [Mtrl$$], [Date Last Issue]" & _
(...)
" FROM cte" & _
" Order By [Job #]"
关于sql - 如何将此 SQL 输入到 Excel VBA 脚本中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71874170/