简体   繁体   中英

How to pull the most recent values from a T-SQL table

I have a database table that I need to process with either a view or a stored procedure or something else that gives me a result based on the live data.

The table holds records of people with data associated with each one. The thing is that people can be in the table more than once. Each record shows a time when one or more pieces of information was recorded for an individual.

The identifier field for the people is cardholder_index. I need to take a DISTINCT list of that field. There is also a date field called bio_complete_date. What I need to do is, for all the other fields in the table, take the most recent non-null (or possibly non-zero) value.

For instance, there is a bmi field. For each distinct cardholder index, I need to take the most recent (by the bio_complete_date field) non-null bmi for that cardholder_index. But there's also a body_fat field, and I need to take the most recent non-null value in that field, which might not necessarily be the same row as the most recent non-null bmi value.

For the record, the table itself does have its own unique identifier column, bio_id, if that helps.

I don't need to show when the most recent piece of information was taken. I just need to show the data itself.

I figure I need to do a distinct on the card_holder index, and then join to it the result sets of querys for each other field. It's writing the subqueries that is giving me problems.

From your description I guess your table looks something like this:

create table people (
    bio_id int identity(1,1), 
    cardholder_index int, 
    bio_complete_date date, 
    bmi int, 
    body_fat int
)

If so, one way (of many) to do the query would be to use correlated queries to pull the latest non-null value for the cardholder_index, either using subqueries like this:

select 
    cardholder_index, 
    (
       select top 1 bmi 
       from people 
       where cardholder_index = p.cardholder_index and bmi is not null 
       order by bio_complete_date desc
    ) as latest_bmi, 
    (
       select top 1 body_fat 
       from people 
       where cardholder_index = p.cardholder_index and body_fat is not null 
       order by bio_complete_date desc
    ) as latest_body_fat
from people p
group by cardholder_index

or to use the apply operator like this:

select cardholder_index, latest_bmi.bmi, latest_body_fat.body_fat
from people p
outer apply (
    select top 1 bmi 
    from people 
    where cardholder_index = p.cardholder_index and bmi is not null 
    order by bio_complete_date desc
) as latest_bmi
outer apply (
    select top 1 body_fat 
    from people 
    where cardholder_index = p.cardholder_index and body_fat is not null 
    order by bio_complete_date desc
) as latest_body_fat
group by cardholder_index, latest_bmi.bmi, latest_body_fat.body_fat

Sample SQL Fiddle demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM