Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 具有行数逻辑问题的嵌套子查询_Sql Server_Subquery - Fatal编程技术网

Sql server 具有行数逻辑问题的嵌套子查询

Sql server 具有行数逻辑问题的嵌套子查询,sql-server,subquery,Sql Server,Subquery,SQLServer2008我正在尝试从工厂的计时系统中过滤劳动记录。下面是一个数据示例。TCODE列的on时间为“O”,off时间为“of”。比如,查韦斯在50号序列上工作了1.06个小时 问题:当一个工作人员完成一个作业序列时,刷卡次数超过2次,系统会自动添加最后一对0小时需要删除的开关记录。在sequence 50 Pull Kit和sequence 400埋头钻的情况下,只有两次滑动打开,然后关闭,因此没有额外的记录。但是,其他每个序列都有两个尾随的额外行。在序列100的情况下,TTIME

SQLServer2008我正在尝试从工厂的计时系统中过滤劳动记录。下面是一个数据示例。TCODE列的on时间为“O”,off时间为“of”。比如,查韦斯在50号序列上工作了1.06个小时

问题:当一个工作人员完成一个作业序列时,刷卡次数超过2次,系统会自动添加最后一对0小时需要删除的开关记录。在sequence 50 Pull Kit和sequence 400埋头钻的情况下,只有两次滑动打开,然后关闭,因此没有额外的记录。但是,其他每个序列都有两个尾随的额外行。在序列100的情况下,TTIME=93724 09:37:24 AM的最后两行是额外的行。您可以看到,最后一行的完整代码为1,正式标记作业序列已完成

MFGORDNO  SEQUENCE  DESCR                 BADGE  LABOR  TCODE  ENAME   TTIME  CompleteCode
M968460   50        Pull Kit              802    0.00   O      CHAVEZ  82300  NULL
M968460   50        Pull Kit              802    1.06   OF     CHAVEZ  92631  0
M968460   100       Load Parts into AJ    396    0.00   O      CURNEY  150014 NULL
M968460   100       Load Parts into AJ    396    1.00   OF     CURNEY  160022 0
M968460   100       Load Parts into AJ    3169   0.00   O      JONES   84612  NULL
M968460   100       Load Parts into AJ    3169   0.85   OF     JONES   93724  0
M968460   100       Load Parts into AJ    3169   0.00   O      JONES   93724  NULL
M968460   100       Load Parts into AJ    3169   0.00   OF     JONES   93724  1
M968460   200       Transfer Drill Holes  3169   0.00   O      JONES   93737  NULL
M968460   200       Transfer Drill Holes  3169   2.73   OF     JONES   132135 0
M968460   200       Transfer Drill Holes  3169   0.00   O      JONES   132135 NULL
M968460   200       Transfer Drill Holes  3169   0.00   OF     JONES   132135 1
M968460   300       TransDrill Splices    3169   0.00   O      JONES   132153 NULL
M968460   300       TransDrill Splices    3169   3.56   OF     JONES   65539  0
M968460   300       TransDrill Splices    3169   0.00   O      JONES   65539  NULL
M968460   300       TransDrill Splices    3169   0.01   OF     JONES   65539  1
M968460   400       Countersinking        3169   0.00   O      JONES   63102  NULL
M968460   400       Countersinking        3169   2.79   OF     JONES   91716  0
M968460   600       SPLICE STRAPS         3169   0.00   O      JONES   131931 NULL
M968460   600       SPLICE STRAPS         3169   1.17   OF     JONES   143040 0
M968460   600       SPLICE STRAPS         3169   0.00   O      JONES   63456  NULL
M968460   600       SPLICE STRAPS         3169   4.12   OF     JONES   105200 0
M968460   600       SPLICE STRAPS         3169   0.00   O      JONES   105200 NULL
M968460   600       SPLICE STRAPS         3169   0.00   OF     JONES   105200 1
期望的结果:简言之,我尝试使用一条SQL语句来获得这个结果数据

