如何检查sql中是否有最近2天的数据

时间:2015-03-16 19:46:15

标签: sql sql-server tsql

我想检查用户是否在过去2天内提交了他们的数据。我的表中有2个字段:USERID和DATE_SUBMITTED。我的表中有最近2天的数据,但我的查询没有返回任何数据。我在这做错了什么:

select distinct [UserID], DT_Submitted 
from myTable 
where [UserID] = 'Joe'
  and DT_Submitted = (select CAST(FLOOR( CAST( GETDATE()-1 AS FLOAT ) )AS DATETIME))
  and DT_Submitted = (select CAST(FLOOR( CAST( GETDATE()-2 AS FLOAT ) )AS DATETIME))

感谢

3 个答案:

答案 0 :(得分:1)

两个EXISTS子查询应足以检查两天是否有行:

DECLARE @StartDate DATETIME, @EndDate DATETIME
SET     @StartDate = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -1) -- It truncates TIME 
SET     @EndDate = DATEADD(DAY, 1, @StartDate)

IF EXISTS 
    (select * from  myTable 
    where [UserID] = 'Joe'
    and DT_Submitted >= @StartDate AND DT_Submitted < @EndDate)
AND EXISTS 
    (select * from  myTable 
    where [UserID] = 'Joe'
    and DT_Submitted >= dateadd(day, -1, @StartDate) AND DT_Submitted < dateadd(day, -1, @EndDate))
BEGIN 
    ...
END

SELECT ... 
FROM dbo.Users u
WHERE EXISTS 
    (select * from  myTable x
    where x.[UserID] = u.UserID
    and DT_Submitted >= @StartDate AND DT_Submitted < @EndDate)
AND EXISTS 
    (select * from  myTable x
    where x.[UserID] = u.UserID
    and DT_Submitted >= dateadd(day, -1, @StartDate) AND DT_Submitted < dateadd(day, -1, @EndDate))
WHERE u.UserName = 'Joe'

答案 1 :(得分:1)

以前的人已经回答了,但我相信他们工作太辛苦了,可能会使用更多的资源加上多个联接和分配等等......尝试一下。

SELECT  UserID,
        DT_Submitted INTO myTable
FROM
(
    --Submitted both days
    SELECT 'Joe' UserID,DATEADD(DAY,-1,GETDATE()) DT_Submitted
    UNION ALL
    SELECT 'Joe' UserID,DATEADD(DAY,-2,GETDATE()) DT_Submitted
    UNION ALL
    --Submitted one day
    SELECT 'Bob',DATEADD(DAY,-1,GETDATE()) DT_Submitted
    UNION ALL
    --Submitted 5 days ago
    SELECT 'Helen',DATEADD(DAY,-5,GETDATE()) DT_Submitted
) A

SELECT *
FROM
(
    SELECT  UserID,
            --All this does is count the submissions in the past two days and then adds them up
            SUM(CASE
                    WHEN CAST(DT_submitted AS DATE) =  DATEADD(DAY,-1,CAST(GETDATE() AS DATE)) THEN 1
                    WHEN CAST(DT_submitted AS DATE) =  DATEADD(DAY,-2,CAST(GETDATE() AS DATE)) THEN 1
                    ELSE 0          
                END) AS submissions 
    FROM myTable
    GROUP BY UserID
) A
WHERE submissions = 2

<强>结果:

UserID submissions
------ -----------
Joe    2

答案 2 :(得分:0)

您想查找昨天和前天提交的USERID提交的记录。你需要这样的东西:

SELECT
    userID,
    Dt_Submitted
FROM
    myTable
WHERE
    userid = 'Joe' AND
    userid IN (SELECT distinct userid FROM myTable Where Dt_Submitted = dateadd(day, -2, getdate())) AND
    userid IN (SELECT distinct userid FROM myTable Where Dt_Submitted = dateadd(day, -1, getdate()))

如果您使用Window函数有更新版本的SQL Server:

SELECT
    *
FROM
    (
        SELECT
            userID,
            Dt_Submitted, 
            CASE 
                WHEN
                    MAX(Dt_Submitted) OVER (PARTITION BY userid ORDER BY dt_submitted ROWS UNBOUNDED PRECEDING) = dateAdd(day, -1, dt_submitted) ORDER
                    MIN(Dt_Submitted) OVER (PARTITION BY userid ORDER BY dt_submitted ROWS UNBOUNDED FOLLOWING) = dateAdd(day, +1, dt_submitted)
                THEN 'X' as keeptest

        FROM
            myTable
        WHERE
            userid = 'Joe' AND
    )sub
WHERE keeptest = 'X'

或者使用JOIN删除记录(非常类似于第一个):

SELECT
    t1.userID,
    t1.Dt_Submitted
FROM
    myTable t1
    INNER JOIN (SELECT distinct userid, dt_submitted FROM myTable Where Dt_Submitted = dateadd(day, -2, getdate())) t2 ON
        t1.userid = t2.userid
        AND t1.dt_submitted = t2.dt_submitted
    INNER JOIN (SELECT distinct userid,dt_submitted FROM myTable Where Dt_Submitted = dateadd(day, -1, getdate())) t3 ON
        t1.userid = t3.userid
        AND t1.dt_submitted = t3.dt_submitted
WHERE
    t1.userid = 'Joe'