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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:12:59+00:00 2026-06-14T11:12:59+00:00

I am having trouble trying to get this query right. Say I have a

  • 0

I am having trouble trying to get this query right.

Say I have a table with the following data:

id    |   xyz     |   code
===========================
1     |    1      |    ba
2     |    1      |    zz
3     |    1      |    ba
1     |    1      |    zz
1     |    1      |    ba
2     |    1      |    zz

And I’d like to get a sum of ‘xyz’, grouped by id e.g.

SELECT id, SUM(xyz) as mysum FROM table WHERE xyz=1 GROUP BY id

I would end up with the results:

id   |  mysum 
================
1    |  3
2    |  2
3    |  1

Perfect.

The thing is, what I’d actually like to do is group by the ‘code’ column as well, so that if say for id: 1, which has 2 entries with the code ‘ba’, then the result would be more like this:

id   |  mysum 
================
1    |  2
2    |  1
3    |  1

Here is where my limited understanding of mysql fails me. Because I would think to do a query like this:

SELECT id, SUM(xyz) AS mysum FROM table WHERE xyz=1 GROUP BY id, code

But this clearly gives the results I’m not after.

How can I do this sort of query that does a SUM of column xyz, while omitting it if it has already added one that has the same ‘code’?

  • 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-14T11:13:01+00:00Added an answer on June 14, 2026 at 11:13 am
    SELECT id,
           SUM(CASE WHEN code = 'ba' then 1 ELSE 0 END)
    FROM table1
    GROUP BY id
    
    • SQLFiddle Demo

    follow-up question: why does id=2 has a value of 1, isn’t it zero?

    UPDATE 1

    SELECT id,
           count(Distinct code)
    FROM table1
    GROUP BY id
    
    • SQLFiddle Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble trying to define the SQL query for this table: There's a
I am having trouble trying to get iterators for inner sub-containers. Basically imagine this
I`m having trouble trying to optimize this query with OVER (PARTITION BY ...) because
I'm having trouble synching a simple textfile, I get this error when trying to
I am having trouble with this query. I have read quite a few things
I'm trying to get a query from a specific model. I'm having trouble getting
Trying to generate a table summing totals etc from a query but having trouble
I'm having trouble trying to get a linebreak included in a Stringbuilder to appear
I am having trouble trying to get NAnt to compile my project, which consists
I am having trouble trying to get a group of li tags to align

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.