![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi, Sorry if I'm in the wrong group for this sql language question, but perhaps someone here can answer -- or direct me to a more appropriate newsgroup? I have a sql query that attempts to select a "DealerClientType" based on a Case statement involving values from other tables. Running the query clocks for minutes and when I cancel I see it must be looping as it returns the same row over 100,000 times. Copied below is the query. Perhaps some combination of a left join and case might work? My intent is to return all rows from the first table (Transactions), and to include a DealerClientType based on different criteria for different Dealers. SELECT a.[TransSQLID] ,a.[DealerCode] ,a.[BranchCode] ,a.[AccountNbr] ,a.[Units], (Select Case When (Select c.DealerCode from [MIP].[dbo].[DealerAccountNbr] c Where a.DealerCode = c.DealerCode and a.AccountNbr = c.AccountNbr) = 'ABC' Then c.AccountClientType When (Select d.DealerCode from [MIP].[dbo].[DealerBranch] d Where a.DealerCode = d.DealerCode and a.BranchCode = d.BranchCode) = 'XYZ' Then d.BranchClientType Else b.[DealerClientType] end) as TransClientType FROM [MIP].[dbo].[Transactions] a , [MIP].[dbo].[Dealers] b , [MIP].[dbo].[DealerAccountNbr] c , [MIP].[dbo].[DealerBranch] d Where a.DealerCode = b.DealerCode Any advice is much appreciated! Thank you! Thomas |
#3
| |||
| |||
|
|
Hi , I will probably write this way-- SELECT a.[TransSQLID] ,a.[DealerCode] ,a.[BranchCode] ,a.[AccountNbr] ,a.[Units], Case When c.DealerCode = 'ABC' Then c.AccountClientType When d.DealerCode = 'XYZ' Then d.BranchClientType Else b.DealerClientType end as TransClientType FROM [MIP].[dbo].[Transactions] a , [MIP].[dbo].[Dealers] b , [MIP].[dbo].[DealerAccountNbr] c , [MIP].[dbo].[DealerBranch] d where a.DealerCode = b.DealerCode and a.AccountNbr = c.AccountNbr and a.DealerCode = c.DealerCode and a.DealerCode = d.DealerCode and a.BranchCode = d.BranchCode |
![]() |
| Thread Tools | |
| Display Modes | |
| |