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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:43:17+00:00 2026-05-14T00:43:17+00:00

I’m not sure if this is possible in one mysql query so I might

  • 0

I’m not sure if this is possible in one mysql query so I might just combine the results via php.

I have 2 tables: ‘users’ and ‘billing’

I’m trying to group summed activity for every date that is available in these two tables. ‘users’ is not historical data but ‘billing’ contains a record for each transaction.

In this example I am showing a user’s status which I’d like to sum for created date and deposit amounts that I would also like to sum by created date. I realize there is a bit of a disconnect between the data but I’d like to some all of it together and display it as seen below. This will show me an overview of all of the users by when they were created and what the current statuses are next to total transactions.

I’ve tried UNION as well as LEFT JOIN but I can’t seem to get either to work.

Union example is pretty close but doesn’t combine the dates into one row.

(
SELECT
created,
SUM(status) as totalActive,
NULL as totalDeposit
FROM users
GROUP BY created
)
UNION
(
SELECT
created,
NULL as totalActive,
SUM(transactionAmount) as totalDeposit
FROM billing
GROUP BY created
)

I’ve also tried using a date lookup table and joining on the dates but the SUM values are being added multiple times.

note: I don’t care about the userIds at all but have it in here for the example.

users table
(where status of ‘1’ denotes “active”)
(one record for each user)

created     |   userId  |   status

2010-03-01  | 10            |   0
2010-03-01  | 11            |   1   
2010-03-01  | 12            |   1   
2010-03-10  | 13            |   0
2010-03-12  | 14            |   1
2010-03-12  | 15            |   1
2010-03-13  | 16            |   0   
2010-03-15  | 17            |   1

billing table
(record created for every instance of a billing “transaction”

created     |   userId  |   transactionAmount   

2010-03-01  | 10            |   50
2010-03-01  | 18            |   50
2010-03-01  | 19            |   100
2010-03-10  | 89            |   55
2010-03-15  | 16            |   50
2010-03-15  | 12            |   90
2010-03-22  | 99            |   150

desired result:

created     |   sumStatusActive     |   sumStatusInactive       |   sumTransactions

2010-03-01  | 2                                 |   1                                       |   200
2010-03-10  | 0                                 |   1                                       |   55
2010-03-12  | 2                                 |   0                                       |   0
2010-03-13  | 0                                 |   0                                       |   0
2010-03-15  | 1                                 |   0                                       |   140
2010-03-22  | 0                                 |   0                                       |   150

Table dump:

CREATE TABLE IF NOT EXISTS `users` (
  `created` date NOT NULL,
  `userId` int(11) NOT NULL,
  `status` smallint(6) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


INSERT INTO `users` (`created`, `userId`, `status`) VALUES
('2010-03-01', 10, 0),
('2010-03-01', 11, 1),
('2010-03-01', 12, 1),
('2010-03-10', 13, 0),
('2010-03-12', 14, 1),
('2010-03-12', 15, 1),
('2010-03-13', 16, 0),
('2010-03-15', 17, 1);


CREATE TABLE IF NOT EXISTS `billing` (
  `created` date NOT NULL,
  `userId` int(11) NOT NULL,
  `transactionAmount` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


INSERT INTO `billing` (`created`, `userId`, `transactionAmount`) VALUES
('2010-03-01', 10, 50),
('2010-03-01', 18, 50),
('2010-03-01', 19, 100),
('2010-03-10', 89, 55),
('2010-03-15', 16, 50),
('2010-03-15', 12, 90),
('2010-03-22', 99, 150);
  • 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-14T00:43:17+00:00Added an answer on May 14, 2026 at 12:43 am

    Try this:

    Select created, sum(status) as totalActive, sum(transactionAmount) as totalDeposit
    From
    ( (
      SELECT
      created,
      status,
      0 as transactionAmount
      FROM users
    )
    UNION
    (
      SELECT
      created,
      0 as status,
      transactionAmount
      FROM billing
    ) ) as x group by created
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text

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.