Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-06-13
4
Medium Priority
?
217 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 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

618 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