This query works, but seems terribly inefficient. There has to be a better way?
What I am trying to do is select 4 different columns from a MarketRates table based on which territory a company is in. There are only 4 Territories, stored as an integer 1-4 in the Company table. So for instance, if the Territory is “1”, then I want to select the 4 Southern California columns (column names are SCA*), but if the Territory is “2”, then I want to select the 4 Norhtern California columns (column names are NCA*), etc.
I know the tables should be constructed differently, but this is what I have to deal with.
The MarketRates table contains these columns (SCA = Southern California, NCA = Northern California, SNV = Southern Nevada, NAZ = Northern Arizona:
- EndingDate – date
- SCA_MRK – decimal (8,2)
- SCA_RATE – decimal (8,2)
- SCA_COMP – decimal (8,2)
- SCA_NEG – decimal (8,2)
- NCA_MRK – decimal (8,2)
- NCA_RATE – decimal (8,2)
- NCA_COMP – decimal (8,2)
- NCA_NEG – decimal (8,2)
- SNV_MRK – decimal (8,2)
- SNV_RATE – decimal (8,2)
- SNV_COMP – decimal (8,2)
- SNV_NEG – decimal (8,2)
- NAZ_MRK – decimal (8,2)
- NAZ_RATE – decimal (8,2)
- NAZ_COMP – decimal (8,2)
- NAZ_NEG – decimal (8,2)
This is the current query that I am using:
Select CompanyName
, case TerritoryNumber
when 1 then (Select top 1 coalesce(SCA_MRK,0) From MarketRates Order by EndingDate desc)
when 2 then (Select top 1 coalesce(NCA_MRK,0) From MarketRates Order by EndingDate desc)
when 3 then (Select top 1 coalesce(SNV_MRK,0) From MarketRates Order by EndingDate desc)
when 4 then (Select top 1 coalesce(NAZ_MRK,0) From MarketRates Order by EndingDate desc)
end AS MRK
, case TerritoryNumber
when 1 then (Select top 1 coalesce(SCA_RATE,0) From MarketRates Order by EndingDate desc)
when 2 then (Select top 1 coalesce(NCA_RATE,0) From MarketRates Order by EndingDate desc)
when 3 then (Select top 1 coalesce(SNV_RATE,0) From MarketRates Order by EndingDate desc)
when 4 then (Select top 1 coalesce(NAZ_RATE,0) From MarketRates Order by EndingDate desc)
end AS RATE
, case TerritoryNumber
when 1 then (Select top 1 coalesce(SCA_COMP,0) From MarketRates Order by EndingDate desc)
when 2 then (Select top 1 coalesce(NCA_COMP,0) From MarketRates Order by EndingDate desc)
when 3 then (Select top 1 coalesce(SNV_COMP,0) From MarketRates Order by EndingDate desc)
when 4 then (Select top 1 coalesce(NAZ_COMP,0) From MarketRates Order by EndingDate desc)
end AS COMP
, case TerritoryNumber
when 1 then (Select top 1 coalesce(SCA_NEG,0) From MarketRates Order by EndingDate desc)
when 2 then (Select top 1 coalesce(NCA_NEG,0) From MarketRates Order by EndingDate desc)
when 3 then (Select top 1 coalesce(SNV_NEG,0) From MarketRates Order by EndingDate desc)
when 4 then (Select top 1 coalesce(NAZ_NEG,0) From MarketRates Order by EndingDate desc)
end AS NEG
from Company
where CompanyID = 'THISID'
You should only have to select your 1 row from MarketRates once since you’re referencing the same row each time. Select it in a sub-query and join to it and you can reference that throughout the query. I re-wrote what MRK would look like, similar syntax/logic for the other columns as well.