Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.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 完整状态出现2次及以上时的返回值_Sql_Sql Server - Fatal编程技术网

Sql 完整状态出现2次及以上时的返回值

Sql 完整状态出现2次及以上时的返回值,sql,sql-server,Sql,Sql Server,Ori数据=#B 预期结果: +--------+-------+---------------+---------------+---------------+---------------+----------+ | RowNum | SeqNo | Name | NameReason | NameDate | Date-Of-Birth | Status | +--------+-------+---------------+-------------

Ori数据=#B

预期结果:

+--------+-------+---------------+---------------+---------------+---------------+----------+
| RowNum | SeqNo |     Name      |  NameReason   |   NameDate    | Date-Of-Birth |  Status  |
+--------+-------+---------------+---------------+---------------+---------------+----------+
|      1 | AAA   | ABC           | A             | 2019-01-01    | 1905-01-01    | Complete |
|      2 | AAA   | 'emptystring' | 'emptystring' | 'emptystring' | 1905-01-01    | Pending  |
|      3 | AAA   | 'emptystring' | 'emptystring' | 'emptystring' | 1905-01-01    | Pending  |
|      4 | AAA   | 'emptystring' | 'emptystring' | 'emptystring' | 1905-01-01    | Pending  |
|      5 | AAA   | 'emptystring' | 'emptystring' | 'emptystring' | 1905-01-01    | Complete |
|        |       |               |               |               |               |          |
|      1 | BBB   | 'emptystring' | 'emptystring' | 'emptystring' | 1970-01-01    | Pending  |
|      2 | BBB   | DEF           | A             | 2019-01-02    | 1970-01-01    | Complete |
|      3 | BBB   | GHI           | A             | 2019-01-03    | 1970-01-01    | Complete |
|      4 | BBB   | ABC           | A             | 2019-01-03    | 1970-01-01    | Complete |
|      5 | BBB   | 'emptystring' | 'emptystring' | 'emptystring' | 1970-01-01    | Pending  |
+--------+-------+---------------+---------------+---------------+---------------+----------+
RowNum是正确筛选的row_num函数。我使用rownum按日期对seqno、出生日期顺序进行分区。该数据来自表A,我将其插入表B

问题: 不在乎我有多少行数,只要status='Completed'出现2次及以上,我就想要这些数据。如果status='Completed'出现1次,就不要这些数据


之所以我希望的结果像这样毫无意义,是因为我想找出中间的任何更改,如果有更改(我在另一个脚本中处理),我会显示出来。

如果您只是希望给定序号的所有行具有超过1个“已完成”状态:

SELECT
  *
FROM
  B
WHERE
  SeqNo IN (
    SELECT
      SeqNo
    FROM
      B
    WHERE
      Status='Completed'
    GROUP BY
      SeqNo
    HAVING
      COUNT(*) > 1); 

这就是Oracle语法。我希望您在SeqNo字段上有一个索引。

据我所知,您希望在这方面有所进展

declare @Something table
(
    RowNum int
    , SeqNo char(3)
    , Name varchar(20)
    , NameReason varchar(20)
    , NameDate varchar(20)
    , DateOfBirth date
    , Status varchar(20)
)

insert @Something values
(1, 'AAA', 'ABC        ', 'A          ', '2019-01-01 ', '1905-01-01', 'Complete')
, (2, 'AAA', 'emptystring', 'emptystring', 'emptystring', '1905-01-01', 'Pending ')
, (3, 'AAA', 'emptystring', 'emptystring', 'emptystring', '1905-01-01', 'Pending ')
, (4, 'AAA', 'emptystring', 'emptystring', 'emptystring', '1905-01-01', 'Pending ')
, (5, 'AAA', 'emptystring', 'emptystring', 'emptystring', '1905-01-01', 'Complete')
, (1, 'BBB', 'emptystring', 'emptystring', 'emptystring', '1970-01-01', 'Pending ')
, (2, 'BBB', 'DEF        ', 'A          ', '2019-01-02 ', '1970-01-01', 'Complete')
, (3, 'BBB', 'GHI        ', 'A          ', '2019-01-03 ', '1970-01-01', 'Complete')
, (4, 'BBB', 'ABC        ', 'A          ', '2019-01-03 ', '1970-01-01', 'Complete')
, (5, 'BBB', 'emptystring', 'emptystring', 'emptystring', '1970-01-01', 'Pending ')
, (1, 'CCC', 'ABC        ', 'A          ', '2019-01-01 ', '1990-01-01', 'Complete')
, (2, 'CCC', 'emptystring', 'emptystring', 'emptystring', '1990-01-01', 'Pending ')
, (3, 'CCC', 'emptystring', 'emptystring', 'emptystring', '1990-01-01', 'Pending ')
, (1, 'DDD', 'emptystring', 'emptystring', 'emptystring', '2001-05-02', 'Pending ')
, (2, 'DDD', 'ABC        ', 'A          ', '2019-01-01 ', '2001-05-02', 'Complete')
, (3, 'DDD', 'emptystring', 'emptystring', 'emptystring', '2001-05-02', 'Pending ')

