1

顶部查询在一个表中查找每个语句只有一条记录,因此如果客户有 7 条语句,则行数应为 7,查询将列出它们,如下所示 1,2,3,4,5,6 ,7

底部查询在一个表中查找,该表将包含上述 7 个语句,但通常它们会被拆分,因此如果每个语句有 2 行,则将有 14 行,即 1、1、2、2, 3,3,4,4,5,5,6,6,7,7

现在,我想要实现的是以下内容,顶部查询很好,但是底部查询需要区分。查看底部图像上的结果编号 2,我希望它返回为 1、2、3、4、5、6、7、8、9、10。

如果有一个特定的例子,语句分别上升到 8 和 4,我想要 1、2、3、4、5、6、7、8。基本上它是在大多数情况下复制底部查询的语句编号,我希望它只是对整个结果做一个不同的occross,但是我尝试放入一个不同的,它抱怨按项目排序必须包含在选择中如果声明包含在不同的内容中,这会破坏我的查询。

这两个查询的最终目的是将顶部结果集与底部结果集进行比较,并只带回不匹配的结果集(因为这意味着我在顶部查询表中缺少一条语句)

--Shows the each consolidated statement number that exisits for that particular customer reference number within the dbo.rss table.

Select Main.cust_ref,
       Left(CAST(Main.consolidatedstatements as varchar(max)),Len(CAST(Main.consolidatedstatements as varchar(max)))-1) As "consolidatedstatements"
From(Select distinct ST2.cust_ref, 
           (Select CAST(ST1.consolidated_stmt_num as varchar(max)) + ',' AS [text()]
            From dbo.rss ST1
            Where ST1.cust_ref = ST2.cust_ref
            ORDER BY ST1.cust_ref
            For XML PATH ('')) [consolidatedstatements]
     From dbo.rss ST2) [Main]


--Shows the each consolidated statement number that exisits for that particular customer reference number within the dbo.SC table.
Select Main.cust_ref,
       Left(CAST(Main.consolidatedstatements as varchar(max)),Len(CAST(Main.consolidatedstatements as varchar(max)))-1) As "consolidatedstatements"
From(Select distinct ST2.cust_ref, 
           (Select  CAST(ST1.consolidated_stmt_num as varchar(max)) + ',' AS [text()]
            From dbo.SC ST1
            Where ST1.cust_ref = ST2.cust_ref
            ORDER BY ST1.cust_ref
            For XML PATH ('')) [consolidatedstatements]
     From dbo.SC ST2) [Main]

在此处输入图像描述

4

1 回答 1

2
CREATE TABLE #rss(cust_ref VARCHAR(32), consolidated_stmt_num INT);

CREATE TABLE #SC(cust_ref VARCHAR(32), consolidated_stmt_num INT);

INSERT #SC VALUES
('A',1),('A',2),('A',3),('A',4),('A',5),('A',6),('A',7),('A',8),('A',9),
('B',1),('B',2),('B',3),('B',4),('B',5),('B',6),('B',7),('B',8),('B',9),
('C',1),('C',2),('C',3),('C',4),('C',5),('C',6),('C',7),('C',8),('C',9);

-- missing A,7 and C,2/C,4:
INSERT #rss VALUES
('A',1),('A',2),('A',3),('A',4),('A',5),('A',6),        ('A',8),('A',9),
('B',1),('B',2),('B',3),('B',4),('B',5),('B',6),('B',7),('B',8),('B',9),
('C',1),        ('C',3),        ('C',5),('C',6),('C',7),('C',8),('C',9);

GO

-- 仅此一项就会告诉您缺少语句的 cust_refs(以及它们是哪些):

SELECT cust_ref, consolidated_stmt_num FROM #SC
EXCEPT
SELECT cust_ref, consolidated_stmt_num FROM #rss;

-- 这将允许您在一行中从每个表中获取完整的串联列表:

;WITH x AS
(
  SELECT DISTINCT cust_ref FROM 
  (
    SELECT cust_ref, consolidated_stmt_num FROM #SC
    EXCEPT
    SELECT cust_ref, consolidated_stmt_num FROM #rss
  ) AS y
)
SELECT x.cust_ref, 
    rss = STUFF((SELECT ',' + CONVERT(VARCHAR(12), r.consolidated_stmt_num) 
      FROM #rss AS r WHERE r.cust_ref = x.cust_ref
      ORDER BY r.cust_ref
    FOR XML PATH(''), TYPE).value('./text()[1]','varchar(max)'),1,1,''),
    sc =  STUFF((SELECT ',' + CONVERT(VARCHAR(12), s.consolidated_stmt_num)
      FROM #SC  AS s WHERE s.cust_ref = x.cust_ref
      ORDER BY s.cust_ref
    FOR XML PATH(''), TYPE).value('./text()[1]','varchar(max)'),1,1,'')
FROM x;

-- 如果你需要它们在不同的行:

;WITH x AS
(
  SELECT DISTINCT cust_ref FROM 
  (
    SELECT cust_ref, consolidated_stmt_num FROM #SC
    EXCEPT
    SELECT cust_ref, consolidated_stmt_num FROM #rss
  ) AS y
)
SELECT x.cust_ref, source = 'rss',
    list = STUFF((SELECT ',' + CONVERT(VARCHAR(12), r.consolidated_stmt_num) 
      FROM #rss AS r WHERE r.cust_ref = x.cust_ref
      ORDER BY r.cust_ref
    FOR XML PATH(''), TYPE).value('./text()[1]','varchar(max)'),1,1,'')
    FROM x
UNION ALL SELECT x.cust_ref, 'sc',
    list = STUFF((SELECT ',' + CONVERT(VARCHAR(12), s.consolidated_stmt_num)
      FROM #SC  AS s WHERE s.cust_ref = x.cust_ref
      ORDER BY s.cust_ref
    FOR XML PATH(''), TYPE).value('./text()[1]','varchar(max)'),1,1,'')
    FROM x
ORDER BY cust_ref, source;

- 清理:

DROP TABLE #rss, #SC;
于 2013-08-12T16:11:27.293 回答