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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:14:33+00:00 2026-05-24T07:14:33+00:00

I have two SELECT statements that are UNIONed, as in the pseudo code below

  • 0

I have two SELECT statements that are UNIONed, as in the pseudo code below

SELECT A.InvoiceNumber, A.itemCode, A.itemDescription, A.Cost
FROM someTable A
UNION
SELECT InvoiceNumberFromSomeTable, B.itemCode, B.itemDescription, ***IwantTheSumOfAllCostsInTheFirstQueryForThisInvoiceNumber*** as Cost
FROM yetAnotherTable B

The expected result is

InvoiceNumber    itemCode         itemDescription                           Cost
I001             A000001          This is Item01 From SomeTable             15
I001             A000002          This is Item02 From SomeTable             16
I001             Total            This is Total From YetAnotherTable        31
I002             A000001          This is Item01 From SomeTable             25
I002             B000002          This is Item99 From SomeTable             26
I002             Total            This is Total From YetAnotherTable        51

In my case, the second query will always return one row and the Cost value should be the sum of all the Cost Values in first query.

How can one accomplish this? I’ve tried every approach I’m familiar with, but couldn’t make it work. Please help and Thank you SOF community.

EDIT: I forgot to add the invoice number column. The totalling should happen for each invoice. Apologies for not adding this earlier.

  • 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-24T07:14:33+00:00Added an answer on May 24, 2026 at 7:14 am

    What you can do is to SUM as an extra column

    SELECT
        A.itemCode, A.itemDescription, A.Cost,
        SUM(A.Cost) OVER () AS SumCostA
    FROM someTable A
    UNION
    SELECT B.itemCode, B.itemDescription,
        Whatever1, Whatever2 --NULLs?
    FROM yetAnotherTable B
    

    It doesn’t that much sense to have yetAnotherTable itemCode and itemDescription related to data from another table. What about Cost and Sum(Cost) from yetAnotherTable?

    Is yetAnotherTable parent or category table? If so, a JOIN (no UNION) would make more sense

    Edit, after update

    SELECT
        A.InvoiceNumber, A.itemCode, A.itemDescription, A.Cost,
        SUM(A.Cost) OVER (PARTITION BY A.InvoiceNumber) AS SumCostAPerInvoice
    FROM someTable A
    UNION
    SELECT InvoiceNumberFromSomeTable, B.itemCode, B.itemDescription,
        Whatever1, Whatever2 --NULLs?
    FROM yetAnotherTable B
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two select statements that I am trying to port from Oracle to
I currently have two fairly long select statements, identical apart from the fact that
I have tried the following two statements: SELECT col FROM db.tbl WHERE col (LIKE
I have two SELECT statements that return a number each, and I'd like to
I have two separate SELECT statements: SELECT VCe.VId FROM `VCe` WHERE `YId` = 9007
Let's say.. I have two statements select min(Log_In_Time) from tbl where (event_ID=4) select max(Log_Off_Time)
I have the following two T-SQL statements that I really need to combine: SELECT
I have two select statements that find the maximum salary for a specific role.
Lets say I have two subqueries: SELECT Id AS Id0 FROM Table0 => Id0
I have the following two columns: SELECT b.ip_address AS IP ,b.mask AS MASK FROM

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.