Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…

766 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