简体   繁体   English

SQL:GROUP BY id 具有最大日期

[英]SQL: GROUP BY id having max date

I have two tables:我有两个表:

Table 1:
worker_number, first_name , last_name , clinic , doctor_type

1   BB BB  Z2 ENT
10  CC CC  Z3 ENT
4   DD DD Z4    Orthopedist
5   EE EE Z5Surgeon
8   AA AA Z1 ENT


Table 2:
worker_number, first_name , last_name , clinic , doctor_type, dt_date, patient_id

'1', 'BB', 'BB', 'Z2', 'ENT', '2017-02-01 10:00:00', '1'
'1', 'BB', 'BB', 'Z2', 'ENT', '2017-02-27 15:20:00', '1'
'8', 'AA', 'AA', 'Z1', 'ENT', '2017-02-28 08:40:00', '1'

Basically i want to get all rows from table 1 that doctor_type = "A" ordered by the dt_date from table 2 which has same worker_number and doctor_type基本上我想从表1中得到的所有行是doctor_type = "A"所订购的dt_date从具有相同的表2 worker_numberdoctor_type

i tried to do somthing like:我试图做类似的事情:

SELECT `table 1`.worker_number, `table 1`.first_name, `table 1`.last_name, `table 1`.clinic
FROM `health`.`table 1`
LEFT JOIN `health`.`table 2`
ON `health`.`table 1`.worker_number = `health`.`table 2`.worker_number
order by `health`.`table 2`.dt_date desc

But i got the following output:但我得到以下输出:

8   AA  AA  Z1
1   BB  BB  Z2
10  CC  CC  Z3
1   BB  BB  Z2
3   DD  DD  Z4
5   EE  EE  Z5

It works really good except for the BB is showing twice, How can i do group by for the maximum date?除了 BB 显示两次之外,它的效果非常好,我如何为最大日期分组?

excpected result:预期结果:

8   AA  AA  Z1
1   BB  BB  Z2
10  CC  CC  Z3
3   DD  DD  Z4
5   EE  EE  Z5

This adds an extra column to your output.这会为您的输出添加一个额外的列。 If you don't want that, you can wrap this query with another select query to select just the fields you want.如果您不想这样,您可以使用另一个选择查询包装此查询以仅选择您想要的字段。

SELECT `table 1`.worker_number, `table 1`.first_name, `table 1`.last_name, `table 1`.clinic, max(`table 2`.dt_date) max_dt_date
FROM `health`.`table 1`
LEFT JOIN `health`.`table 2`
       ON `health`.`table 1`.worker_number = `health`.`table 2`.worker_number
group by `table 1`.worker_number, `table 1`.first_name, `table 1`.last_name, `table 1`.clinic
order by max_dt_date desc

You can aggregate on the worker_number and sort on MAX (or MIN based on your needs) of the dt_date :您可以在worker_number上聚合worker_number MAX (或根据您的需要, MINdt_date

Try this:尝试这个:

SELECT 
    t1.worker_number, t1.first_name, t1.last_name, t1.clinic
FROM
    `health`.`table 1` t1
        LEFT JOIN
    `health`.`table 2` t2 ON t1.worker_number = t2.worker_number
GROUP BY t1.worker_number
ORDER BY MAX(t2.dt_date) DESC

do the fatc you are not using aggregation function you shoudl need the group by clause but you should use only DISTINCT做你没有使用聚合函数的 fatc 你应该需要 group by 子句但你应该只使用 DISTINCT

SELECT DISTINCT 
      `table 1`.worker_number
    , `table 1`.first_name
    , `table 1`.last_name
    , `table 1`.clinic
FROM `health`.`table 1`
LEFT JOIN `health`.`table 2`
ON `health`.`table 1`.worker_number = `health`.`table 2`.worker_number
order by `health`.`table 2`.dt_date desc
Select worker_number, first_name , last_name , clinic , doctor_type
from table1 t1
where doctor_type = "A"
Order By (Select dt_date from table2
          where worker_number = t1.worker_number 
             and doctor_type = t1.doctor_type)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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