English 中文(简体)
如何同时按2个领域和顺序排列?
原标题:How to group by 2 fields and order by date at the same time?

www.un.org/spanish/ga/president

我需要找到所有各行,每个行各有一行<条码>。 (在我可以使用一个小组的地方),但鉴于可能有许多条目涉及相同的<条码>、个人_id + personal_id_type(因为雇员可以辞职并在以后再次受雇),我只需要打上最新的条目(<条码>,按日期顺序——注册的DESC)。

这在概念上是否正确? 由2个领域分类的星号是什么? 我能否做到这一点?

Edit: I m sorry, 实际有许多领域,因此,我需要最新的增长,而不仅仅是新日期。

最佳回答

在每个<代码>(人/人/人/人/人/人/人/人/人/人/人/人/人/人/人/人/人/人/人/人/人/人/人/人/人/人

SELECT
    e.*
FROM
    employees e
  JOIN   
    ( SELECT
          personal_id
        , personal_id_type
        , MAX(date_registered) AS latest_date_registered
      FROM
          employees
      GROUP BY
          personal_id
        , personal_id_type
    ) AS grp
    ON  grp.personal_id = e.personal_id
    AND grp.personal_id_type = e.personal_id_type
    AND grp.latest_date_registered = e.date_registered
问题回答

你们想要的是:

select personal_id, personal_id_type, max(date_registered)
from employees 
group by personal_id, personal_id_type

从概念上讲,你并没有按日期顺序排列,因为你只回到每人的面值/个人面值。 由于你使用小组,你利用一个总的功能选择“公平日期”。

SELECT
    personal_id,
    personal_id_type,
    MAX(date_registered) AS latest_date_registered
FROM
    Employees E
GROUP BY
    personal_id,
    personal_id_type

如果你需要增加一栏,你就不必在提问中增加细节。

SELECT personal_id, personal_id_type, MAX(date_registered)
FROM employees
GROUP BY personal_id, personal_id_type
ORDER BY MAX(date_registered) DESC

This is where aggregate functions become useful. You will group by personal_id and personal_id_type, then use MAX(date_registered) to select the newest date. Just remember that you have to group by all non-aggregate columns.

是的,这是正确的:

SELECT 
    personal_id, 
    personal_id_type, 
    MAX(date_registered) AS latest_date
FROM 
    employees 
GROUP BY 
    personal_id,
    personal_id_type
ORDER BY
    MAX(date_registered) DESC




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签