Solved

MySQL query to transform table

Posted on 2013-11-06
5
250 Views
Last Modified: 2013-11-06
Hi,

I need a mysql query to transform Table1 into Table2.

Thank you,


Table1
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
 
Table2
 Name   |2013-10-09| 2013-10-10 | 2013-10-11
   John   |       8       |      -            |       -
   Mark   |      8.30   |     7.20        |      8
 Andy    |        -       |     9.10        |      -
0
Comment
Question by:Qw M
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:plusone3055
Comment Utility
thats using a Pivot Table

I credit Jimhorn for this  link
http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

good examples here
0
 
LVL 11

Expert Comment

by:Louis01
Comment Utility
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?
0
 

Author Comment

by:Qw M
Comment Utility
@Louis01:

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

Thank you!
0
 
LVL 11

Accepted Solution

by:
Louis01 earned 500 total points
Comment Utility
This should give you a good start:
SELECT
  [Name],
  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',
FROM
  Table1
WHERE
  [Date] between '2013-10-01' and '2013-10-31'
GROUP BY
  [Name];

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.
0
 

Author Comment

by:Qw M
Comment Utility
Thank you Louis01!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now