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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:01:12+00:00 2026-05-23T23:01:12+00:00

Currently I have UNION’s between 3 different select statements giving me three rows. What

  • 0

Currently I have UNION’s between 3 different select statements giving me three rows. What I’m needing is to modify this to have the union inside the from clause so that I can generate more columns (if I understand the functionality).

Basically what is going on is that the existing framework is designed and built to have a single row of data returned to it and will require heavy modification to handle a multi-row result set (everything is getting parsed to xml before being passed to the front-end).

My biggest issue (I believe) is being able to differentiate between the three sub selects inside the primary.

Guarantees in the select

1) Each select inside the from will only produce a single row result set.

2) All result sets from inside of the from will have same column count and column names (inherent of union I believe)

For example…
I currently have

SELECT * FROM A
    UNION
SELECT * FROM B
    UNION
SELECT * FROM C

Doing it this way produces a three row result set.

What I’m wanting if possible is….

SELECT cost as CurrentSelectedCost, /* from first select */
       cost as PreviousCost, /* from second select */
       cost as NextCost /* from third select */
FROM (
    SELECT * FROM A
        UNION
    SELECT * FROM B
        UNION
    SELECT * FROM C
)

Now I’m guessing that I will need to alias the different select statements that are within the from clause, but I’m having issues getting that to function. The examples that I’ve found on here didn’t seem to address the need to have all select statements inside of from differentiated. If this has been answered on here a link will suffice no reason to re-invent the wheel (I may just not know the terminology to search for) Also the database is a DB2 instance running on an iSeries

  • 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-23T23:01:13+00:00Added an answer on May 23, 2026 at 11:01 pm

    You need to unify the columns retrieved from the three SELECT statements.

    SELECT CurrentSelectedCost,
           PreviousCost, 
           NextCost 
    FROM (
        SELECT cost as CurrentSelectedCost, 0 as PreviousCost, 0 as NextCost FROM A
            UNION
        SELECT 0 as CurrentSelectedCost, cost as PreviousCost, 0 as NextCost  FROM B
            UNION
        SELECT 0 as CurrentSelectedCost, 0 as PreviousCost, cost as NextCost  FROM C
    ) as COSTS
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we
Framework: Rails 2.3.8 Database; PostgreSQL I currently have the following SQL statements (generated by
If have a union that currently foresees some simple data types: union Value {
I currently have 2 queries that are returning lists of MyModel like this: var
Currently I have two different queries that return exactly the same results however, changing
I currently have this code use for printing a table in mysql database ,
I currently have an MS Access application that connects to a PostgreSQL database via
I currently have speakers set up both in my office and in my living
I currently have an existing database and I am using the LINQtoSQL generator tool
We currently have a company email server with Exchange, and a bulk email processing

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.