SQL 查询帮助
使用 SQL SERVER - 2000
表结构
CARDEVENTDATE CARDEVENTTIME CARDNO
20090224 92007 485
20090224 92345 321
20090225 163932 168
20090225 164630 471
20090225 165027 488
20090225 165137 247
20090225 165147 519
20090225 165715 518
20090225 165749 331
20090303 162059 240
20090303 162723 518
20090303 155029 386
20090303 155707 441
20090303 162824 331
Cardeventdate
和 Cardeventtime
- nvarchar 数据类型 日期和时间是单独的列
我想获取
昨天 03:00:01 AM 到今天 03:00:00 AM 之间的数据 前天 03:00:01 AM 至 昨天 03:00:00 AM 所以……
我尝试了下面提到的查询,
Select Cardno, cardeventdate, min(cardeventtime), max(cardeventtime)
from table
where cardeventtime between 030001 to 030000
Select Cardno, Cardeventdate, Min(cardeventtime), max(cardeventtime)
from table
where Cardeventtime >030001 and Cardeventtime < 030000
结果中没有显示任何内容,因为它需要今天的时间从凌晨 03.00 到凌晨 03.01
Select Cardno, Cardeventdate, min (cardeventtime), max (cardeventtime)
from table
where cardeventtime < 030000 and cardeventtime > previous day time – query help
正是我需要昨天上午 03.00.01 到今天凌晨 03.00.00 的数据,前天 03.00。上午 01 点到昨天凌晨 03 点 00 分数据………………。 所以
我需要上述条件的sql查询。 谁能帮我?
(编辑:我需要从昨天早上到今天早上的日期,截至凌晨 03 点)
Am Using SQL SERVER - 2000
Table Structure
CARDEVENTDATE CARDEVENTTIME CARDNO
20090224 92007 485
20090224 92345 321
20090225 163932 168
20090225 164630 471
20090225 165027 488
20090225 165137 247
20090225 165147 519
20090225 165715 518
20090225 165749 331
20090303 162059 240
20090303 162723 518
20090303 155029 386
20090303 155707 441
20090303 162824 331
Cardeventdate
and Cardeventtime
- nvarchar data type
Date and Time is separate column
I want to get a data between
Yesterday 03:00:01 AM to today 03:00:00 AM
Day before yesterday 03:00:01 AM to yesterday 03:00:00 AM
So On……..
I tried the below mentioned query’s
Select Cardno, cardeventdate, min(cardeventtime), max(cardeventtime)
from table
where cardeventtime between 030001 to 030000
Select Cardno, Cardeventdate, Min(cardeventtime), max(cardeventtime)
from table
where Cardeventtime >030001 and Cardeventtime < 030000
Nothing displayed in the result because it is taking today time from 03.00 am to 03.01am
Select Cardno, Cardeventdate, min (cardeventtime), max (cardeventtime)
from table
where cardeventtime < 030000 and cardeventtime > previous day time – query help
Exactly I need yesterday 03.00.01 am to today 03.00.00 am data’s, Day before yesterday 03.00.01 am to yesterday 03.00.00 am data’s …………………. So on
I need the sql query for the above condition. Can any one help me?
(edit: I need the date from yesterday morning to this morning, up to 03.00am)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
该脚本是 SQLServer2005,但使用#Temp 表,您应该能够将其转换为可用的 SQLServer2000 脚本。
如果我理解正确的话,以下脚本将按 dd 03:00:00 - dd+1 对记录进行分组
03:00:00
The script is SQLServer2005 but using #Temp tables, you should be able to convert it to a usable SQLServer2000 script.
If I understand you correctly, following script groups the records by dd 03:00:00 - dd+1
03:00:00
第 1 步
首先将包含日期和时间的两列放在
datetime
类型的一列中(即carddate
)。第 2 步
如果您想根据一天中的某些分割时间进行分组,请执行以下操作(示例中的分割时间为凌晨 3 点):
但是您将无法像这样简单地获取组内的卡号。 您必须使用用户功能来实现这种功能
Step 1
First put both columns with date and time together in one column (i.e.
carddate
) of typedatetime
.Step 2
If you want to do grouping based on some split time during the day do something like this (split time in example 3 AM):
But you won't be able to get card numbers within groups as simple as this. You'll have to use user function for that kind of functionality
仅使用查询的解决方案:
参数为:
它的实现方式是根据您的日期和时间组成 DATETIME,但使用 not一个 DATETIME 数据类型,但只是一个 BIGINT,因此 20090224092007 的值将是 20090224 的日期和 092007 的时间。最重要的是它仍然是可排序的,并且只需使用 INTEGER 除法即可轻松切断/转移购买。 这就是我买的东西减去150000(时间15:00:00)。
结果(使用上面查询中的过滤器)是:
下面是没有任何过滤器的结果,以便您稍后可以按 StartDate 进行过滤:
这是带有 GROUP BY、MIN(..) 和 MAX(..) 的版本:
注意有几点:
再次强调:除非您重新提出一个好问题,否则您将不会得到您正在寻找的答案。
Solution using just a query:
The parameters are:
The way it is implemented is by composing a DATETIME from your date and time, but using not a DATETIME data type, but just a BIGINT, so that value of 20090224092007 would be date of 20090224 and time of 092007. Most important thing is that it is still sortable and it is easy to cut off/shift buy just using the INTEGER division. That is what I do buy substracting 150000 (time 15:00:00).
The result (with filter as in query above) is:
The result without any filter is below, so that you can filter by StartDate later:
This is the version with GROUP BY, MIN(..) and MAX(..):
Note few things:
Again: unless you re-ask a GOOD question, you will NOT get an answer you are looking for.
以下是 SQL Server 2005 Express 上的测试。 如果 SQL Server 2000 没有公用表表达式 (CTE),则可以使用视图来生成与“cteTbl”相同的结果。 我确实希望 SQL Server 2000 支持 CASE-WHEN-END。
我在此使用的想法是延长一天的时间,从 24 小时到 27 小时 (+3),但仅当 [CardEventTime] 小于或等于 03:00:00 (24 小时) 时,我将 24 小时添加到 [ CardEventDate] 减去 1d。
The below is testet on SQL Server 2005 Express. If SQL Server 2000 does not have Common Table Expressions (CTE), a view could be used instead to produce the same as 'cteTbl' would. And I do hope that SQL Server 2000 supports the CASE-WHEN-END.
The idea I use in this, is to extend the hours a day have, from 24h to 27h (+3), but only where [CardEventTime] is less or equal to 03:00:00 (24h) I add 24h and to [CardEventDate] subtract 1d.
你不能直接打电话:
couldn't you just call:
查询中的日期格式似乎是特定于实现的。
对 SQLServer2000,您希望查询使用日期作为日期 - 而不是数字。 您确实应该有一个“日期时间”类型的字段,而不是两个单独的字段。 但如果您无法控制这一点,那么您将需要进行一些日期时间添加。 我举个例子。
尝试这样的事情:
Date formatting in a query seems to be implementation specific.
Right SQLServer2000, You want to make your query to use the dates as dates - not as numbers. You really should have a single field that is a 'datetime' type, and not two separate fields. But if you don't have control over this, then you'll need to do some datetime addition. I'll pull up an example.
Try something like this:
这是不正确的,因为条件始终为假:(您将日期存储为整数而不是日期时间,并且整数无法知道它应该在午夜环绕)
查询应该这样写:
一旦将其纳入其他查询中,您应该能够找到解决方案。
编辑:抱歉,查询中的意思是 OR 而不是 AND。
This would be incorrect because the condition will always be false: (you're storing your date as an integer instead of a datetime, and an integer has no way of knowing it should wrap around at midnight)
The query should be written like this instead:
Once you factor this into your other queries, you should be able to come up with a solution.
Edit: sorry, meant OR instead of AND in the query.