select *
from @Something s
where exists 
(
    select s2.SeqNo
    from @Something s2
    where s2.SeqNo = s.SeqNo
        and s2.Status = 'Complete'
    group by s2.SeqNo
    having count(*) > 1
)

使用GROUPBY with having子句。您必须对此进行解释。你的输出毫无意义。为什么有些行返回,有些行不返回?此外,您似乎关心行的顺序,但您的数据中没有任何内容可用于对这些行进行排序。您似乎也在混合数据类型。在单个列中包含日期和字符串之类的内容。是否有任何列能够表示行的顺序?您已经编辑了此内容,但仍然没有多大意义。如果至少有状态为“完成”的行,是否希望给定序号的所有行?使用where-exists将很快解决这个问题。到目前为止,您尝试了什么?而不是使用“where-SeqNo in”,您确实应该使用“exists”,这可能会带来更好的性能
declare @Something table
(
    RowNum int
    , SeqNo char(3)
    , Name varchar(20)
    , NameReason varchar(20)
    , NameDate varchar(20)
    , DateOfBirth date
    , Status varchar(20)
)

insert @Something values
(1, 'AAA', 'ABC        ', 'A          ', '2019-01-01 ', '1905-01-01', 'Complete')
, (2, 'AAA', 'emptystring', 'emptystring', 'emptystring', '1905-01-01', 'Pending ')
, (3, 'AAA', 'emptystring', 'emptystring', 'emptystring', '1905-01-01', 'Pending ')
, (4, 'AAA', 'emptystring', 'emptystring', 'emptystring', '1905-01-01', 'Pending ')
, (5, 'AAA', 'emptystring', 'emptystring', 'emptystring', '1905-01-01', 'Complete')
, (1, 'BBB', 'emptystring', 'emptystring', 'emptystring', '1970-01-01', 'Pending ')
, (2, 'BBB', 'DEF        ', 'A          ', '2019-01-02 ', '1970-01-01', 'Complete')
, (3, 'BBB', 'GHI        ', 'A          ', '2019-01-03 ', '1970-01-01', 'Complete')
, (4, 'BBB', 'ABC        ', 'A          ', '2019-01-03 ', '1970-01-01', 'Complete')
, (5, 'BBB', 'emptystring', 'emptystring', 'emptystring', '1970-01-01', 'Pending ')
, (1, 'CCC', 'ABC        ', 'A          ', '2019-01-01 ', '1990-01-01', 'Complete')
, (2, 'CCC', 'emptystring', 'emptystring', 'emptystring', '1990-01-01', 'Pending ')
, (3, 'CCC', 'emptystring', 'emptystring', 'emptystring', '1990-01-01', 'Pending ')
, (1, 'DDD', 'emptystring', 'emptystring', 'emptystring', '2001-05-02', 'Pending ')
, (2, 'DDD', 'ABC        ', 'A          ', '2019-01-01 ', '2001-05-02', 'Complete')
, (3, 'DDD', 'emptystring', 'emptystring', 'emptystring', '2001-05-02', 'Pending ')

select *
from @Something s
where exists 
(
    select s2.SeqNo
    from @Something s2
    where s2.SeqNo = s.SeqNo
        and s2.Status = 'Complete'
    group by s2.SeqNo
    having count(*) > 1
)