访问SQL使用最新日期对数据进行排序

时间:2014-07-22 09:52:57

标签: sql ms-access inner-join

我有这个问题:

SELECT  distinct  a.Project_Manager, a.Project_ID, a.Topic,  a.Stage,
a.Presentation_Date, a.Presentation_Notes,  a.Presentation_Status,  a.Category,
a.Postponed_Reason,  a.Postponed_Manager
FROM GSC_Presentation_Info  a
inner join   (
           SELECT Project_ID as Project_ID, MAX(Presentation_Date) as
           Max_Presentation_Date
           from GSC_Presentation_Info 
           group by  Project_ID   
              )  b
ON   a.Project_ID = b.Project_ID
and  a.Presentation_Date = b.Max_Presentation_Date

我真的想要隐藏与最近的演示日期无关的记录。但Access不断向我显示由查询定义的Project_ID列表中的别名SELECT引起的循环引用。我真的不知道如何解决这个问题。

试过这段代码:

SELECT Max(GSC_Presentation_Info.Presentation_Date) AS MaxOfPresentation_Date1, GSC_Presentation_Info.Project_ID AS Project_ID
FROM GSC_Presentation_Info
GROUP BY GSC_Presentation_Info.Project_ID;

适合我,但我也需要其他专栏。但是一旦我在组功能中添加它们,它们就会立即显示副本。

还尝试使用此代码,但它也显示重复:

SELECT * 
    FROM GSC_Presentation_Info GPI,  
     (SELECT Max(GSC_Presentation_Info.Presentation_Date) AS MaxOfPresentation_Date, GSC_Presentation_Info.Project_ID
     FROM GSC_Presentation_Info
     GROUP BY GSC_Presentation_Info.Project_ID) MVV
    WHERE  GPI.Presentation_Date = MVV.Presentation_Date 
    AND GPI.Project_ID = MVV.Project_ID;

3 个答案:

答案 0 :(得分:1)

您只需删除Project_ID列的别名 - Access不允许您使用已存在的别名作为列名或其他别名。

...inner join   (
           SELECT Project_ID, MAX(Presentation_Date) as
           Max_Presentation_Date
           from GSC_Presentation_Info 
           group by  Project_ID   
           )  b
...

答案 1 :(得分:0)

试试这个

SELECT Project_Manager, Project_ID, Topic, Stage, Presentation_Date, Presentation_Notes, Presentation_Status, Category, Postponed_Reason, Postponed_Manager FROM GSC_Presentation_Info
group by Project_ID order by Presentation_Date desc limit 5

这将产生最新的5个日期结果。如果你想要的不仅仅是增加限制。

答案 2 :(得分:0)

我认为这也适用于RANK()函数。 我正在使用Teradata SQL。 synatx对我们来说可能略有不同。

我创建的表格:

CREATE TABLE stack_test 
(
 Project_ID VARCHAR(3) CHARACTER SET LATIN CASESPECIFIC,
 Project_Manager VARCHAR(20) CHARACTER SET LATIN CASESPECIFIC,
 Presentation_Date DATE FORMAT 'yyyy-mm-dd'
)
PRIMARY KEY stack_test_pk ( Project_ID ,Presentation_Date );

我插入的记录:

INSERT INTO stack_test ('123','Adam','2014-05-01');
INSERT INTO stack_test ('123','Adam','2014-05-02');
INSERT INTO stack_test ('123','Adam','2014-05-03');
INSERT INTO stack_test ('234','Leveen','2014-05-03');
INSERT INTO stack_test ('345','Sang','2014-03-01');
INSERT INTO stack_test ('345','Sang','2014-03-02');
INSERT INTO stack_test ('678','Liam','2014-05-19');

使用的SELECT语句:

SELECT 
Project_Manager, 
Project_ID,
Presentation_Date,
RANK() OVER (PARTITION BY Project_ID ORDER BY Presentation_Date DESC) presen_rank
FROM stack_test 
QUALIFY presen_rank = 1;

我得到的结果:

Project_Manager       Project_ID  Presentation_Date  presen_rank
--------------------  ----------  -----------------  -----------
Adam                  123                2014-05-03            1
Leveen                234                2014-05-03            1
Sang                  345                2014-03-02            1
Liam                  678                2014-05-19            1

希望这也适合你。

因为RANK()不适合你。请尝试以下方法:

SELECT y.Project_Manager, y.Project_ID,y.Presentation_Date
FROM (
        SELECT
            Project_ID,
            MAX(Presentation_Date) AS Presentation_Date
        FROM stack_test 
        GROUP BY Project_ID
     ) x
INNER JOIN stack_test y ON (y.Project_ID = x.Project_ID AND y.Presentation_Date =  x.Presentation_Date)
GROUP BY y.Project_Manager, y.Project_ID,y.Presentation_Date;

这给出了如下结果。

Project_Manager       Project_ID  Presentation_Date
--------------------  ----------  -----------------
Liam                  678                2014-05-19
Sang                  345                2014-03-02
Adam                  123                2014-05-03
Leveen                234                2014-05-03