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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:06:17+00:00 2026-06-05T23:06:17+00:00

Here is my query (I replaced table names with generic ones). I am trying

  • 0

Here is my query (I replaced table names with generic ones). I am trying to do a union all on two different queries in order to group them all by date so that results with similar dates come out as one row.

I am getting the “Every derived table must have its own alias” error when attempting to execute. What am I typing in wrong?

I have researched this but couldn’t find the answer. Every selected field has an alias? Or is the issue in the first SELECT?

SELECT SUM(val), id, dat, title FROM (

                      SELECT table1.product_id as id, SUM(table1.qty) as val, DATE_FORMAT(table1.created, '%Y-%m-1') as dat, table2.title as title 
                      FROM table1
                      LEFT JOIN table3 ON table1.event_id = table3.id
                      LEFT JOIN table2 ON table1.product_id = table2.id
                      WHERE table1.user_id = $user_id AND table3.active != 2 AND table3.temp = 0 AND table2.active != 2

                      GROUP BY dat

                      UNION ALL

                      SELECT table4.product_id as id, SUM(table4.qty) as val, DATE_FORMAT(table4.created, '%Y-%m-1') as dat, table2.title as title 
                      FROM table4
                      LEFT JOIN table5 ON table4.festival_id = table5.id
                      LEFT JOIN table2 ON table4.product_id = table2.id
                      WHERE table4.user_id = $user_id AND table5.active != 2 AND table2.active != 2

                      GROUP BY dat

                      )
                      GROUP BY id
                      ORDER BY dat ASC

Here is what I am attempting to do:

My original result:

Array
(
[0] => stdClass Object
    (
        [id] => 1
        [val] => 1
        [dat] => 2012-05-1
        [title] => Test Product
    )

[1] => stdClass Object
    (
        [id] => 1
        [val] => 8
        [dat] => 2012-06-1
        [title] => Test Product
    )

[2] => stdClass Object
    (
        [id] => 2
        [val] => 4
        [dat] => 2012-06-1
        [title] => Test Product 2
    )

[3] => stdClass Object
    (
        [id] => 3
        [val] => 6
        [dat] => 2012-06-1
        [title] => Test Product 3
    )

[4] => stdClass Object
    (
        [id] => 1
        [val] => 10
        [dat] => 2012-05-1
        [title] => Test Product
    )

[5] => stdClass Object
    (
        [id] => 1
        [val] => 8
        [dat] => 2012-06-1
        [title] => Test Product
    )

[6] => stdClass Object
    (
        [id] => 2
        [val] => 3
        [dat] => 2012-06-1
        [title] => Test Product 2
    )

[7] => stdClass Object
    (
        [id] => 3
        [val] => 3
        [dat] => 2012-06-1
        [title] => Test Product 3
    )

)

So if they have a similar date and ID, I need those to be just one result. Like so:

    Array
(
[0] => stdClass Object
    (
        [id] => 1
        [val] => 11
        [dat] => 2012-05-1
        [title] => Test Product
    )

[1] => stdClass Object
    (
        [id] => 1
        [val] => 8
        [dat] => 2012-06-1
        [title] => Test Product
    )

[2] => stdClass Object
    (
        [id] => 2
        [val] => 7
        [dat] => 2012-06-1
        [title] => Test Product 2
    )

[3] => stdClass Object
    (
        [id] => 3
        [val] => 9
        [dat] => 2012-06-1
        [title] => Test Product 3
    )

)

Please let me know if you need anything else. Thanks in advance.

  • 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-05T23:06:18+00:00Added an answer on June 5, 2026 at 11:06 pm

    Try this:

    SELECT SUM(val), id, dat, title FROM (
    
                      SELECT table1.product_id as id, SUM(table1.qty) as val, DATE_FORMAT(table1.created, '%Y-%m-1') as dat, table2.title as title 
                      FROM table1
                      LEFT JOIN table3 ON table1.event_id = table3.id
                      LEFT JOIN table2 ON table1.product_id = table2.id
                      WHERE table1.user_id = $user_id AND table3.active != 2 AND table3.temp = 0 AND table2.active != 2
    
                      GROUP BY dat
    
                      UNION ALL
    
                      SELECT table4.product_id as id, SUM(table4.qty) as val, DATE_FORMAT(table4.created, '%Y-%m-1') as dat, table2.title as title 
                      FROM table4
                      LEFT JOIN table5 ON table4.festival_id = table5.id
                      LEFT JOIN table2 ON table4.product_id = table2.id
                      WHERE table4.user_id = $user_id AND table5.active != 2 AND table2.active != 2
    
                      GROUP BY dat
    
                      ) AS t
                      GROUP BY id, dat
                      ORDER BY dat ASC
    

    As the error suggests, every view/derived table must have an alias..

    Edit: This will give you records with distinct id/dat pair. Seems this is what is you are after.

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

Sidebar

Related Questions

Here is my query Select Gender from Table The result pane shows 1 1
Here is my query select t1.*, t2.name as customer, t2.name as vendor from order
Here is the query which will get me all the contacts have HD quality
I'm trying to write a table trigger which queries another table that is outside
I'm using this query to list duplicate records SELECT DISTINCT column1 from table group
OK, here is the deal. I have 3 queries putting data in different tables.
i am trying trying to get a query from oracle table called sys.all_objects into
I'm stuck with another coordinates related query here. I have a number of CSV
Here's the query: SELECT COUNT(*) AS c, MAX(`followers_count`) AS max_fc, MIN(`followers_count`) AS min_fc, MAX(`following_count`)
Here is the query that I am working on: SELECT `unitid`, `name` FROM apartmentunits

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.