比较两个表后创建标志列

时间:2019-05-22 19:10:09

标签: sql sql-server performance tsql sql-server-2017

我有两个不同的报表表,分别带有日期时间和报表所有者。我想选择至少写过两次报告的人。我还需要一个计算字段,以显示他们编写的报告编号。报告1优先,因此,如果有人在任何时候编写了报告1,则新的report_number列应为1,否则为2(对于报告2)。

'people' table
| person_id | full_name
--------------------------
| 1         | John L Smith
| 2         | Carl M Selt
| 3         | Another Person

'report_1' table
| report_1_id | author_person_id | date_entered | other_columns
---------------------------------------------------------------
| 1           | 1                | 2018-01-12   | foo
| 2           | 1                | 2018-02-18   | foo foo

'report_2' table
| report_2_id | author_person_id | date_entered | other_columns
---------------------------------------------------------------
| 1           | 1                | 2018-03-21   | bar
| 2           | 1                | 2018-03-28   | bar bar
| 3           | 2                | 2018-04-16   | baz
| 4           | 2                | 2018-04-30   | baz baz

所需结果:

| full_name    | report_number
---------------------------
| John L Smith | 1
| Carl M Smelt | 2

请注意,尽管约翰的report_number1,尽管他也撰写了一份报告2。

报表1和报表2的其他列不同,即使它们在上方看起来相同。

我尝试过的事情:

    /* Get people from both reports */
WITH report_1_people AS (
    SELECT P.full_name
    FROM report_1 R1
    INNER JOIN people P ON R1.author_person_id = P.person_id
    WHERE P.full_name IS NOT NULL 
    AND P.full_name <> ''
), report_2_people AS (
    SELECT P2.full_name
    FROM report_2 R2
    INNER JOIN people P2 ON R2.author_person_id = P2.person_id
    WHERE P2.full_name IS NOT NULL 
    AND P2.full_name <> ''
)
SELECT 
    P.full_name,
    CASE WHEN P.full_name IN ( /* Check if in report 1 */
                    SELECT full_name
                    FROM report_1)
                    THEN 1
            ELSE 2
            END AS report_number
FROM people P
WHERE P.full_name IS NOT NULL AND P.full_name <> ''
/* Eliminate duplicate names */
GROUP BY P.full_name 
/* Filter only who either authored report 1 or report 2 */
HAVING P.full_name IN (SELECT full_name
                       FROM report_1_people)
OR P.full_name IN (SELECT full_name
                   FROM report_2_people)

注意:人员表中有一个GROUP BY,因为某些原因,它们存在重复项。

查询花费了很长时间才与数据库断开连接(超过24小时),所以我认为我做错了什么。有没有更好的方法来完成基于两个表的此标志计算列? SQL相对较新,所以我想知道是否还有另一种思考方式让我过度关注SQL逻辑。

3 个答案:

答案 0 :(得分:1)

您可以使用OUTER APPLY

SELECT person_id, full_name, COALESCE(ca1.report_num, ca2.report_num)
FROM people
OUTER APPLY (SELECT TOP (1) 1 FROM report_1 WHERE author_person_id = people.person_id) AS ca1(report_num)
OUTER APPLY (SELECT TOP (1) 2 FROM report_2 WHERE author_person_id = people.person_id) AS ca2(report_num)

Demo on db<>fiddle

答案 1 :(得分:1)

CTE上的IN很可能将其杀死。

另一种方法是使用EXISTS检查某人是否已写报告。 CASE表达式可以处理优先级。

SELECT p.full_name,
       CASE
         WHEN EXISTS (SELECT *
                             FROM report_1 r1
                             WHERE r1.author_person_id = p.person_id) THEN
           1
         WHEN EXISTS (SELECT *
                             FROM report_2 r2
                             WHERE r2.author_person_id = p.person_id) THEN
           2
       END report_number
       FROM people p
       WHERE EXISTS (SELECT *
                            FROM report_1 r1
                            WHERE r1.author_person_id = p.person_id)
              OR EXISTS (SELECT *
                                FROM report_2 r2
                                WHERE r2.author_person_id = p.person_id);

为获得性能,请尝试在report_1 (author_person_id)report_2 (author_person_id)上建立索引。对于people,您可以尝试在person_id(可能已经存在)上使用索引,也可以在person_idfull_name上使用复合索引。

答案 2 :(得分:0)

这只是获得结果的另一种方式。

SELECT 
    P.full_name,
    MIN( R.Report_Number) AS report_number
FROM people P
OUTER APPLY (SELECT 1 WHERE EXISTS(SELECT * FROM report_1 R1 WHERE R1.author_person_id = P.person_id)
             UNION ALL
             SELECT 2 WHERE EXISTS(SELECT * FROM report_2 R2 WHERE R2.author_person_id = P.person_id)) AS R(Report_Number)
WHERE P.full_name IS NOT NULL AND P.full_name <> ''
/* Eliminate duplicate names */
GROUP BY P.full_name;