Solved

Collapsing multiple records into a single record where some fields are null

Posted on 2008-06-13
4
210 Views
Last Modified: 2010-03-19
I've got an issue with a table which has multiple versions of the same key record however some are missing relevant data.  I know I could build a cursor to loop through but I thought someone would have a more elegant SQL solution.

Example:

key|version|field1|field2|field3
123|10000|FL      |888  |null
123|10001|null    |888  |ABC

Condensed record would be
123|x         |FL      |888  |ABC
The version number is not relevant on the condensed record.

The actual table contains about 80 fields and has up to 15 versions of the record.



0
Comment
Question by:bigwahoo2
[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
  • 2
4 Comments
 
LVL 32

Expert Comment

by:awking00
ID: 21779580
Is it possible to have different field values other than null for a given key?
0
 

Author Comment

by:bigwahoo2
ID: 21779772
I'm making the assumption your calling field1,2,3 key fields here, but no, either they are filled in with the same data or null.  Just need to compress it down to one record, which is fully populated based on all fields.
0
 
LVL 7

Accepted Solution

by:
Cedric_D earned 250 total points
ID: 21780916
select
  key,
  max(field1),    -- these are automatically remove NULL entries
  max(field2),
  max(field3)
from tbl
group by key


you may also inlude ISNULL inside MAX if it will be more appropriable:
  MAX(ISNULL(field1,'')),  MAX(ISNULL(field2, 0))
0
 
LVL 32

Expert Comment

by:awking00
ID: 21780961
I have a terrible time posting to EE, so I see Cedric_D beat me to it. The reason I asked the question was that if the values were all the same other than null, you would be able to use max (you could also use min) :-)
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Options for Linking SQL tables to Access 2013 9 69
Database Owner 3 44
get count of orders by customer Sql Server table. 3 46
Migrate SQL 2005 DB to SQL 2016 4 25
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.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

737 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