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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:49:31+00:00 2026-05-28T19:49:31+00:00

I have (again) this tables: User: id | name ————- 1 | ‘John’ 2

  • 0

I have (again) this tables:

User:
id | name
-------------
1  | 'John'
2  | 'Peter'
3  | 'Luke'

Accounts:
id | userid | amount | date
--------------------------
1  | 1      | 1000   | '2012-01-26'
2  | 1      | 2000   | '2011-12-25'
3  | 1      | 1000   | '2012-01-25'
4  | 2      | 1500   | '2012-01-15'
5  | 3      | 2500   | '2011-11-30'

I need to collect data, so I have, at the same time, a resultset with the users, the count of accounts for each user, the sum of the amounts for the accounts for each user, and also, the count of accounts for each user, but only that have a date only on the present year…

Something like this:

Some-query:
UserName | CountAccts | SumAccts | CountAcctsThisYear
-----------------------------------------------------
'John'   | 3          | 4000     | 2
'Peter'  | 1          | 1500     | 1
'Luke'   | 1          | 2500     | 0

I manage to get the first 3 columns with this query:

SELECT u.name as Username,
       COUNT(a.id) as CountAccts,
       SUM(a.amount) as SumAccts
FROM   User u
LEFT JOIN Accounts a ON u.id = a.userid
GROUP BY u.id;

Also, I can get the first and last columns with this other one:

SELECT u.name as Username,
       COUNT(acctsthisyear.id) as CountAcctsThisYear
FROM   User u
LEFT JOIN (SELECT a.id
             FROM Accounts a
            WHERE DATE_FORMAT(a.date,'%Y') = DATE_FORMAT(CURDATE(),'%Y')
          ) AS acctsthisyear ON u.id = acctsthisyear.userid
GROUP BY u.id;

But, here’s the problem, I can’t manage to use this two queries in such a way to get the final result I’m expecting…

I have…
– tried to left join the Accounts table and the acctsthisyear subquery with the User table (grouping by the us.id field), but all I get is something like this:

UserName | CountAccts | SumAccts | CountAcctsThisYear
-----------------------------------------------------
'John'   | 6          | 6000     | 6
'Peter'  | 1          | 1500     | 1
'Luke'   | 0          | 0        | 0

(can’t remember all of the details, but the thing is that, for example, the 2 results for ‘John’ in CountAcctsThisyear, get duplicated too in the result, and all gets multiplied…)

  • tried each query as a subquery:
    SELECT * FROM
    (first query with u.id as ufid also selected) AS first,
    (second query with u.id as usid also selected) AS second
    GROUP BY ufid, usid

and got something like this:

UserName | CountAccts | SumAccts | CountAcctsThisYear
-----------------------------------------------------
'John'   | 3          | 4000     | 2
'Peter'  | 1          | 1500     | 2
'Luke'   | 1          | 2500     | 2
'John'   | 3          | 4000     | 1
'Peter'  | 1          | 1500     | 1
'Luke'   | 1          | 2500     | 1
'John'   | 3          | 4000     | 0
'Peter'  | 1          | 1500     | 0
'Luke'   | 1          | 2500     | 0

(and if I invert the Group by elements, all I get is the same but grouped in a different order…)

Any way… That’s it… any ideas of what is the right way to achieve what I’m looking for? I’m really frustrated with it, since I’m really no expert on SQL, specially on Joins :-/

  • 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-28T19:49:32+00:00Added an answer on May 28, 2026 at 7:49 pm

    try this:

    SELECT u.`Name` as UserName,
           Count(*) as CountAccts,
           SUM(a.`amount`) as SumAccts,
           COALESCE(iTable.CountAcctsThisYear, 0) as CountAcctsThisYear
    FROM    `User` u INNER JOIN `Accounts` a
                        ON u.`ID` = a`.`ID`
                   LEFT JOIN
                       (SELECT `ID`, Count(*) as CountAcctsThisYear
                        FROM `Accounts`
                        WHERE DATE_FORMAT(`date`,'%Y') = DATE_FORMAT(CURDATE(),'%Y')
                        GROUP BY `ID`) as iTable
                    ON `User`.`ID` = iTable.`ID`
    GROUP BY `User`.`Name`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this databases: table<User>(UserID,Name,Surname,Username,Password,Email) , table<Role>(RoleID,RoleName,Description) , and table<UsersInRole>(UserID,RoleID) . I create a
I have this case <WrapPanel> <CheckBox>Really long name</CheckBox> <CheckBox>Short</CheckBox> <CheckBox>Longer again</CheckBox> <CheckBox>Foo</CheckBox> <Slider MinWidth=200
Again I have a question regarding this plugin: http://t.wits.sg/2008/06/20/jquery-progress-bar-11/ What I want to achieve
This time I have an error which I have been trying again to figure
I followed this guide here perfectly and have gone through it again but when
I have a table in SQL Server which looks like this: ID Code Name
Hi (I'm pretty new to this), I have created a portal where the user
I have very simple form along with two database tables. In this form is
The Schema: I have 3 Tables: User Feature User_has_Feature: initially all users has no
I have once again fleshed out Ruby, after two years of not touching it,

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.