MFGORDNO  SEQUENCE  DESCR                 BADGE  LABOR  TCODE  ENAME   TTIME  CompleteCode
M968460   50        Pull Kit              802    0.00   O      CHAVEZ  82300  NULL
M968460   50        Pull Kit              802    1.06   OF     CHAVEZ  92631  0
M968460   100       Load Parts into AJ    396    0.00   O      CURNEY  150014 NULL
M968460   100       Load Parts into AJ    396    1.00   OF     CURNEY  160022 0
M968460   100       Load Parts into AJ    3169   0.00   O      JONES   84612  NULL
M968460   100       Load Parts into AJ    3169   0.85   OF     JONES   93724  0
M968460   200       Transfer Drill Holes  3169   0.00   O      JONES   93737  NULL
M968460   200       Transfer Drill Holes  3169   2.73   OF     JONES   132135 0
M968460   300       TransDrill Splices    3169   0.00   O      JONES   132153 NULL
M968460   300       TransDrill Splices    3169   3.56   OF     JONES   65539  0
M968460   400       Countersinking        3169   0.00   O      JONES   63102  NULL
M968460   400       Countersinking        3169   2.79   OF     JONES   91716  0
M968460   600       SPLICE STRAPS         3169   0.00   O      JONES   131931 NULL
M968460   600       SPLICE STRAPS         3169   1.17   OF     JONES   143040 0
M968460   600       SPLICE STRAPS         3169   0.00   O      JONES   63456  NULL
M968460   600       SPLICE STRAPS         3169   4.12   OF     JONES   105200 0
我最初开始在最内部的子查询中添加行号,以便以后可以像这样引用MAXrn:

SELECT
    *, 
    ROW_NUMBER() OVER (ORDER by TTIME) rn 
FROM
    MySmallData
在上面的子查询中,我希望从原始数据中选择所有记录,删除CompleteCode中包含1的行和上面的行

这与其说是语法问题,不如说是策略问题。我希望得到一些建议

谢谢, 约翰

编辑:抱歉,以下是一些示例数据:

CREATE TABLE MySmallData 
(
    [MFGORDNO] NVARCHAR(7),
    [SEQUENCE] INT,
    [DESCR] NVARCHAR(20),
    [BADGE] INT,
    [LABOR] NUMERIC(3, 2),
    [TCODE] NVARCHAR(2),
    [TSTAMP] NVARCHAR(26),
    [ENAME] NVARCHAR(19),
    [TTIME] INT,
    [CompleteCode] INT
);

