Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6342299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:12:55+00:00 2026-05-24T20:12:55+00:00

This query works, but seems terribly inefficient. There has to be a better way?

  • 0

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'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:12:56+00:00Added an answer on May 24, 2026 at 8:12 pm

    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.

    Select CompanyName
      , case TerritoryNumber 
        when 1 then coalesce(SCA_MRK,0)
        when 2 then coalesce(NCA_MRK,0)
        when 3 then coalesce(SNV_MRK,0)
        when 4 then coalesce(NAZ_MRK,0)
      end AS MRK
      , ...etc
    from Company 
    cross join (select top 1 * from MarketRates order by EndingDate desc) MarketRates
    where CompanyID = 'THISID'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query that works as desired, but it seems like there should
This query works fine, but there seems to be an error in the definitions
I have a problem with a query. This query works with phpMyAdmin, but I
When using the PHP Sphinx client this works: SphinxClient::query ('*', 'index1'); But when I
I'm logging output with log = /var/log/mysql/query.log and this works ok, but I only
This query works fine for me: $query = SELECT p.topnode_id, p.param_key, p.param_value FROM tbl_params
This query works: item = db.GqlQuery(SELECT * FROM Item WHERE CSIN = 13)[0] although
I've got this query that works ok, select * from Materia where Cursar_Cursada=0 and
So I have this query that works perfectly: SELECT users.*, GROUP_CONCAT(categories.category_name) AS categories FROM
i have this sql query (below) and it works great but i need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.