Solved

SQL Code to flatten out records

Posted on 2009-04-02
6
531 Views
Last Modified: 2013-11-25
I have a table sitting in SQL Server. I have a need to "flatten out" a table.  Here is my basic table

Date            Person      Prize1      Prize2      Prize3
1/1/2009 11:01:02      Bill      Item1      Item2      Item3
1/1/2009 11:05:23      Bill      Item4      NULL      NULL
I need to manipulate the data so Bill is just one record. A 4th field would need to be added to the output. I dont care about the timestamp. The new record would just have a value of 1/1/2009.  Would need sql code to do this.

Result set would be:
Date             Person    Prize1     Prize2   Prize3   Prize4
1/1/2009      Bill           Item1       Item2     Item3     Item4
0
Comment
Question by:d1cjm1ex
[X]
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
  • 3
  • 2
6 Comments
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24051282
what if second record also have prize1,2 and 3 along with 4, should first record need to be overwritten?
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 24051606
SELECT Person, CONVERT(CHAR(8), Date, 112),
    MAX(CASE WHEN Prize2 IS NOT NULL THEN Prize1 ELSE '' END) AS Prize1,
    MAX(CASE WHEN Prize2 IS NOT NULL THEN Prize2 ELSE '' END) AS Prize2,
    MAX(CASE WHEN Prize2 IS NOT NULL THEN Prize3 ELSE '' END) AS Prize3,
    MAX(CASE WHEN Prize2 IS NOT NULL THEN '' ELSE Prize1 END) AS Prize4
FROM tablename
GROUP BY Person, CONVERT(CHAR(8), Date, 112)
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 24051649
CORRECTION: Forgot to put the day back into a date format:


SELECT Person, CAST(CONVERT(CHAR(8), Date, 112) AS DATETIME) AS Date,
    MAX(CASE WHEN Prize2 IS NOT NULL THEN Prize1 ELSE '' END) AS Prize1,
    MAX(CASE WHEN Prize2 IS NOT NULL THEN Prize2 ELSE '' END) AS Prize2,
    MAX(CASE WHEN Prize2 IS NOT NULL THEN Prize3 ELSE '' END) AS Prize3,
    MAX(CASE WHEN Prize2 IS NOT NULL THEN '' ELSE Prize1 END) AS Prize4
FROM tablename
GROUP BY Person, CAST(CONVERT(CHAR(8), Date, 112) AS DATETIME)
0
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!

 

Author Comment

by:d1cjm1ex
ID: 24051907
The first record should not be overwritten.  Additional columns would need to be added to the output to include all prizes.
0
 

Author Comment

by:d1cjm1ex
ID: 24053983
Your solution does create the result set I gave as an example, however I guess I should have been a bit clearer....I need this to be a bit more dynamic in that the NULL values in the second record could contain actual values (Prize 6, etc).  If I add the value "Item6" in the second record for the Prize2 column, the result set is no longer correct.

For a given day for a given person, I need to create one record that has all the Prize values. Each record can have a max of 3 prizes.  The second (or third or fourth)  record may only have one prize value or it could have 2 or 3.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 24060190
Ooh, yeah, I misunderstood -- that requirement's going to be considerably more complicated :-) .
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Removing SCCM 2016 4 53
SQL Query (lookup) 8 65
Load Fact table in SQL Server SSIS package 14 47
SQL - Using an 'array' kind of variable 5 19
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

751 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