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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:36:39+00:00 2026-06-12T23:36:39+00:00

I have two tables, fbpost and fbalbum, which each have a DATETIME column called

  • 0

I have two tables, fbpost and fbalbum, which each have a DATETIME column called createdTime. I’m finding the number of albums per month, and the number of posts per month, and adding them together.

Here’s my query(which works as described):

SELECT  createdTime, itemCount FROM 
        (SELECT DATE_FORMAT(createdTime, '%m-%Y') AS createdTime, COUNT(*) AS itemCount FROM fbpost WHERE page_id =2 
        GROUP BY YEAR(createdTime), MONTH(createdTime)
        UNION ALL
        SELECT DATE_FORMAT(createdTime, '%m-%Y') AS createdTime, COUNT(*) AS itemCount FROM fbalbum WHERE page_id=2
        GROUP BY YEAR(createdTime), MONTH(createdTime)) AS foo
GROUP BY createdTime

This gives the results:

01-2009 | 173
01-2010 | 21
01-2011 | 521
01-2012 | 776
02-2009 | 117
02-2010 | 158
02-2011 | 678
...

But I would like the results to be ordered like this:

01-2009 | 173
02-2009 | 56
03-2009 | 543
04-2009 | 211
05-2009 | 723
06-2009 | 55
07-2009 | 521
...

How can I achieve this?

Note: DATE_FORMAT() gives a string, not a DATETIME, so you can’t sort by date. But, if I take out the DATE_FORMAT() in the two nested select statements, I get 2 rows for most months, since that would leave the day. Though there would be only one row per month for each nested select, they day would usually differ, since the last item in a month may be on any day.

  • 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-12T23:36:40+00:00Added an answer on June 12, 2026 at 11:36 pm

    Don’t use DATE_FORMAT until your outer query:

    SELECT DATE_FORMAT(a.createdTime, '%m-%Y') AS createdTime, SUM(a.itemCount) AS itemCount
    FROM
        (SELECT DATE(createdTime) AS createdTime, COUNT(*) AS itemCount 
        FROM fbpost WHERE page_id = 2
        GROUP BY DATE(createdTime)
        UNION ALL
        SELECT DATE(createdTime) AS createdTime, COUNT(*) AS itemCount
        FROM fbalbum WHERE page_id = 2
        GROUP BY DATE(createdTime)) a
    GROUP BY YEAR(a.createdTime), MONTH(a.createdTime)
    

    See it in action *

    *Demo does not have page_id

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have two tables say ABC and XYZ and contain one column which data will
I have two tables, orders and old_orders. Each table has some similar fields and
I have two tables, items and itemmeta. items has item_id which links it to
I have two tables which look like the following tools : id | part
I have two tables in MYSQL database which contains same Columns by counting and
I have two tables in my database, called ratings and movies . Ratings: |
I have two tables that I want to use for viewing my reports which
I have two tables called calendar_colour and user, calendar_colour ( colour_id int primary key,
I have two tables in a SQL Server 2008 database in which I want
I have two tables both having same column and but different no of rows.

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.