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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:15:24+00:00 2026-06-17T09:15:24+00:00

So I have this data from basic mySQL select query, that outputs per row.

  • 0

So I have this data from basic mySQL select query, that outputs per row.

-Basic mySQL select query

ID---------DATE---------POINT1---POINT2------TOTAL
1----2013-01-03----------10----------16---------26
2----2013-01-03----------11----------22---------33
3----2013-01-03----------15----------7----------22
1----2013-01-04----------20----------4----------24
2----2013-01-04----------8-----------32---------40
3----2013-01-04----------16----------12---------28
1----2013-01-05----------12----------17---------29
2----2013-01-05----------2-----------29---------31
3----2013-01-05----------8-----------10---------18

What I want to do is sort the data by date in columns and by id in rows dynamically, something like per month. here is the desired output,

/----------/2013-01-03/---------/2013-01-04/------/2013-01-05/------/
ID--Point 1-Point2-Total-Point 1-Point2-Total-Point 1-Point2-Total
1----10-------16-----26-----20-------4-----24----12-----17------29
2----11-------22-----33------8------32-----40-----2-----29------31
3----15-------7------22-----16------12-----28-----8-----10------18

then output it to csv or excel. I’m kinda lost on how I can achieve this. If someone can guide me it would be great. thanks

  • 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-17T09:15:25+00:00Added an answer on June 17, 2026 at 9:15 am

    You can unpivot the data in your columns and then perform a pivot to transform all of the data back into columns:

    select id,
      sum(case when date='2013-01-03' and col='Point1' then value end) Point1_01032013,
      sum(case when date='2013-01-03' and col='Point2' then value end) Point2_01032013,
      sum(case when date='2013-01-03' and col='Total' then value end) Total_01032013,
      sum(case when date='2013-01-04' and col='Point1' then value end) Point1_01042013,
      sum(case when date='2013-01-04' and col='Point2' then value end) Point2_01042013,
      sum(case when date='2013-01-04' and col='Total' then value end) Total_01042013,
      sum(case when date='2013-01-05' and col='Point1' then value end) Point1_01052013,
      sum(case when date='2013-01-05' and col='Point2' then value end) Point2_01052013,
      sum(case when date='2013-01-05' and col='Total' then value end) Total_01052013
    from
    (
      select id, date_format(date, '%Y-%m-%d') date, 'Point1' col, Point1 as value
      from yourtable
      union all
      select id, date_format(date, '%Y-%m-%d') date, 'Point2' col, Point2
      from yourtable
      union all
      select id, date_format(date, '%Y-%m-%d') date, 'Total' col, Total
      from yourtable
    ) src
    group by id;
    

    See SQL Fiddle with Demo

    The result is:

    | ID | POINT1_01032013 | POINT2_01032013 | TOTAL_01032013 | POINT1_01042013 | POINT2_01042013 | TOTAL_01042013 | POINT1_01052013 | POINT2_01052013 | TOTAL_01052013 |
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
    |  1 |              10 |              16 |             26 |              20 |               4 |             24 |              12 |              17 |             29 |
    |  2 |              11 |              22 |             33 |               8 |              32 |             40 |               2 |              29 |             31 |
    |  3 |              15 |               7 |             22 |              16 |              12 |             28 |               8 |              10 |             18 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this query for retrieving data from mysql as below select FeedbackCode,EMailID,FeedbackDetail,
I have this SQL Query that pulls data from 3 tables. I am unable
I like to have a query like this: select data from table where (x
I have this script that collects data from users and I want to check
I have this function in jQuery that gets data from a page with POST,
I have this data from a xml file: <?xml version=1.0 encoding=utf-8 ?> <words> <id>...</id>
i have this simple code to load data from other php page using get
I have this FIFO which I'm going to use to store data from network
I have this code to display the data retrieved from DB in accounts_view.php :
I have this code to update my SQL database from data in a textbox,

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.