带会话的SQL Server案例

时间:2018-06-25 09:06:37

标签: php sql sql-server-2008-r2

我遇到一个问题,我试图根据大于24小时,大于18小时或小于18小时的时间范围显示结果,到目前为止,我的查询写为

SELECT MAX(CASE WHEN DATEDIFF(HH,DATEADD(SECOND, inc.ORIGINATION_DATE, '19700101'),DATEADD(hour,6,GETDATE())) > 24 then 2 
WHEN DATEDIFF(HH,DATEADD(SECOND, inc.ORIGINATION_DATE, '19700101'),DATEADD(hour,6,GETDATE())) > 18 then 1 
Else 0 End) as DIFFERENCE
   FROM dbo.HELP_DESK as inc
   WHERE inc.LOGIN_ID in "some user"
     and NOT EXISTS (SELECT work.Description from dbo.WORKLOG as work WHERE 
     work.INCIDENT_NUMBER = inc.INCIDENT_NUMBER 
     and work.WORK_LOG_TYPE =  '16000' 
     and work.WORK_LOG_SUBMITTER in "User Group")
UNION ALL
SELECT MAX(CASE WHEN DATEDIFF(HH,DATEADD(SECOND, chg.ORIGINATION_DATE, '19700101'),DATEADD(hour,6,GETDATE())) > 24 then 2 
WHEN DATEDIFF(HH,DATEADD(SECOND, chg.ORIGINATION_DATE, '19700101'),DATEADD(hour,6,GETDATE())) > 18 then 1 
Else 0 End) as DIFFERENCE
   FROM dbo.INFRASTRUCTURE_CHANGE as chg
   WHERE chg.ASLOGID in "some user"
     and NOT EXISTS (SELECT work.Description from dbo.WORKLOG as work WHERE 
     work.CHANGE_ID = chg.CHANGE_ID  
     and work.WORK_LOG_TYPE =  '31000' 
     and work.WORK_LOG_SUBMITTER in "User Group")

在此示例中,我的输出是:

+------------+
| DIFFERENCE |
+------------+
|          2 |
|          2 |
+------------+

我有多个值> 24,因此显然是由于并集Im将正确显示两个单独的值显示为2。但是,我想要对它们进行分组,所以我可以将输出2、1或0设为一个然后输出到PHP会话。

除非我错误地进行分组,否则

某种形式的计数或分组似乎都失败了。也许还有更好的方法,但是我似乎要走很长的路。

感谢任何指导我前进的指针。

1 个答案:

答案 0 :(得分:0)

从UNION ALL到UNION的简单更改返回了我要输出的会话值的唯一结果