从SQL查询获取MAX日期

时间:2017-02-22 16:26:10

标签: sql sql-server max

查询如下所示,但似乎没有正常工作,只需要最近开始日期的行收费:

{{1}}

Result

我的查询没有选择最新日期,我不确定为什么?

由于

5 个答案:

答案 0 :(得分:0)

我建议您删除该组,然后更改为全选(*)。结果中的最新日期是?我怀疑它不是。

答案 1 :(得分:0)

将您的查询分为两部分。有一个选择拉出'键'只有最大日期,然后加入一个类似的查询加入密钥加上最大日期。 例如:

select B.* from 
  (select PLACE-REF, TENANCY-REF, ACCOUNT-CODE, MAX (START-DATE) AS "START-DATE" from tables...) A 
inner join 
  (select PLACE-REF, TENANCY-REF, ACCOUNT-CODE, START-DATE, NET-AMT AS [PREVIOUS-MEALS-NET-AMOUNT] from tables...) B  
 on A.PLACE-REF = B.PLACE-REF and A.TENANCY-REF = B.TENANCY-REF and A.ACCOUNT-CODE = B.ACCOUNT-CODE and A.START-DATE = B.START-DATE

答案 2 :(得分:0)

现在您的问题并不是很清楚,您正在尝试检索哪些数据。但是,一般来说row_number函数应该可以帮助你根据分组和排名获得一行(还有一个排名函数,但现在让我们用row_number运行)。接受你的询问,这样的事情可能就是你所追求的。

SELECT * FROM
(SELECT 
  Loc.[PLACE-REF],
  Tenant.[TENANCY-REF],
  PChar.[ACCOUNT-CODE],
  PChar.[START-DATE],
  ROW_NUMBER() OVER (PARTITION BY Loc.[PLACE-REF],
      Tenant.[TENANCY-REF],
      PChar.[ACCOUNT-CODE]
    ORDER BY PChar.[START-DATE] DESC) AS rownum,
  PChar.[NET-AMT] AS [PREVIOUS-MEALS-NET-AMOUNT]
FROM
dbo.[RE-TENANCY] AS TenanT
LEFT OUTER JOIN
dbo.[RE-TNCY-PLACE] AS TncyPlace ON TncyPlace.[TNCY-SYS-REF] = TenanT.[TNCY-SYS-REF]
LEFT OUTER JOIN
dbo.[IH-LOCATION] AS Loc ON Loc.[PLACE-REF] = TncyPlace.[PLACE-REF]
LEFT OUTER JOIN
dbo.[IH-PLACE-CHG] AS PChar ON Pchar.[PLACE-REF] = Loc.[PLACE-REF]
WHERE
(
    (Pchar.[START-DATE] = '04/01/2016') 
    OR 
    (Pchar.[START-DATE] BETWEEN '11/01/2016' AND '03/31/2017')
)
AND Tenant.[TNCY-STATUS] = 'CUR'        
AND PChar.[ACCOUNT-CODE] = 'MEALS'  
AND Loc.[PLACE-REF] = '166CHA010') A
WHERE A.rownum = 1
ORDER BY A.[PLACE-REF]

从内存中写出来,因此它可能无法正常工作,但它应该是接近的,而且似乎是您正在寻找的所需数据。

答案 3 :(得分:0)

如果你只想要顶行...

public String getPath(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    String document_id = cursor.getString(0);
    document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
    cursor.close();

    cursor = getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
    cursor.moveToFirst();
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
    cursor.close();

    return path;
}

答案 4 :(得分:0)

克里斯弗林的解决方案很有效。谢谢大家。

SELECT * FROM
(SELECT 
  Loc.[PLACE-REF],
  Tenant.[TENANCY-REF],
  PChar.[ACCOUNT-CODE],
  PChar.[START-DATE],
  ROW_NUMBER() OVER (PARTITION BY Loc.[PLACE-REF],
      Tenant.[TENANCY-REF],
      PChar.[ACCOUNT-CODE]
    ORDER BY PChar.[START-DATE] DESC) AS rownum,
  PChar.[NET-AMT] AS [PREVIOUS-MEALS-NET-AMOUNT]
FROM
dbo.[RE-TENANCY] AS TenanT
LEFT OUTER JOIN
dbo.[RE-TNCY-PLACE] AS TncyPlace ON TncyPlace.[TNCY-SYS-REF] = TenanT.[TNCY-SYS-REF]
LEFT OUTER JOIN
dbo.[IH-LOCATION] AS Loc ON Loc.[PLACE-REF] = TncyPlace.[PLACE-REF]
LEFT OUTER JOIN
dbo.[IH-PLACE-CHG] AS PChar ON Pchar.[PLACE-REF] = Loc.[PLACE-REF]
WHERE
(
    (Pchar.[START-DATE] = '04/01/2016') 
    OR 
    (Pchar.[START-DATE] BETWEEN '11/01/2016' AND '03/31/2017')
)
AND Tenant.[TNCY-STATUS] = 'CUR'        
AND PChar.[ACCOUNT-CODE] = 'MEALS'  
`enter code here`AND Loc.[PLACE-REF] = '166CHA010') A
WHERE A.rownum = 1
ORDER BY A.[PLACE-REF]