繁体   English   中英

SQL中的日期时间戳查询

[英]Date time stamp query in SQL

我是一个 SQL 初学者并且有一个查询以查找在 2015 年 5 月预订最后一次销售的人的 ID, occurred_at的列是datetime时间(2015-10-06 17:31:14),我不知道如何从中获得 2015 年 5 月预订的最后一笔销售。

这是我在下面的查询,它说没有作为Sales_Date的列。 请帮忙

SELECT 
    ID, 
    CAST(occurred_at AS time) AS sales_time,
    CONCAT(' ', CAST(occurred_at AS DATE)) AS Sales_Date,
    extract(year from sales_date) AS year, 
    extract(month from sales_date) AS Month  
FROM
    <table name >
WHERE 
    year = 2015 AND month = 05 
ORDER BY 
    sales_time DESC

如果您想获得最后一次销售,为什么不直接使用 MAX(occured_at) 和 year = 2015?

您还需要在where子句中提取年份月份

SELECT ID , CAST(occurred_at AS time ) AS sales_time  ,
       CONCAT ( ' ' , CAST(occurred_at AS DATE) ) AS Sales_Date ,
       extract ( year from sales_date) AS year , 
       extract ( month from sales_date) AS Month  
from <table name >
where extract(year from sales_date) = 2015 and extract(month from sales_date) = 5 
order by sales_time desc 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM