简体   繁体   中英

How to select a column value from the highest distinct value of another column

I have a table named License with columns of:

    pk_license  
    fk_customer 
    option_1

and values of:

    4297    1   0
    30640   1   1
    29990   87  0
    29373   134 0
    2720    144 1
    30579   144 0
    24799   172 0
    30035   183 1
    27855   197 0
    30337   197 1
    30596   198 1
    28083   216 0

I need to get the value of option_1 for the highest pk_license of each distinct fk_customer. I've tried:

SELECT        *
FROM          License li
INNER JOIN
    (SELECT   opt_2, fk_station, MAX(pk_license) AS MaxLicense
     FROM     License
     GROUP BY opt_2, fk_station) groupedli 
ON li.opt_2 = groupedli.opt_2
AND li.pk_license = groupedli.MaxLicense
ORDER BY li.fk_station

...but I'm getting two rows for each value of option_1. What I'm trying to do is to determine the most recent option_1 value for each customer.

Try this:

SELECT        li.*
FROM          License li
INNER JOIN
    (SELECT   fk_customer,
              max(pk_license) as maxLicense
     FROM  License  
     GROUP BY fk_customer) lm
ON li.fk_customer = lm.fk_customer
AND li.pk_license = lm.MaxLicense
ORDER BY li.fk_customer, li.pk_license 

You can try this:

SELECT DISTINCT(`fk_customer`),`pk_license`,`option_1` FROM `License` order by `pk_license` DESC

Query result:

1   | 30640 | 1
198 | 30596 | 1
144 | 30579 | 0
197 | 30337 | 1
183 | 30035 | 1
87  | 29990 | 0
134 | 29373 | 0
216 | 28083 | 0
197 | 27855 | 0
172 | 24799 | 0
1   | 4297  | 0
144 | 2720  | 1

Here is my solution:

Create table tblLicense 
( 
     pk_license  int,
     fk_customer int,
     option_1 int
)
Go 

   Insert into tblLicense values (4297,    1,   0)
   Insert into tblLicense values (30640,  1  ,1)
   Insert into tblLicense values (29990,  87 ,0)
   Insert into tblLicense values (29373,  134,0)
   Insert into tblLicense values (2720 ,  144,1)
   Insert into tblLicense values (30579,  144,0)
   Insert into tblLicense values (24799,  172,0)
   Insert into tblLicense values (30035,  183,1)
   Insert into tblLicense values (27855,  197,0)
   Insert into tblLicense values (30337,  197,1)
   Insert into tblLicense values (30596,  198,1)
   Insert into tblLicense values (28083,  216,0)
    Go


SELECT tblLicense.pk_license,tblLicense.fk_customer,option_1 FROM tblLicense
INNER JOIN 
(
    SELECT fk_customer,MAX(pk_license) as pk_license from tblLicense
    GROUP BY fk_customer
)  groupedTbl
on tblLicense.fk_customer = groupedTbl.fk_customer
AND tblLicense.pk_license = groupedTbl.pk_license
ORDER BY tblLicense.fk_customer

There is no need to doing the joins you could use correlate subquery

select * from table t
where pk_license = 
           (select max(pk_license) from table where fk_customer = t.fk_customer)
order by fk_customer

For your current attempt you are doing group by based on two columns which are ( opt_2 , fk_station ) which could affect your desired result.

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