我找不到sql server中case语句出了什么问题。有人可以帮我吗?

问题描述 投票:0回答:2

我想在代码中输入,如果操作员的工作量超过每周一定的分钟数(480,420,540),则停止操作。这使我对第一种情况有强调,我不明白为什么。谢谢!

create table HOURS (
   IDHOUR               nvarchar(2)           not null,
   STARTHOUR            time             not null,
   ENDHOUR              time             not null,
   constraint PK_HOURS primary key (IDHOUR)
)
GO
create table OPERATOR (
   IDOPERATOR          nvarchar(3)           not null,
   TYPE             nvarchar(20)           null,
   constraint TYPE
   check (TYPE in ('assistant1','assistant2','assistant3')),  
   constraint PK_OPERATOR primary key (IDOPERATOR)
)
go

insert into HOURS (idhour,starthour, endhour) values (14,'14:30','15:30');
insert into HOURS (idhour,starthour, endhour) values (1,'09:00','09:45');

insert into OPERATOR (IDOPERATOR , type) values (1,'assistant1');
 insert into OPERATOR (IDOPERATOR , type) values (2,'assistant2');
 insert into OPERATOR (IDOPERATOR , type) values (3,'asssistant3');

select * from OPERATOR 
 case 
    when TYPE = 'assistant1'
    when
             select sum(datediff(minute,starthour,endhour)) as minutes from hours > 480 then 'cease operation'
     when 
        TYPE = 'assistant2'
             select sum(datediff(minute,starthour,endhour))  as minutes from hours> 420 then 'cease operation'
     when
        TYPE = 'asssistant3'
             select sum(datediff(minute,starthour,endhour))  as minutes from hours> 540 then 'cease operation'
else 'without effect'
end
sql sql-server case operator-keyword
2个回答
2
投票

HOURS表似乎需要引用OPERATOR表的外键。

然后您可以使用CASE中的SELECT

例如。

样本数据:

create table OPERATOR (
   IDOPERATOR int not null,
   [TYPE]     nvarchar(20) not null,
   constraint [TYPE]
     check ([TYPE] in ('assistant1','assistant2','assistant3')),  
   constraint PK_OPERATOR 
     primary key (IDOPERATOR)
);

create table HOURS (
   IDHOUR     int identity(1,1) not null,
   IDOPERATOR int not null,
   STARTHOUR  time not null,
   ENDHOUR    time not null,
   constraint PK_HOURS 
     primary key (IDHOUR),
   constraint FK_HOURS_OPERATOR
     foreign key (IDOPERATOR) references OPERATOR(IDOPERATOR)
);

insert into OPERATOR 
(IDOPERATOR , [TYPE]) values
  (1,'assistant1')
, (2,'assistant2')
, (3,'assistant3')
;
insert into HOURS (idoperator, starthour, endhour) values
  (2,'14:30','15:30')
, (1,'09:00','17:33')
;

查询:

SELECT op.IDOPERATOR, op.[type]
,sum(datediff(minute,starthour,endhour)) as tmdiff
,case 
 when [type] = 'assistant1'
  and sum(datediff(minute,starthour,endhour)) > 480 
  then 'cease operation'
 when [type] = 'assistant2'
  and sum(datediff(minute,starthour,endhour)) > 420 
  then 'cease operation'
 when [type] = 'assistant3'
  and sum(datediff(minute,starthour,endhour)) > 540 
 then 'cease operation'
 when max(hr.IDOPERATOR) is null
 then 'unknown'
 else 'without effect'
 end AS [result]
FROM OPERATOR op
LEFT JOIN HOURS hr
  ON hr.IDOPERATOR = op.IDOPERATOR
GROUP BY op.IDOPERATOR, op.[type];

结果:

IDOPERATOR |类型tmdiff |结果---------:| :--------- | -----:| :--------------1 |助手1 | 513 |停止运作2 |助手2 | 60 |没有效果3 |助手3 |  |未知

db <>小提琴here

额外

仅一次查询并不需要。但是,如果可以重新使用某些逻辑,则有时会使用UDF(用户定义的功能)。

IF OBJECT_ID (N'dbo.ufnEvalHourDiff', N'FN') IS NOT NULL  
    DROP FUNCTION dbo.ufnEvalHourDiff;
GO
--
-- A User Defined Function
--
CREATE FUNCTION dbo.ufnEvalHourDiff
(
 @OperatorType NVARCHAR(20), 
 @TimeDiffMinutes INT
)  
RETURNS VARCHAR(30)   
AS   
BEGIN
  DECLARE @result VARCHAR(30);

  IF (@OperatorType = 'assistant1' 
      AND @TimeDiffMinutes > 480) OR
     (@OperatorType = 'assistant2' 
      AND @TimeDiffMinutes > 420) OR
     (@OperatorType = 'assistant3' 
      AND @TimeDiffMinutes > 540)
  begin
    SET @result = 'cease operation';
  end
  ELSE
    SET @result = 'without effect';

  RETURN @result;
END;
GO
SELECT op.IDOPERATOR, op.[type]
, SUM(DATEDIFF(MINUTE, starthour, endhour)) AS tmdiff
, dbo.ufnEvalHourDiff(op.[type], SUM(DATEDIFF(MINUTE, starthour, endhour))) as result
FROM OPERATOR op
LEFT JOIN HOURS hr
  ON hr.IDOPERATOR = op.IDOPERATOR
GROUP BY op.IDOPERATOR, op.[type];
GO
IDOPERATOR |类型tmdiff |结果---------:| :--------- | -----:| :--------------1 |助手1 | 513 |停止运作2 |助手2 | 60 |没有效果3 |助手3 |  |没有效果

db <>小提琴here


0
投票

如果IDhour与IDoperator相同,则可以执行以下操作:

select b.IDoperator,b.Type,
 case when TYPE = 'assistant1' and sum(datediff(minute,b.starthour,b.endhour)) > 480 then 'cease operation'
      when TYPE = 'assistant2' and sum(datediff(minute,b.starthour,b.endhour))> 420 then 'cease operation'
      when TYPE = 'assistant2' and sum(datediff(minute,b.starthour,b.endhour))> 540 then 'cease operation'
      else 'without effect' end as flag_check
from HOURS a inner join Operator b on a.idhour=b.idoperator
Group by b.IDOperator,b.Type
© www.soinside.com 2019 - 2024. All rights reserved.