mysql> select * from FinalTable;
+------+-------+-------+---------------------+
| id | name | state | timestamp |
+------+-------+-------+---------------------+
| 12 | name1 | TX | 2020-01-25 11:29:36 |
| 14 | name3 | CA | 2020-01-25 11:29:36 |
| 14 | name3 | TX | 2020-01-25 11:29:36 |
| 12 | name1 | CA | 2020-01-25 11:29:36 |
| 13 | name2 | TA | 2020-01-25 11:29:36 |
| 14 | name3 | CA | 2020-01-25 11:29:36 |
+------+-------+-------+---------------------+
我正在查看给出响应的输出查询:
I am looking at output query which gives response as:
I2 name1 TX 2020-01-25 11:29:36 CA 2020-01-25 11:29:36
当我运行查询时,
select id,name,state,timestamp,
lead(state,1) over (partition by id order by timestamp asc) out_state,
lead(timestamp,1) over (partition by id order by timestamp asc) out_timestamp
from FinalTable
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(partition by id order by timestamp asc) out_state,
lead(timestamp,1) over (part' at line 2
还可以在数据库中创建高达毫秒而不是秒的时间戳吗?我正在使用 CURRENT_TIMESTAMP.
also is it possible to create timetamp upto milliseconds instead of seconds in DB? I am using CURRENT_TIMESTAMP.
窗口函数(例如 lead())仅在 MySQL 8.0 中添加,因此它们在 5.7 版中不可用.您可以像这样使用自连接来模拟 lead():
Window functions (such as lead()) were added in MySQL 8.0 only, so they are not available in version 5.7. You can emulate lead() with a self-join like so:
select t.*, tlead.state, tlead.timestamp
from FinalTable t
left join FinalTable tlead
on tlead .id = t.id
and tlead.timestamp = (
select min(t1.timestamp)
from FinalTable t1
where t1.id = t.id and t1.timestamp > t.timestamp
)
旁注:为了使该方法正常工作,您需要相同id的后续记录具有不同的timestamp - 这在示例数据中并非如此您展示的所有时间戳都相同(我认为这是您的示例数据中的拼写错误).
Side note: for this method to work properly, you need subsequent records of the same id to have different timestamps - which is not the case in the sample data that you showed, where all timestamps are the same (I assume this is a typo in your sample data).
这篇关于ERROR 1064 (42000) 过度分区语法中的数据库错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
我应该使用什么 SQL Server 数据类型来存储字节 What SQL Server Datatype Should I Use To Store A Byte[](我应该使用什么 SQL Server 数据类型来存储字节 [])
解释 SQL Server 中 sys.objects 中的类型代码Interpreting type codes in sys.objects in SQL Server(解释 SQL Server 中 sys.objects 中的类型代码)
Typeorm 不返回所有数据Typeorm Does not return all data(Typeorm 不返回所有数据)
Typeorm .loadRelationCountAndMap 返回零Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
如何将“2016-07-01 01:12:22 PM"转换为“2016-07-0How to convert #39;2016-07-01 01:12:22 PM#39; to #39;2016-07-01 13:12:22#39; hour format?(如何将“2016-07-01 01:12:22 PM转换为“2016-07-01 13:1
MS SQL:ISDATE() 是否应该返回“1"?什么时候不能MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否应该返回“1?什么时候不能投射为日期?)