避免循环输出

时间:2016-10-25 16:43:12

标签: sql sql-server-2008

有没有办法检查输出中是否有循环?

示例:

CREATE TABLE packages 
(
 request varchar(10), 
 depend varchar(10)
);

INSERT INTO packages
(request, depend)
VALUES
('R001', 'R990'),
('R001', 'R992'),
('R002', 'R991'),
('R002', 'R990'),
('R990', 'R001'),
('R010', 'R001'),
('R215', 'R001'),
('R990', 'R887'),
('R990', 'R886');

SELECT
  request,
  depend
FROM
(
  SELECT request, depend FROM packages WHERE request = 'R001'
  UNION ALL
  SELECT request, depend FROM packages WHERE depend IN ('R001')
) as Report

http://rextester.com/XLP60912

最终结果是:

     request     depend
1    R001        R990
2    R001        R992
3    R990        R001
4    R010        R001
5    R215        R001

第3行是第1行的循环,因此第3行不应该出现在结果中。

预期结果:

     request     depend
1    R001        R990
2    R001        R992
3    R010        R001
4    R215        R001

感谢。

2 个答案:

答案 0 :(得分:0)

如果您只是寻找交换对(如您的示例中所示),那么这将起作用:

select p.request, p.depend
from packages p
where p.request < p.depend or
      not exists (select 1
                  from packages p2
                  where p2.request = p.depend and p2.depend = p.request
                 );

答案 1 :(得分:0)

subprocess.CalledProcessError: Command 'aws s3 ls s3://path/to/my/bucket/12434 --recursive --human-readable --summarize' returned non-zero exit status 1