我需要使用 MS Query 在 Excel 2007 中显示一组固定管道的月费率,即使管道没有月费率,也必须以这种方式显示
我在 SQL Server 2008 R2 中使用以下代码完成了它。
SELECT P.Name AS [Pipeline Name],
PR.Id,
PR.[Name],
PH.[Value] AS Rate
FROM [GAS].[dbo].[Pipelinerate] PR
INNER JOIN Pipeline P
ON P.Id = PR.Pipelineid
LEFT OUTER JOIN [GAS].[dbo].[Pipelineratehistory] PH
ON PH.[Pipelinerateid] = PR.[Id]
AND ( PH.[Month] = ? --Month
AND PH.[Year] = ? ) --Year
WHERE ( PR.[Id] IN ( 197, 198, 1, 2, 3, 5, 67, 68, 23 ) )
AND ISNULL(PH.Deprecated, 'n') <> 'Y'
ORDER BY [Pipeline name],
PR.[Name]
当我尝试在 Excel 2007 的 MS Query 中执行此操作时,我得到以下错误
[Microsoft][ODBC SQL Server Driver] Invalid Parameter Number
[Microsoft][ODBC SQL Server Driver] Invalid Descriptor Index
我通过跟踪和错误发现如果我从ON
子句中删除带有参数的条件并将其放入WHERE Clause
如下
SELECT P.Name AS [Pipeline Name],
PR.Id,
PR.[Name],
PH.[Value] AS Rate
FROM [GAS].[dbo].[Pipelinerate] PR
INNER JOIN Pipeline P
ON P.Id = PR.Pipelineid
LEFT OUTER JOIN [GAS].[dbo].[Pipelineratehistory] PH
ON PH.[Pipelinerateid] = PR.[Id]
WHERE ( PR.[Id] IN ( 197, 198, 1, 2, 3, 5, 67, 68, 23 ) )
AND ( PH.[Month] = ? --Month
AND PH.[Year] = ? ) --Year
AND ISNULL(PH.Deprecated, 'n') <> 'Y'
ORDER BY [Pipeline name],
PR.[Name]
该代码适用于 MS Query 并给出以下结果
在此输出中,未显示当月没有费率的管道。因此,此代码不起作用。因此,我试图LEFT JOIN
在这种情况下找到替代方法,以使用 MS Query 在 excel 中获得所需的输出。
关联
Pipeline 和 PipelineRate - 可选的一对多关系
PipelineRate 和 PipelineRateHistory - 可选的一对多关系
任何人都可以建议左加入的替代方法或实现此目的的方法吗?
PS:我不能使用存储过程。我知道如何使用 VBA 来做到这一点。我需要使用 MS Query 来完成此操作