SQL查询查找,天气输入的记录是否是当天的第一条记录 [英] Sql Query to find, weather the record entered is the first record or not on that day

查看:105
本文介绍了SQL查询查找,天气输入的记录是否是当天的第一条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个表,其中包含UserID,日历日期和taskName作为列.请让我知道,从sql查询天气中,我们可以发现,对于用户ID,它是否是当天在表中输入的第一条记录.

Suppose we have a table with UserID ,Calendar date and taskName as columns. Please let me know that from sql Query weather we can find that for a user id is it the first record entered in the table on that day or not.

推荐答案

尝试这个:
Try this:
DECLARE @TODAY DATE
SET @TODAY = CONVERT(VARCHAR(10), GETDATE(), 111)
SELECT COUNT(*) FROM myTable WHERE Calendar >= @TODAY

如果为零,则从午夜开始没有任何条目.

[edit]更改了表名和列名-OriginalGriff [/edit]

If it is zero, then there are no entries since midnight.

[edit]Changed table and column names - OriginalGriff[/edit]


不确定该问题,但是如果您想为单个日期的用户查找第一条记录,那么也许类似于以下内容.它应该为今天的给定用户获取第一条记录
Not sure about the question, but if you want to find the first record for a user fo a single date, then perhaps something like the following. It should fetch the first record for given user for today
SELECT *
FROM TableName t1
WHERE UserId = <userid_goes_here>
AND   CalendarDate >= CAST( GETDATE() AS DATE)
AND   NOT EXISTS (SELECT 1
                  FROM TableName t2
                  WHERE t2.UserId = t1.UserId
                  AND   t2.CalendarDate < t1.CalendarDate
                  AND   t2.CalendarDate >= CAST( GETDATE() AS DATE))</userid_goes_here>


您可以将GETDATE替换为正确的日期.


You can replace the GETDATE with a proper date.


这篇关于SQL查询查找,天气输入的记录是否是当天的第一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