I need to display the monthly rates for a fixed set of pipelines in Excel 2007 using MS Query and even if a pipeline has no monthly rate it has to be displayed in this manner
required output http://i42.tinypic.com/29j615.jpg
I have done it using the following code in 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]
When I try to do it in MS Query in Excel 2007 then I get I get the following errors
[Microsoft][ODBC SQL Server Driver] Invalid Parameter Number
[Microsoft][ODBC SQL Server Driver] Invalid Descriptor Index
I found out by trail and error that If I remove the conditions with the parameters from the ON
clause and put it in WHERE Clause
as below
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]
The code works in MS Query and give the following result
actual output http://i42.tinypic.com/5qiog.jpg
In this output the pipelines with no rates for the month are not shown. Hence this code doesn't work. Thus I am trying to find alternatives to LEFT JOIN
in this case to get the desired output in excel using MS Query.
Associations
Pipeline and PipelineRate - optional one to many relationship
PipelineRate and PipelineRateHistory - optional one to many relationship
Can anyone suggest alternatives to left join or a way to accomplish this?
PS: I can't use stored procedures. I know how to do this using VBA. I require to accomplish this using MS Query