INSERT INTO MySmallData 
VALUES
    ('M968460',0050,'Pull Kit',802,0,'O','2019-02-26 08:23:00.000004','CHAVEZ',82300,NULL),
    ('M968460',0050,'Pull Kit',802,1.06,'OF','2019-02-26 09:26:30.999995','CHAVEZ',92631,0),
    ('M968460',0100,'Load Parts into AJ',396,0,'O','2019-03-05 15:00:13.999997','CURNEY',150014,NULL),
    ('M968460',0100,'Load Parts into AJ',396,1,'OF','2019-03-05 16:00:22.000001','CURNEY',160022,0),
    ('M968460',0100,'Load Parts into AJ',3169,0,'O','2019-03-06 08:46:12.000003','JONES',84612,NULL),
    ('M968460',0100,'Load Parts into AJ',3169,0.85,'OF','2019-03-06 09:37:23.999998','JONES',93724,0),
    ('M968460',0100,'Load Parts into AJ',3169,0,'O','2019-03-06 09:37:23.999998','JONES',93724,NULL),
    ('M968460',0100,'Load Parts into AJ',3169,0,'OF','2019-03-06 09:37:23.999998','JONES',93724,1),
    ('M968460',0200,'Transfer Drill Holes',3169,0,'O','2019-03-06 09:37:37.000001','JONES',93737,NULL),
    ('M968460',0200,'Transfer Drill Holes',3169,2.73,'OF','2019-03-06 13:21:35.000001','JONES',132135,0),
    ('M968460',0200,'Transfer Drill Holes',3169,0,'O','2019-03-06 13:21:35.000001','JONES',132135,NULL),
    ('M968460',0200,'Transfer Drill Holes',3169,0,'OF','2019-03-06 13:21:35.000001','JONES',132135,1),
    ('M968460',0300,'TransDrill Splices',3169,0,'O','2019-03-06 13:21:52.999998','JONES',132153,NULL),
    ('M968460',0300,'TransDrill Splices',3169,3.56,'OF','2019-03-06 16:55:39','JONES',165539,0),
    ('M968460',0300,'TransDrill Splices',3169,0,'O','2019-03-06 16:55:39','JONES',165539,NULL),
    ('M968460',0300,'TransDrill Splices',3169,0.01,'OF','2019-03-06 16:55:39','JONES',165539,1),
    ('M968460',0400,'Countersinking',3169,0,'O','2019-03-07 06:31:01.999998','JONES',63102,NULL),
    ('M968460',0400,'Countersinking',3169,2.79,'OF','2019-03-07 09:17:15.999996','JONES',91716,0),
    ('M968460',0600,'SPLICE STRAPS',3169,0,'O','2019-03-08 13:19:30.999999','JONES',131931,NULL),
    ('M968460',0600,'SPLICE STRAPS',3169,1.17,'OF','2019-03-08 14:30:39.999997','JONES',143040,0),
    ('M968460',0600,'SPLICE STRAPS',3169,0,'O','2019-03-12 06:34:56.000003','JONES',63456,NULL),
    ('M968460',0600,'SPLICE STRAPS',3169,4.12,'OF','2019-03-12 10:52:00.000002','JONES',105200,0),
    ('M968460',0600,'SPLICE STRAPS',3169,0,'O','2019-03-12 10:52:00.000002','JONES',105200,NULL),
    ('M968460',0600,'SPLICE STRAPS',3169,0,'OF','2019-03-12 10:52:00.000002','JONES',105200,1);

我认为你可以通过以下方式做到这一点:


是一个dbfiddle。

开始研究我自己的版本,我想不,使用@Gordon's,那么他是如何做到的,遇到了一些问题,所以使用+1并对其进行了一些修改:

SELECT msd.*
 from mysmalldata msd
 where msd.completecode = 0  --  All "completeCode=0" rows are accepted
  or (--  All "NULL" rows for which there is no matching "completeCode=1" row are accepted
      msd.completecode is null
       and not exists (select 1
                        from mysmalldata msd2
                         where msd2.MFGORDNO = msd.MFGORDNO
                          and msd2.sequence = msd.sequence
                          and msd2.badge = msd.badge
                          and msd2.ttime = msd.ttime
                          and msd2.completecode = 1
                      )
     )
 --  Note that no "completeCode=1" rows are accepted

谢谢@Gordon Linoff。是的,这就是我遇到麻烦的地方。以上仅返回CompleteCode=0的记录。它删除所有空行。应仅删除与CompleteCode=1行成对的空行。其他空行需要保留在结果中。@JohnJoseph。这是我很长一段时间以来发现的一个更微妙的错误。当completecode为NULL时,OR上的第二个条件返回NULL。这导致整个表达式为NULL并被过滤掉。我已经取代了逻辑。也是很棒的@PhillipKelley。很好的减少。感谢您的帮助未来的读者:@Gordon根据我的修订版修改了他的代码。两个可能的解决方案,一个价格!
SELECT msd.*
 from mysmalldata msd
 where msd.completecode = 0  --  All "completeCode=0" rows are accepted
  or (--  All "NULL" rows for which there is no matching "completeCode=1" row are accepted
      msd.completecode is null
       and not exists (select 1
                        from mysmalldata msd2
                         where msd2.MFGORDNO = msd.MFGORDNO
                          and msd2.sequence = msd.sequence
                          and msd2.badge = msd.badge
                          and msd2.ttime = msd.ttime
                          and msd2.completecode = 1
                      )
     )
 --  Note that no "completeCode=1" rows are accepted