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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:15:59+00:00 2026-06-09T15:15:59+00:00

table id | name | year Data: 1 | ham | 2006 2 |

  • 0

table

 id | name | year

Data:

 1  | ham   | 2006
 2  | ham   | 2007
 3  | ham   | 2008
 4  | amm   | 2007
 5  | amm   | 2008
 6  | inn   | 2009

Now i’m trying to construct a sql that gives me the following output (or similar)

 name  | y1   | y2   | y3   | y4    
 ham   | 2006 | 2007 | 2008 | null
 amm   | null | 2007 | 2008 | null
 inn   | null | null | null | 2009

When doing multiple (self) left joins I kind of gets this, but only if 2006 is set for that name. Is there a way to achieve this?

  • 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-09T15:16:01+00:00Added an answer on June 9, 2026 at 3:16 pm

    You want what is called a “pivot”, and call you need is a conditional sum:

    select
       name,
       sum(year = 2006) as y1,
       sum(year = 2007) as y2,
       sum(year = 2008) as y3,
       sum(year = 2009) as y4
    from mytable
    group by name;
    

    No joins, no muss, no fuss. And it will perform really well.

    This works because in mysql, true is 1 and false is 0, so summing a condition counts how many times it was true!

    Note that this will give you zeroes instead of nulls for “no data” years, which is probably better. If you really want nulls, use if(sum(year = 2006) = 0, null, sum(year = 2006)) as y1 etc, but hopefully you don’t need it.

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

Sidebar

Related Questions

Below I have table - Company id name value year 1 IBM 10 2011
I have a table that looks something like this: Name Year Value A 2000
I have a table that looks like this: Name Job Year ImpFile ImpDate ------------------------------------------------
Is table name prefix for fields to be update not allowed in SQL ?
I am trying to order data from the table 'tech_inquiry' by the Field 'number'
I am selecting the following data from an SQL database table in my controller
DB: SQL Server 2005 We have a table that has data in this manner:
I need some help to build SQL Query. I have table having data like:
I'm trying to build a query that will sort by Name and Year ,
I have 2 tables as follow : table1 has these fields: year,day,month,name table2 has

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.