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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:33:31+00:00 2026-06-18T10:33:31+00:00

I have a table as below, +—–+——–+———–+———-+ | id | type_id| product_id| date |

  • 0

I have a table as below,

+-----+--------+-----------+----------+
| id  | type_id| product_id| date     |
+-----+--------+-----------+----------+
|  1  |   1    |   300     |22/01/2013|
|  2  |   1    |   800     |22/01/2013|
|  3  |   1    |   400     |30/01/2013|
|  4  |   1    |   300     |05/02/2013|
|  5  |   5    |   300     |27/02/2013|
|  6  |   1    |   300     |28/02/2013|
|  7  |   3    |   400     |12/03/2013|
|  8  |   5    |   400     |02/03/2013|
|  9  |   1    |   300     |06/03/2013|
| 10  |   1    |   400     |06/03/2013|
| 11  |   5    |   400     |06/03/2013|
| 12  |   1    |   400     |08/03/2013|

Lets say if I want to get count of each type for each product group by month and year, Is this possible? Output that I’m after should be something similar to following..

+-----------+------------+-------------+------------+------+------+
|product_id |count_type_1|count_type_3 |count_tyep_5| month| year |
+-----------+------------+-------------+------------+------+------+
| 300       |     1      |      0      |     0      |   01 | 2013 |
| 800       |     1      |      0      |     0      |   01 | 2013 |
| 400       |     1      |      0      |     0      |   01 | 2013 |
| 300       |     2      |      0      |     1      |   02 | 2013 |
| 300       |     1      |      0      |     0      |   03 | 2013 |
| 400       |     2      |      1      |     2      |   03 | 2013 | 

Is this can be achieve using a single SQL?

PS: This is on a MYSQL server

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

    You can use the following to pivot the data which uses an aggregate function and a CASE expression to create each column:

    select 
      product_id,
      sum(case when type_id=1 then 1 else 0 end) count_type_1,
      sum(case when type_id=2 then 1 else 0 end) count_type_2,
      sum(case when type_id=3 then 1 else 0 end) count_type_3,
      sum(case when type_id=4 then 1 else 0 end) count_type_4,
      sum(case when type_id=5 then 1 else 0 end) count_type_5,
      month(date) month,
      year(date) year
    from yourtable
    group by product_id, month(date), year(date)
    

    See SQL Fiddle with Demo

    This gives the result:

    | PRODUCT_ID | COUNT_TYPE_1 | COUNT_TYPE_2 | COUNT_TYPE_3 | COUNT_TYPE_4 | COUNT_TYPE_5 | MONTH | YEAR |
    --------------------------------------------------------------------------------------------------------
    |        300 |            1 |            0 |            0 |            0 |            0 |     1 | 2013 |
    |        300 |            2 |            0 |            0 |            0 |            1 |     2 | 2013 |
    |        300 |            1 |            0 |            0 |            0 |            0 |     3 | 2013 |
    |        400 |            1 |            0 |            0 |            0 |            0 |     1 | 2013 |
    |        400 |            2 |            0 |            1 |            0 |            2 |     3 | 2013 |
    |        800 |            1 |            0 |            0 |            0 |            0 |     1 | 2013 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have the table below. I want to get all the friends,
I have table as below: GroupOrderID Station Date ----------------------------------------------- 28797 NB003 2013-01-03 12:53:00 28797
I have a table Like below. I want the product_id of Minimum, Maximum and
Lets say I have table as below. create table t1 (id INT, tempData varchar(25));
I have a table as below ID Date 1 Null 1 Null 1 Null
I have a table as below Name Priority Date ------------------------- A 2 d1 B
I have a table as below dbo.UserLogs ------------------------------------- Id | UserId |Date | Name|
I have a table like below. I want to be able to get a
Currently i have a table like below Table1 hTeam-----aTeam-------date--- 3----------5 ------2012/01/01 4 -------- 7
I have a table below with various column. I want to update the values

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.