尝试在SQL-SERVER查询中使用大小写

时间:2017-03-16 14:19:41

标签: sql sql-server

我有一个包含这些列的表enter image description here 我在下面写了两个查询来报告交易发送或待处理的时间 -

SELECT [unityTimeStamp], datediff(hour, unityTimeStamp, getdate())difference_by_hours, datediff(minute, unityTimeStamp, getdate())difference_by_minutes  
FROM [VansoAlertDB].[dbo].[Vanso] WHERE [unityTimeStamp]>dateadd(hour,-10,getdate()) and deliverystatus = 'P'

SELECT [sent_at], datediff(hour, sent_at, getdate())difference_by_hours, datediff(minute, sent_at, getdate())difference_by_minutes  
FROM [VansoAlertDB].[dbo].[Vanso] WHERE [unityTimeStamp]>dateadd(hour,-10,getdate()) and deliverystatus = 'S'

不,我已经尝试编写一个检查的查询,如果所有事务都在一小时或更长时间内完成,那么值1应分配给新列S1hr否则为0.同样,它会检查如果任何交易在30分钟内成功发送,并指定值为1,则为其他0.

对于待处理的交易,它会检查unityTimeStamp列与当前timeStamp的对应关系,如果P1hr> = 1小时,则分配1,否则为0,如果P30min <= 30min,则分配0,否则为0

我试过了 -

SELECT  
case when  max( deliverystatus)='S' and     datediff(hour,getdate(),max(sent_at))>=1 then 1 else 0 end S1hr,
case when max( deliverystatus)='S' and datediff(minute,getdate(),max(sent_at))>=30 then 1 else 0 end S30min, 
case when  max( deliverystatus)='P' and datediff(hour,getdate(),max(unityTimeStamp))>=1 then 1 else 0 end P1hr,
case when  max( deliverystatus)='P' and  datediff(minute,getdate(),max(unityTimeStamp))>=30 then 1 else 0 end P30min
FROM [VansoAlertDB].[dbo].[Vanso] WHERE [unityTimeStamp]>dateadd(hour,-10,getdate())

但我一直在 -

S1hr, S30min, P1hr, P30min

0   , 1     , 0   , 0

如何使用CASE语句获得所需的结果?

已发送查询

Sent/Processed transactions from the query mentioned above

sent_at               destinationAddress    difference_by_hours difference_by_minutes
2017-03-16 08:15:00   08060293904           7   440
2017-03-16 08:15:00   08165777415           7   440
2017-03-16 08:15:00   08035001717           7   440
2017-03-16 08:16:00   08185200110           7   439
2017-03-16 08:15:00   08092717339           7   440
2017-03-16 08:15:00   2347055686321         7   440

待处理查询

Pending transactions from the query mentioned above

unityTimeStamp  destinationAddress  difference_by_hours difference_by_minutes
2017-03-16 09:14:00 08062313735       6                  405
2017-03-16 09:14:00 2348036736566     6                  405
2017-03-16 09:14:00 08022621333       6                  405
2017-03-16 09:14:00 08034859672       6                  405
2017-03-16 09:14:00 2347013038026     6                  405
2017-03-16 09:14:00 2348060472208     6                  405

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您希望从查询中返回一行以显示每个类别中的订单数量?像这样的东西?

select  
sum(case when deliverystatus='S' and datediff(hour,getdate(),sent_at)>=1 then 1 else 0 end) S1hr,
sum(case when deliverystatus='S' and datediff(minute,getdate(),sent_at)>=30 then 1 else 0 end) S30min, 
sum(case when deliverystatus='P' and datediff(hour,unityTimeStamp,getdate())>=1 then 1 else 0 end) P1hr,
sum(case when deliverystatus='P' and datediff(minute,unityTimeStamp,getdate())>=30 then 1 else 0 end) P30min
from [VansoAlertDB].[dbo].[Vanso] where [unityTimeStamp]>dateadd(hour,-10,getdate())