外连接/联盟? - t / sql查询

时间:2012-03-14 14:44:30

标签: sql sql-server tsql join

我需要一个复杂的t / sql查询帮助我写一篇特别的报告。 我不太清楚如何表达这个问题;但我有一个去:

基本上,我有三个表格如下:

@PrimaryTable
  Key - Is unique and may or may not exist
  Description1 - Some field 
  Description2 - Some field which works as a heading, more on this later

@Subtable1
  KeyFK - is NOT unique, links to Key in primary table. May or may not exist. May also exist multiple times per Key
  Description - The field I want

@Subtable2 
 - Just like subtable1, but does not relate to subtable1 in any way

我想将这些表连接到PrimaryTable,因为它看起来是UNION ALL,但没有所有NULL值。

请参阅此查询以获取示例:

DECLARE @PrimaryTable AS table ("Key" int, Description1 varchar(32), Description2 varchar(32));        --Contains MAXIMUM one of each Key
DECLARE @Subtable_1 AS table ("KeyPK" int, SubDescription1 varchar(32));    --Can contain zero, one or more lines with same KeyPK
DECLARE @Subtable_2 AS table ("KeyPK" int, SubDescription2 varchar(32));    --Can contain zero, one or more lines with same KeyPK

INSERT INTO @PrimaryTable VALUES (1, 'Description', 'Heading');
INSERT INTO @PrimaryTable VALUES (2, 'Description', 'Heading');
INSERT INTO @PrimaryTable VALUES (3, 'Description', 'Heading');
INSERT INTO @PrimaryTable VALUES (5, 'Description', 'Heading');
INSERT INTO @PrimaryTable VALUES (6, 'Description', 'Heading');
INSERT INTO @PrimaryTable VALUES (7, 'Description', 'Heading');
INSERT INTO @PrimaryTable VALUES (8, 'Description', 'Heading');

INSERT INTO @Subtable_1 VALUES (1, 'Subdescription1_1');

INSERT INTO @Subtable_2 VALUES (2, 'Subdescription2_1');

INSERT INTO @Subtable_1 VALUES (3, 'Subdescription1_1');
INSERT INTO @Subtable_2 VALUES (3, 'Subdescription2_1');

INSERT INTO @Subtable_1 VALUES (4, 'Subdescription1_1');
INSERT INTO @Subtable_2 VALUES (4, 'Subdescription2_1');
INSERT INTO @Subtable_2 VALUES (4, 'Subdescription2_2');

INSERT INTO @Subtable_1 VALUES (5, 'Subdescription1_1');
INSERT INTO @Subtable_1 VALUES (5, 'Subdescription1_2');
INSERT INTO @Subtable_2 VALUES (5, 'Subdescription2_1');

INSERT INTO @Subtable_1 VALUES (6, 'Subdescription1_1');
INSERT INTO @Subtable_2 VALUES (6, 'Subdescription2_1');
INSERT INTO @Subtable_2 VALUES (6, 'Subdescription2_2');

INSERT INTO @Subtable_1 VALUES (7, 'Subdescription1_1');
INSERT INTO @Subtable_1 VALUES (7, 'Subdescription1_2');

我希望结果如下:

/*
Key Description1    Description2    Subdescription1     Subdescription2
_________________________________________________________________________
1   Description     Heading         ''                  ''
1   Description     ''              Subdescription1_1   NULL

2   Description     Heading         ''                  ''
2   Description     ''              NULL                Subdescription2_1

3   Description     Heading         ''                  ''
3   Description     ''              Subdescription1_1   Subdescription2_1

5   Description     Heading         ''                  ''
5   Description     ''              Subdescription1_1   Subdescription2_1
5   Description     ''              Subdescription1_2   NULL

6   Description     Heading         ''                  ''
6   Description     ''              Subdescription1_1   Subdescription2_1
6   Description     ''              NULL                Subdescription2_2

7   Description     Heading         ''                  ''
7   Description     ''              Subdescription1_1   NULL
7   Description     ''              Subdescription1_2   NULL

8   Description     Heading         ''                  ''
*/

带有空描述的标题行没有问题;之后可以很容易地使用工会来完成,但我不知道如何加入这些表格 - 有谁知道如何做到这一点?

编辑: 我应该从一开始就解释报告: 我正在研究一个非常棒的数据库结构,但我有一个非常奇怪的报告要求。

PrimaryTable是一个实际的表,报告要求 Subtable1是解释主表不同部分的表,这是真实的信息。 Subtable2不是一个表,而是一个合同(来自PrimaryTable的字段)分解为一行pr。线。

然后,报告希望主表中的所有信息都作为包含一些小计的标题。然后有一个折叠选项,打开到一行pr。 subtable2描述/合同中的行。线条的数量pr Key只显示每个折叠的长度。

SQL可以为报告回答时间完成无效操作。 - 这将是一个存储过程,它将生成一个基于快速应答解决方案的表

2 个答案:

答案 0 :(得分:1)

你想要这样的东西:

SELECT key, description1, description2, '', ''
FROM PrimaryTable
UNION
SELECT distinct p.description1, '', s1.subdescription1, s2.subdescription2
FROM PrimaryTable as p
LEFT JOIN Subtable1 as s1
ON p.key = s1.key
LEFT JOIN Subtable2 as s2
ON p.key = s2.key

我还会说,看起来你有一个非常奇怪的数据库结构。你有两个“subdescription”表吗?

答案 1 :(得分:0)

这接近您的预期结果(仔细检查Key = 6和8的位置):

SELECT "Key", 
       P1.Description1, '' AS Description2, 
       S1.SubDescription1, S2.SubDescription2
  FROM @PrimaryTable AS P1
       LEFT OUTER JOIN @Subtable_1 AS S1
          ON P1."Key" = S1."KeyPK"
       LEFT OUTER JOIN @Subtable_2 AS S2
          ON P1."Key" = S2."KeyPK"
UNION
SELECT "Key", 
       P1.Description1, P1.Description2, 
       '' AS SubDescription1, '' AS SubDescription2
  FROM @PrimaryTable AS P1
 ORDER 
    BY "Key", Description2 DESC;