简体   繁体   中英

select distinct columns from db2 table

I'm using spring batch to read from table and write the data to another table and I'm using spring writer for the insertion part. My problem is:

I'm getting data from a table that looks like the following:

cust   type   date
====   =====  ====
 1       P    1985
 1       P    1980
 1       P    1970
 2       P    1984

and I'm trying to insert these data to another table by running a query that select distinct( cust , type) from the above table to get the following result:

cust   type   date
====   =====  ====
 1       P    1985
 2       P    1984 

so basically what I want to do is to get the distinct set of cust and type and if there are multiple records for this cust. type cust. type set then I get the max(date) .

Is there anyway to do so in using query? or any recommendation for an efficient approach?

Thanks in advance!

UPDATE:

there are some records in date that have NULL value so for example

cust   type   date
====   =====  ====
 1       P    NULL
 1       P    NULL
 1       P    NULL
 2       P    1984

and if I query for max(date) it will not return back any record from cust(1) .. any suggestions will be appreciated!

select 
   cust, 
   type, 
   max(isnull(date,'1/jan/1900')) 
from 
   <table>
group by 
    cust, 
    type

Should do the job!

EDIT:

Added in an isnull around the date, so that if it is null it will still return a value. I haven't tested it myself, so let me know how it goes!

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