Solved

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

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

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 31

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now