SQL如何选择最近的日期项

发布于 2024-10-01 04:29:05 字数 434 浏览 3 评论 0原文

您好,我有一个包含列的表:

*使用 oracle

ID                  NUMBER
USER_ID            NUMBER
DATE_ADDED          DATE
DATE_VIEWED        DATE
DOCUMENT_ID        VARCHAR2
URL                VARCHAR2
DOCUMENT_TITLE      VARCHAR2
DOCUMENT_DATE        DATE

我想知道如何获取给定用户最近添加的文档。

Select * FROM test_table WHERE user_id = value AND (do something with date_added column)

谢谢

Hello I have a table with columns:

*using oracle

ID                  NUMBER
USER_ID            NUMBER
DATE_ADDED          DATE
DATE_VIEWED        DATE
DOCUMENT_ID        VARCHAR2
URL                VARCHAR2
DOCUMENT_TITLE      VARCHAR2
DOCUMENT_DATE        DATE

I want to know how i can get the most recently added document for a given user.

Select * FROM test_table WHERE user_id = value AND (do something with date_added column)

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

凉月流沐 2024-10-08 04:29:05
Select * 
FROM test_table 
WHERE user_id = value 
AND date_added = (select max(date_added) 
   from test_table 
   where user_id = value)
Select * 
FROM test_table 
WHERE user_id = value 
AND date_added = (select max(date_added) 
   from test_table 
   where user_id = value)
潦草背影 2024-10-08 04:29:05

不确定确切的语法(您使用 varchar2 类型,这意味着不是 SQL Server,因此是 TOP),但您可以对 MySQL 使用 LIMIT 关键字:

Select * FROM test_table WHERE user_id = value
     ORDER BY DATE_ADDED DESC LIMIT 1

或者 Oracle 中的 rownum

 SELECT * FROM
     (Select rownum as rnum, * FROM test_table WHERE user_id = value ORDER BY DATE_ADDED DESC)
 WHERE rnum = 1

如果是 DB2,我不确定它是 TOP、LIMIT 还是 rownum...

Not sure of exact syntax (you use varchar2 type which means not SQL Server hence TOP) but you can use the LIMIT keyword for MySQL:

Select * FROM test_table WHERE user_id = value
     ORDER BY DATE_ADDED DESC LIMIT 1

Or rownum in Oracle

 SELECT * FROM
     (Select rownum as rnum, * FROM test_table WHERE user_id = value ORDER BY DATE_ADDED DESC)
 WHERE rnum = 1

If DB2, I'm not sure whether it's TOP, LIMIT or rownum...

萌逼全场 2024-10-08 04:29:05

使用 SQL Server 尝试:

SELECT TOP 1 *
FROM dbo.youTable
WHERE user_id = 'userid'
ORDER BY date_added desc

With SQL Server try:

SELECT TOP 1 *
FROM dbo.youTable
WHERE user_id = 'userid'
ORDER BY date_added desc
会发光的星星闪亮亮i 2024-10-08 04:29:05

您尚未指定如果同时添加多个文档,查询应返回什么内容,因此此查询假设您希望返回所有文档:

SELECT t.ID,
       t.USER_ID,
       t.DATE_ADDED,
       t.DATE_VIEWED,
       t.DOCUMENT_ID,
       t.URL,
       t.DOCUMENT_TITLE,
       t.DOCUMENT_DATE
FROM (
  SELECT test_table.*,
         RANK()
         OVER (ORDER BY DOCUMENT_DATE DESC) AS the_rank
  FROM   test_table
  WHERE  user_id = value
  )
WHERE the_rank = 1;

此查询只会遍历一次数据。

You haven't specified what the query should return if more than one document is added at the same time, so this query assumes you want all of them returned:

SELECT t.ID,
       t.USER_ID,
       t.DATE_ADDED,
       t.DATE_VIEWED,
       t.DOCUMENT_ID,
       t.URL,
       t.DOCUMENT_TITLE,
       t.DOCUMENT_DATE
FROM (
  SELECT test_table.*,
         RANK()
         OVER (ORDER BY DOCUMENT_DATE DESC) AS the_rank
  FROM   test_table
  WHERE  user_id = value
  )
WHERE the_rank = 1;

This query will only make one pass through the data.

默嘫て 2024-10-08 04:29:05

假设您的 RDBMS 知道窗口函数,并且 CTE 和 USER_ID 是患者的 ID:

WITH TT AS (
    SELECT *, ROW_NUMBER() OVER(PARTITION BY USER_ID ORDER BY DOCUMENT_DATE DESC) AS N
    FROM test_table
)
SELECT *
FROM TT
WHERE N = 1;

我假设您想按 DOCUMENT_DATE 排序,如果需要,您可以轻松更改它。如果您的 RDBMS 不知道窗口函数,则必须执行连接:

SELECT *
FROM test_table T1
INNER JOIN (SELECT USER_ID, MAX(DOCUMENT_DATE) AS maxDate
            FROM test_table
            GROUP BY USER_ID) T2
    ON T1.USER_ID = T2.USER_ID
        AND T1.DOCUMENT_DATE = T2.maxDate;

不过最好告诉我们您的 RDBMS 是什么。此查询为每个患者选择最近的日期,您可以为给定患者添加条件。

Assuming your RDBMS know window functions and CTE and USER_ID is the patient's id:

WITH TT AS (
    SELECT *, ROW_NUMBER() OVER(PARTITION BY USER_ID ORDER BY DOCUMENT_DATE DESC) AS N
    FROM test_table
)
SELECT *
FROM TT
WHERE N = 1;

I assumed you wanted to sort by DOCUMENT_DATE, you can easily change that if wanted. If your RDBMS doesn't know window functions, you'll have to do a join :

SELECT *
FROM test_table T1
INNER JOIN (SELECT USER_ID, MAX(DOCUMENT_DATE) AS maxDate
            FROM test_table
            GROUP BY USER_ID) T2
    ON T1.USER_ID = T2.USER_ID
        AND T1.DOCUMENT_DATE = T2.maxDate;

It would be good to tell us what your RDBMS is though. And this query selects the most recent date for every patient, you can add a condition for a given patient.

暗喜 2024-10-08 04:29:05
Select Top 1* FROM test_table WHERE user_id = value order by Date_Added Desc
Select Top 1* FROM test_table WHERE user_id = value order by Date_Added Desc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文