MySQL query to transform table

Posted on 2013-11-06
Last Modified: 2013-11-06

I need a mysql query to transform Table1 into Table2.

Thank you,

Name    |     Date        |   Hours    
John    |    2013-10-09  |      8
Mark    |    2013-10-10  |     7.20
Andy    |    2013-10-10 |    9.10
Mark    |    2013-10-09  |     8.30
Mark    |    2013-10-11 |       8
 Name   |2013-10-09| 2013-10-10 | 2013-10-11
   John   |       8       |      -            |       -
   Mark   |      8.30   |     7.20        |      8
 Andy    |        -       |     9.10        |      -
Question by:Qw M
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 22

Expert Comment

ID: 39627276
thats using a Pivot Table

I credit Jimhorn for this  link 

good examples here
LVL 11

Expert Comment

ID: 39627318
Q1: Would Table2's date columns be limited (like always from day 1 to 31 of a day) or should it be dynamic based on the data (in other words show a column for every date whether there are 1 or 100)

Q2: What about dates not in range? If Table 1 has data for 2013-10-01, 2013-10-02 and 2013-10-04 but not 2013-10-03, should it show 2013-10-03 with a 0 count?

Author Comment

by:Qw M
ID: 39627334

Q1:  date columns will be limited (0-31).
Q2: should show the character "-".

Thank you!
LVL 11

Accepted Solution

Louis01 earned 500 total points
ID: 39627351
This should give you a good start:
  SUM(IF(DAY([Date]) = 1, [Hours], NULL)) AS '01',
  SUM(IF(DAY([Date]) = 2, [Hours], NULL)) AS '02',
  SUM(IF(DAY([Date]) = 31, [Hours], NULL)) AS '31',
  [Date] between '2013-10-01' and '2013-10-31'

Open in new window

The output in the date columns would be numeric. You can change it to VARCHAR and check for NULL and show "-" but it is often easier to show "-" for null in whatever application output the values.

Author Comment

by:Qw M
ID: 39627381
Thank you Louis01!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question