查询最新日期

时间:2020-03-03 17:05:22

标签: sql

我有以下查询:

SELECT 

 a.JobStatusID
,a.JobID    
,a.JobNavn
,b.AdjustmentDate
,b.ExpTypeDescr 
,b.Invoiced
,b.ActualPL 

from Job a
          INNER JOIN JobAdjustmentDetails b on b.JobId = a.jobid
          INNER JOIN SysJobtype c on c.JobTypeID = a.JobTypeID

WHERE a.JobID BETWEEN '700000' AND '799999' 

GROUP BY
   a.JobID,
ROLLUP (
     a.JobStatusID
,a.JobNavn
,b.AdjustmentDate
,b.ExpTypeDescr 
,b.Invoiced 
,b.ActualPL
)

这将返回如下数据:

enter image description here

任何人都可以建议最好的方法来延长我的专栏AdjustmentDate的最新日期吗?

例如,我想要以下输出:

enter image description here

完整的查询将检索日期不同的多个作业,因此我只想查看每个Jobid的最长日期。

4 个答案:

答案 0 :(得分:1)

这并不漂亮,还有更好的方法,但是我想这应该可行并且可以理解:

SELECT 

 a.JobStatusID
,a.JobID    
,a.JobNavn
,b.AdjustmentDate
,b.ExpTypeDescr 
,b.Invoiced
,b.ActualPL 

from Job a
          INNER JOIN JobAdjustmentDetails b on b.JobId = a.jobid
          INNER JOIN SysJobtype c on c.JobTypeID = a.JobTypeID

WHERE a.JobID BETWEEN '700000' AND '799999' 
and b.AdjustmentDate = (select max(b.AdjustmentDate)
from Job a
          INNER JOIN JobAdjustmentDetails b on b.JobId = a.jobid
          INNER JOIN SysJobtype c on c.JobTypeID = a.JobTypeID

WHERE a.JobID BETWEEN '700000' AND '799999' )

GROUP BY
   a.JobID,
ROLLUP (
     a.JobStatusID
,a.JobNavn
,b.AdjustmentDate
,b.ExpTypeDescr 
,b.Invoiced 
,b.ActualPL
)

它再次执行相同的查询,但只选择最大日期。

编辑:

每个工作ID,我猜我会做CTE。你能检查一下是否可行吗?

/* Formatted on 3-3-2020 19:05:25 (QP5 v5.300) */
with cte as (SELECT MAX (b.AdjustmentDate) AdjustmentDate, a.JobID
                    FROM Job a
                         INNER JOIN JobAdjustmentDetails b ON b.JobId = a.jobid
                         INNER JOIN SysJobtype c ON c.JobTypeID = a.JobTypeID
                   WHERE a.JobID BETWEEN '700000' AND '799999'
                   group by a.JobID
                   )
  SELECT a.JobStatusID,
         a.JobID,
         a.JobNavn,
         b.AdjustmentDate,
         b.ExpTypeDescr,
         b.Invoiced,
         b.ActualPL
    FROM Job a
         INNER JOIN JobAdjustmentDetails b ON b.JobId = a.jobid
         INNER JOIN SysJobtype c ON c.JobTypeID = a.JobTypeID
         INNER JOIN CTE ON CTE.JobID = a.JobID and b.AdjustmentDate = CTE.AdjustmentDate
   WHERE     a.JobID BETWEEN '700000' AND '799999'
GROUP BY a.JobID,
         ROLLUP (a.JobStatusID,
                 a.JobNavn,
                 b.AdjustmentDate,
                 b.ExpTypeDescr,
                 b.Invoiced,
                 b.ActualPL)

给出以下结果。绿色突出显示的行是正确的信息,但是每个ExpTypeDescr我又收到2行吗?

Results added by PipRon7

答案 1 :(得分:0)

让我们尝试在日期和必填字段中使用汇总函数MAX。

SELECT 

     a.JobStatusID
    ,a.JobID    
    ,a.JobNavn
    ,MAX(b.AdjustmentDate)
    ,b.ExpTypeDescr 
    ,b.Invoiced
    ,b.ActualPL 

FROM Job a
    INNER JOIN JobAdjustmentDetails b on b.JobId = a.jobid
    INNER JOIN SysJobtype c on c.JobTypeID = a.JobTypeID

WHERE a.JobID BETWEEN '700000' AND '799999' 

GROUP BY
    a.JobID
    ,a.JobStatusID,
    ,a.JobNavn
    ,b.ExpTypeDescr 

答案 2 :(得分:-1)

select * from tablename
WHERE AdjustmentDate=CURRENT_DATE() ;

答案 3 :(得分:-1)

如果将其与当前日期进行比较,而不是使用MAX(如下所示)会更好:

compass = (CompassLayer) worldWindow.getModel().getLayers().getLayerByName("Compass");

// this works ...           
// compass.setPosition(AVKey.SOUTHEAST);
// compass.setLocationOffset(new Vec4(0, 20));

// this does not work ...  
compass.setLocationCenter(worldWindow.getView().getCenterPoint());

// this part works fine
String compassPath = "images" + File.separator + "CompassRoseWhite.png";
String compassImg  = new ClassPathResource(compassPath).getURL().toString();
compass.setIconFilePath(compassImg);
compass.setEnabled(true);