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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:13:33+00:00 2026-06-03T18:13:33+00:00

I have a table events like this id | user_id | date | is_important

  • 0

I have a table “events” like this

   id   |  user_id   |   date     |  is_important
---------------------------------------------------
   1    |     3      | 01/02/2012 |       0
   1    |     3      | 01/02/2012 |       1
   1    |     3      | 01/02/2011 |       1
   1    |     3      | 01/02/2011 |       1
   1    |     3      | 01/02/2011 |       0

Basically, what I need to get is this:

(for the user_id=3)

   year   |  count   |   count_importants
--------------------------------------------
  2012    |    2     |          1
  2011    |    3     |          2

I’ve tried this:

SELECT YEAR(e1.date) as year,COUNT(e1.id) as count_total, aux.count_importants
FROM events e1 
LEFT JOIN 
(
  SELECT YEAR(e2.date) as year2,COUNT(e2.id) as count_importants
  FROM `events` e2
  WHERE e2.user_id=18 
  AND e2.is_important = 1
  GROUP BY year2
) AS aux ON aux.year2 = e1.year
WHERE e1.user_id=18 
GROUP BY year

But mysql gives me an error

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'aux ON aux.year2 = e1.year WHERE e1.user_id=18 GROUP BY year LIMIT 0, 30' at line 10

And i’ve run out of ideas to make this query u_Uº. Is it possible to do this using only one query??

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-03T18:13:35+00:00Added an answer on June 3, 2026 at 6:13 pm

    Edit: I think I over-complicated things. Can’t you just do this in a simple query?

    SELECT 
        YEAR(`year`) AS `year`, 
        COUNT(`id`) AS `count`, 
        SUM(`is_important`) AS `count_importants`
    FROM `events`
    WHERE user_id = 18
    GROUP BY YEAR(`year`)
    

    Here’s the big solution that adds summaries 🙂

    Consider using MySQL GROUP BY ROLLUP. This will basically do a similar job to a normal GROUP BY, but will add rows for the summaries too.

    In the example below, you see two records for Finland in 2000, for £1500 and £100, and then a row with the NULL product with the combined value of £1600. It also adds NULL rollup rows for each dimension grouped by.

    From the manual:

    SELECT year, country, product, SUM(profit)
    FROM sales
    GROUP BY year, country, product WITH ROLLUP
    
    +------+---------+------------+-------------+
    | year | country | product    | SUM(profit) |
    +------+---------+------------+-------------+
    | 2000 | Finland | Computer   |        1500 |
    | 2000 | Finland | Phone      |         100 |
    | 2000 | Finland | NULL       |        1600 |
    | 2000 | India   | Calculator |         150 |
    | 2000 | India   | Computer   |        1200 |
    | 2000 | India   | NULL       |        1350 |
    | 2000 | USA     | Calculator |          75 |
    | 2000 | USA     | Computer   |        1500 |
    | 2000 | USA     | NULL       |        1575 |
    | 2000 | NULL    | NULL       |        4525 |
    | 2001 | Finland | Phone      |          10 |
    | 2001 | Finland | NULL       |          10 |
    | 2001 | USA     | Calculator |          50 |
    | 2001 | USA     | Computer   |        2700 |
    | 2001 | USA     | TV         |         250 |
    | 2001 | USA     | NULL       |        3000 |
    | 2001 | NULL    | NULL       |        3010 |
    | NULL | NULL    | NULL       |        7535 |
    +------+---------+------------+-------------+
    

    Here’s an example the specifically matches your situation:

    SELECT year(`date`) AS `year`, COUNT(`id`) AS `count`, SUM(`is_important`) AS `count_importants`
    FROM new_table
    GROUP BY year(`date`) WITH ROLLUP;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data in an SQLite table that looks like this: user_id event_date ----------
I have a table in my postgresql 8.4 database like this: id(serial), event_type_id(id, foreign
I have an events table with a 'featureEvent' boolean column. I need to display
I have an Events table with a not-null date field. Based on a pivot
I have a deep hierarchical model that looks like this: Accounts -> Venues ->Events
I have the following call in my RoR script: Foo.where(event = 'Login').group('user_id').order('user_id ASC').count() This
I have 3 tables: table events . In this I have fields such as
I have 2 tables table 'events' id event_name date 1 buy milk 1319074920 //
I have a table like so: Table eventlog user | user_group | event_date |
I have a database table of events. Each event has a category and a

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.