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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:02:36+00:00 2026-06-11T10:02:36+00:00

Not long ago I was given a task to implement report. The problem that

  • 0

Not long ago I was given a task to implement report. The problem that i am facing is the following. I have a table:

name      value   type
------------------------
100 Kasa  | 50000 |  B
100 Kasa  | 30000 |  A
 80 Kasa  |  500  |  A
 80 Kasa  |  50   |  B
200 Hello | 3000  |  A
645 Bye   | 1234  |  B

What I want to achieve is group by name, sum values if type is same and if type is different then write it into another cell. I have only 2 types.

I want to get result as

name       valueB    valueA   
--------------------------
100 Kasa  | 50000 |  30000
 80 Kasa  |   0   |   550
200 Hello |   0   |   3000
645 Bye   | 1234  |    0

For now, I am grouping them in php, which makes a mess in code, and now i am hoping that someone can help me to do it with mysql

  • 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-11T10:02:37+00:00Added an answer on June 11, 2026 at 10:02 am

    I think your expected output (result) is wrong
    It should be:

    NAME        VALUEB  VALUEA
    --------------------------
    100 Kasa    50000   30000
    200 Hello   0       3000
    645 Bye     1234    0
    80 Kasa     50      500
    

    You can use SUM function for that

    With CASE statement:

    SELECT name,
           SUM(CASE type WHEN 'B'THEN value ELSE 0 END) valueB,
           SUM(CASE type WHEN 'A' THEN value ELSE 0 END) valueA
    FROM myTable
    GROUP BY name;
    

    Or with IF statement:

    SELECT name,
           SUM(IF (type = 'B' , value , 0 )) valueB,
           SUM(IF (type = 'A' , value , 0 )) valueA
    FROM myTable
    GROUP BY name;
    

    See this SQLFiddle

    If your expected output is like this (NULL instead of 0)

    SELECT name,
           GROUP_CONCAT(CASE type WHEN 'B'THEN value ELSE NULL END) valueB,
           GROUP_CONCAT(CASE type WHEN 'A' THEN value ELSE NULL END) valueA
    FROM myTable
    GROUP BY name;
    

    See this SQLFiddle

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

Sidebar

Related Questions

Somebody asked similar question not long ago. But nobody answered comprehensively. Assume I have:
not so long ago i found out that you can do this in css
I posted not so long ago a class diagram for an application that I
Not long ago, we switched to new Crystal Reports version, and I have a
Not long ago developed a system based on compact framework for Windows Mobile Professional
Not so long ago I started to learn to use System.Reflection.Emit namespace. I'm now
I remember the days (not too long ago, really) when log4net came along with
This enum used to exist not too long ago. I am upgrading to Castle
I was talking to someone in a bar not too long ago, and the
I'm no longer getting intellisense in my XAML. Not too long ago, I changed

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.