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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:00:35+00:00 2026-06-04T08:00:35+00:00

I have 2 seperate select statements, using aggregate functions in each. I would like

  • 0

I have 2 seperate select statements, using aggregate functions in each. I would like to be able to take the results and combine them.

table_a
id int
entered_date datetime (holds utc stamp)
balance money
group_id int

table_b
id int
entered_date date
balance money
transaction_type int

query 1: 
select convert(date,entered_date), sum(balance) as earned
from table_a
where group_id in (1, 2, 3, 4)
group by convert(date,entered_Date)

query 2: 
select convert(date,entered_date), sum(balance) as spent
where transaction_type = 2
group by convert(date,entered_Date)

results:
query 1:
2012-05-13, 5000
2012-05-14, 12000
...

query 2:
2012-05-13, 9000
2012-05-14, 55856
...

I would like to return one row for each record without using temp tables. The result set should have a date, then earned vs. spent. I have a report running using union to get the totals and that is fine, but i need to generate a result set with 1 record and a earned vs against line. Any help with this is appreciated.

  • 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-06-04T08:00:37+00:00Added an answer on June 4, 2026 at 8:00 am

    Try:

    ;With AllDates AS
    (
    select convert(date,entered_date) As EnteredDate
        from table_a
        where group_id in (1, 2, 3, 4)
        group by convert(date,entered_Date)
    UNION
    select convert(date,entered_date)
        from table_b
        where transaction_type = 2
        group by convert(date,entered_Date)
    )
    , AllEarned AS (
    select convert(date,entered_date) AS EnteredDate, sum(balance) as Earned
        from table_a
        where group_id in (1, 2, 3, 4)
        group by convert(date,entered_Date)
    )
    , AllSpent AS (
    select convert(date,entered_date) AS EnteredDate, sum(balance) as Spent
        from table_b
        where transaction_type = 2
        group by convert(date,entered_Date)
    )
    SELECT
        d.EnteredDate, e.Earned, s.Spent
        FROM AllDates d
        LEFT OUTER JOIN AllEarned e ON d.EnteredDate=e.EnteredDate
        LEFT OUTER JOIN AllSpent s ON d.EnteredDate=s.EnteredDate
        ORDER BY 1,2,3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UINavigationController and I have seperate UIViews that I switch between using
I have 2 functions on a seperate page that I am including of the
I have two separate SELECT statements: SELECT VCe.VId FROM `VCe` WHERE `YId` = 9007
I have two separate SELECT statements which are both GROUP-BY'd separately e.g.: SELECT x,
I have multiple select statements from different tables on the same database. I was
I have a report that I would like to base on a single SQL
I was wondering if it's possible to have 2 'while' statements using the same
Is it possible to join the results of 2 sql SELECT statements in one
Say I have a stored procedure consisting of several separate SELECT, INSERT, UPDATE and
Current I have a seperate maven module for my database access, all my DAO

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.