Solved

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

Posted on 2008-06-13
4
211 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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

696 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