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

  • Home
  • SEARCH
  • 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 125189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:07:10+00:00 2026-05-11T05:07:10+00:00

I have a script to extract certain data from a much bigger table, with

  • 0

I have a script to extract certain data from a much bigger table, with one field in particular changing regularly, e.g.

SELECT CASE @Flag WHEN 1 THEN t.field1 WHEN 2 THEN t.field2 WHEN 3         THEN t.field3 END as field,        ...[A bunch of other fields] FROM table t 

However, the issue is now I want to do other processing on the data. I’m trying to figure out the most effective method. I need to have some way of getting the flag through, so I know I’m talking about data sliced by the right field.

One possible solution I was playing around with a bit (mostly to see what would happen) is to dump the contents of the script into a table function which has the flag passed to it, and then use a SELECT query on the results of the function. I’ve managed to get it to work, but it’s significantly slower than…

The obvious solution, and probably the most efficient use of processor cycles: to create a series of cache tables, one for each of the three flag values. However, the problem then is to find some way of extracting the data from the right cache table to perform the calculation. The obvious, though incorrect, response would be something like

SELECT CASE @Flag WHEN 1 THEN table1.field WHEN 2 THEN table2.field WHEN 3         THEN table3.field END as field,        ...[The various calculated fields] FROM table1, table2, table3 

Unfortunately, as is obvious, this creates a massive cross join – which is not my intended result at all.

Does anyone know how to turn that cross join into an ‘Only look at x table’? (Without use of Dynamic SQL, which makes things hard to deal with?) Or an alternative solution, that’s still reasonably speedy?

EDIT: Whether it’s a good reason or not, the idea I was trying to implement was to not have three largely identical queries, that differ only by table – which would then have to be edited identically whenever a change is made to the logic. Which is why I’ve avoided the ‘Have the flag entirely separate’ thing thus far…

  • 1 1 Answer
  • 2 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. 2026-05-11T05:07:11+00:00Added an answer on May 11, 2026 at 5:07 am

    A simpler solution, and one suggested by a workmate:

    SELECT CASE @Flag WHEN 1 THEN t.field1 WHEN 2 THEN t.field2 WHEN 3     THEN t.field3 END as field,    [A bunch of other fields],    @Flag as flag FROM table t 

    Then base the decision making on the last field. A lot simpler, and probably should have occurred to me in the first place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 209k
  • Answers 209k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Instead of using a DirectoryCatalog, use an AggregateCatalog. You will… May 12, 2026 at 9:41 pm
  • Editorial Team
    Editorial Team added an answer There are minor enhancements (bug fixes) I'd definitely do. In… May 12, 2026 at 9:41 pm
  • Editorial Team
    Editorial Team added an answer The TextDocument.Selection property, of type TextSelection, has a TextPane property… May 12, 2026 at 9:41 pm

Related Questions

I am writing a script at the moment that will grab certain information from
I have a script that needs to extract data temporarily to do extra operations
I have a text file containing lines of data. I can use the following
I have some text files containing lines as follow : 07JAN01 , -0.247297942769082E+07, -0.467133797284279E+07,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.