2

我需要使用 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 来完成此操作

4

3 回答 3

0

尝试:

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有一个错误。解决方法之一是将其转换为存储过程并从 Excel 调用存储过程。从阅读来看,参数的确定和编号方式可能存在问题。

于 2013-05-30T22:46:53.533 回答
0

也许只是 ODBC 尝试重写您的查询失败。我不知道为什么该ON版本不起作用,但您只需在WHERE. Nulls 不等于任何东西,因此='s 正在删除带有空值的行。

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 ISNULL(PH.Deprecated, 'n') <> 'Y' 
   AND (PH.[Month] IS NULL OR
        (PH.[Month] = ?   --Month
     AND PH.[Year] = ? )  --Year
        )
ORDER  BY [Pipeline name], 
          PR.[Name] 
于 2013-05-30T22:57:26.463 回答
0

根据您的第一个查询,您尝试将月份过滤器放在左连接中。因为那是一个左连接你的获取记录月份和年份过滤器不是强制性的。

但是在下一个查询中,您将过滤器置于 where 条件中,因此查询不会为您返回任何结果。

如果您的月份和年份过滤器是强制性的,请将其放在 where 并且第二个查询给出正确数量的结果。

否则请使用此查询

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 (select [Pipelinerateid],[Value] 
                       from [GAS].[dbo].[Pipelineratehistory] 
                       where [Month] = ?   --Month
                     AND [Year] = ? --Year
                      ) PH 
               ON PH.[Pipelinerateid] = PR.[Id] 
       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] 
于 2014-03-25T14:21:25.307 回答