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

The Archive Base Latest Questions

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

By writing the following query SELECT item_name, YEAR( DATE ) , SUM( item_sold_qty )

  • 0

By writing the following query

SELECT item_name, YEAR( DATE ) , SUM( item_sold_qty ) 
FROM item
JOIN sales ON item.id = sales.item_number
GROUP BY YEAR( DATE ) , item_name
ORDER BY item_name

i’m able to get the following result

item_name   YEAR( DATE )    SUM( item_sold_qty )
pencil          2011              22
pencil          2012              44
eraser          2012              22
eraser          2011              11
pen             2012              66
pen             2011              33
nib             2012              88
nib             2011              44

Instead i want the result in the following way

 item_name      2011    2012
    pencil       22      44            
    eraser       11      22                   
    pen          33      66                  
    nib          44      88

I’m not really good at sql and have no clue for how to set the years as column names.
Need help.

NOTE :: My database has 2 tables. Sales table has date column which has different dates like 2012-03-01, 2012-04-02, 2011-07-03, so on…

  • 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-03T05:28:34+00:00Added an answer on June 3, 2026 at 5:28 am

    Maybe something like this:

    SELECT 
        item_name, 
        SUM(CASE WHEN YEAR( DATE )=2011 THEN item_sold_qty ELSE 0 END) AS '2011',
        SUM(CASE WHEN YEAR( DATE )=2012 THEN item_sold_qty ELSE 0 END) AS '2012'
    FROM 
        item
    JOIN sales ON item.id = sales.item_number
    GROUP BY
        item_name
    ORDER BY 
        item_name
    

    EDIT

    If you want the other years and still sum them. Then you can do this:

    SELECT 
        item_name, 
        SUM(CASE WHEN YEAR( DATE )=2011 THEN item_sold_qty ELSE 0 END) AS '2011',
        SUM(CASE WHEN YEAR( DATE )=2012 THEN item_sold_qty ELSE 0 END) AS '2012',
        SUM(CASE WHEN NOT YEAR( DATE ) IN (2011,2012) THEN item_sold_qty ELSE 0 END) AS 'AllOtherYears'
    FROM 
        item
    JOIN sales ON item.id = sales.item_number
    GROUP BY
        item_name
    ORDER BY 
        item_name
    

    EDIT2

    If you have a lot of years and you do not want to keep on adding years. Then you need to using dynamic sql. That means that you concat a varchar of the sql and then execute it.

    Useful References:

    • MySQL pivot table with dynamic headers based on single column data
    • How To have Dynamic SQL in MySQL Stored Procedure
    • MySQL/Pivot table
    • MYSQL – Rows to Columns
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Writing an SQL query to SELECT item from the following table I
I've seen people writing the following query SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2
I have the following query: select *, sum(hrs.WPTOTT) as Hours from mstwmlt inner join
I am writing following code to fetch a row from table: $query = SELECT
I am working on codeigniter and I am writing the following query: $this->db->select('*'); $this->db->from('tbl_profile');
Let's say I have the following simple query SELECT TOP 1 name FROM months
I am writing the following sub query for some project specific purpose: SELECT count(*)
I run the following query SELECT * FROM ( SELECT * FROM Client );
I have the following SQL query: SELECT DISTINCT ProductNumber, PageNumber FROM table I am
I'm writing a query to get fname from user_name_n column in following query I'm

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.