有关日期时间类型的SQL查询帮助

时间:2018-07-23 04:34:37

标签: java sql

我想写一个时间检查。也就是说1个客户的存款不能超过1天的5次。我只是写了这个来检查:

Select bookName From Books
       Where bookName= "$values"

要计算客户并检查> 5是否返回false。但是我认为这是错误的。有人帮我查询小时和日期(列日期为SELECT CUSTOMERID FROM TRANSACTIONS WHERE (SELECT DATEPART(HOUR,GETDATE())) BETWEEN ((SELECT DATEPART(HOUR,GETDATE()))-24) AND (SELECT DATEPART(HOUR,GETDATE())) AND METHODID = 1 AND CUSTOMERID = 8 类型)

Here the image of my table.

2 个答案:

答案 0 :(得分:1)

据我了解,您拥有一个客户ID,并希望检查该客户一天的交易次数,如果计数少于5,则允许该交易。请尝试以下查询。(查询将返回“如果交易<5,则为true,否则为” false”。

select case when count(*) < 5 then 'True' else 'False' end  from [YOUR_TABLE_NAME] 
where CUSTOMERID = 8
and cast([YOUR_DATE_COLUMN_NAME] as date)=cast(GETDATE() as date)

答案 1 :(得分:0)

您需要聚合函数countgroup by子句和case when进行检查

如果您的dbms是Mysql,则下面的查询将为您提供帮助

select customerid,date(date) as date_of_month,
  case when count(transactionid)>5 then 'false' else 'True'
  from TRANSACTIONS        
  group by customerid,date(date)

如果您的dbms是mssql服务器

select customerid,convert(date,[date]) as date_of_month,  
case when count(transactionid)>5 then 'false' else 'True' 
from   TRANSACTIONS            
group by customerid,convert(date,[date])