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 3280406
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:42:02+00:00 2026-05-17T19:42:02+00:00

HI, now I have a simple question. This is a simple scenary: I have

  • 0

HI, now I have a simple question. This is a simple scenary:

I have a Table “Table1” with three fields (a1, a2, a3). The result of query is a simple row but I need to create this kind of query:

SELECT 'a1' as [Field], 
        a1 as [Value]
  FROM Table1
UNION
SELECT 'a2' as [Field], 
       a2 as [Value]
  FROM Table1
UNION
SELECT 'a3' as [Field], 
       a3 as [Value]
 FROM Table1

The expected result is a table with to fields (“Field” and “Value”)

The Problem

…that I have is if I am joining three tables, I need at least 14 fields from this JOIN and I have the Where clause, so I have to repeat each FROM and WHERE clause on each UNION.
At the end it is the mother of all queries.
Is it possible to create a more simple query for this task?

I need the same result from my initial example for this more complex scenery.

thanks in advance.

  • 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-17T19:42:03+00:00Added an answer on May 17, 2026 at 7:42 pm

    Is it possible to create a more simple query for this task?

    Yes, but you should first look at making a change to your database schema. If that is not possible then you should stop reading now and explain to your boss (or whoever is obstructing you) that not being able to modify the schema could lead to an unmaintainable mess.

    Assuming that you’re still reading you should note that your database schema contains redundancy – you have similar columns that are repeating within the same table. This could be a sign the your database is not correctly normalized. These columns should most likely be moved out into a new table and instead of having separate columns ‘a1’, ‘a2’, ‘a3’ you can have the columns ‘Value’ and ‘Field’ where the former contains one of the values ‘a1’, ‘a2’, ‘a3’ and the latter contains the actual value . Now you can get all your data by doing a much simpler query without any repetition:

    SELECT Field, Value
    FROM ...
    WHERE ...
    

    If you need different where conditions for each type of field you can do that too:

    WHERE (...common conditions...)
    AND (
       (Field = 'a1' AND ...) OR
       (Field = 'a2' AND ...) OR
       ...
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple question; right now I have something like this: typedef void(*MyFunctionPointer)(int); typedef std::vector <
I have a simple web page that till now didn't need any login. It
Following on from this question I now have code that can attach to a
I have a very simple question about transactions. (In sql server 2000, but I
This should be a simple question. I have a simple if/else statement: <?php //
This might be a simple question, but i can't seem to grasp it. I
I may have a simple question, I need to know how to get to
I have one simple question. Now I'm working on a e-commerce script and of
Hey, this is probably a simple question, but I am having trouble running my
I am sure this is a simple question, but I am having no luck

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.