Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help with 3 tables in SQL & ACCESS 1

Status
Not open for further replies.

memkam

Technical User
Aug 3, 2004
40
US
what i am trying to do is read entries from TransactionReport, if the IsItRes field is true, compare the entries in the TransactionReport table to the Res table otherwise compare to ComTable

Query doesn't work.. please help

SELECT CASE
WHEN IsIsRes = TRUE
THEN
SELECT [TransactionReport].[ServiceType], [TransactionReport].[Weight], [TransactionReport].[Zone], [TransactionReport].[Price], [ResPrice].[Contractprice], [ResPrice].[HistoricPrice], [ResPrice].[ServiceType], [ResPrice].[Weight], [ResPrice].[Zone]
FROM TransactionReport, ResPrice
WHERE ((([ResPrice].[ServiceType]=[TransactionReport].[ServiceType]) And ([ResPrice].[Weight]=[TransactionReport].[Weight]) And ([ResPrice].[Zone]=[TransactionReport].[Zone]))
ELSE
SELECT [TransactionReport].[ServiceType], [TransactionReport].[Weight], [TransactionReport].[Zone], [TransactionReport].[Price], [ComPrice].[Contractprice], [ComPrice].[HistoricPrice], [ComPrice].[ServiceType], [ComPrice].[Weight], [ComPrice].[Zone]
FROM TransactionReport, ComPrice
WHERE (([ComPrice].[ServiceType]=[TransactionReport].[ServiceType]) And ([ComPrice].[Weight]=[TransactionReport].[Weight]) And ([ComPrice].[Zone])=[TransactionReport].[Zone]))
END
 
I think this will do the trick. The idea is to OUTER JOIN to both Residential and Commericial tables, then use CASE to decide which table's columns to SELECT. HTH, Good luck!

Code:
SELECT [TransactionReport].[ServiceType],
  [TransactionReport].[Weight],
  [TransactionReport].[Zone],
  [TransactionReport].[Price],
  [TransactionReport].[IsIsRes],
  CASE IsItRes WHEN TRUE THEN [ResPrice].[Contractprice]
               ELSE [ComPrice].[Contractprice]
  END AS [ContractPrice],
  CASE IsItRes WHEN TRUE THEN [ResPrice].[HistoricPrice]
               ELSE [ComPrice].[HistoricPrice]
  END AS [HistoricPrice],
  CASE IsItRes WHEN TRUE THEN [ResPrice].[ServiceType]
               ELSE [ComPrice].[ServiceType]
  END AS [ServiceType],
  CASE IsItRes WHEN TRUE THEN [ResPrice].[Weight]
               ELSE [ComPrice].[Weight]
  END AS [Weight],
  CASE IsItRes WHEN TRUE THEN [ResPrice].[Zone]
               ELSE [ComPrice].[Zone]
  END AS [Zone],
FROM ResPrice
        RIGHT OUTER JOIN TransactionReport
          ON [ResPrice].[ServiceType]=[TransactionReport].[ServiceType]
          AND [ResPrice].[Weight]=[TransactionReport].[Weight]
          AND [ResPrice].[Zone]=[TransactionReport].[Zone]
            LEFT OUTER JOIN ComPrice
               ON [ComPrice].[ServiceType]=[TransactionReport].[ServiceType]
               AND [ComPrice].[Weight]=[TransactionReport].[Weight]
               AND [ComPrice].[Zone])=[TransactionReport].[Zone]

--John [rainbow]
-----------------------------------
Behold! As a wild ass in the desert
go forth I to do my work.
--Gurnie Hallock (Dune)
 
John,

Thanks for the code (especially for the time you took to write it)..

I am getting an error though:

Syntax error (missing operator) in query 'CASE IsItRes WHEN TRUE THEN [ResPrice].[ContractPrice]
ELSE
[ResPrice].[ContractPrice]
END
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top