1

表格1:

Person_ID   Name    Salary_Revisions
1           Test1   100
1           Test1   200
2           Test2   300
2           Test2   400

表2:

Person ID                  Department
-------------------------- ---------------- 
1                          Physics
1                          Chemistry
2                          Maths

我想得到如下结果:

Person_ID             Name               Salary_Revisions       Department
--------------------- ------------------ ---------------------- --------------
1                     Test1              100                    Physics
1                     Test1              200                    Chemistry
2                     Test2              300                    Maths
2                     Test2              400

实际的:

Person ID          Name      Salary Revisions      Department
------------------ --------- --------------------- ----------------
1                  Test1     100                   Physics
1                  Test1     200                   Physics
1                  Test1     100                   Chemistry
1                  Test1     200                   Chemistry
2                  Test2     300                   Maths
2                  Test2     400                   Maths

你能帮我实现预期的结果吗?

在实现这一点时,我通过使用人员 ID 将表 1 与表 2 左连接来编写了一个存储过程。通过在数据库中执行查询它返回像实际结果一样。

SQL查询:

SELECT table1.person_ID, table1.name, table1.salary_revisions, table2.department 
from table1 
left outer join table2 on table1.person_id=table2.person_id
4

3 回答 3

1

我不太了解使用场景,但我认为这就是您想要的。

两个 CTE 获取每个人每列的所有唯一值,并FULL OUTER JOIN逐行组合它们。

WITH salary_revision AS (
  SELECT person_id, name, salary_revisions sr, 
  ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY person_id) row
  FROM table1
), department AS (
  SELECT person_id, department,
  ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY person_id) row
  FROM table2
)
SELECT sr.person_id,sr.name, sr.sr, d.department
FROM salary_revision sr
FULL OUTER JOIN department d
  ON sr.person_id = d.person_id
 AND sr.row = d.row

一个用于测试的 SQLfiddle

于 2013-05-19T12:38:34.553 回答
1

鉴于您评论修订与部门之间没有关系,那么使用列表是有意义的。逗号分隔看起来不错。像这样:

 PERSON_ID  NAME   SALARY REVISION LIST  DEPARTMENT LIST
 ---------- ------ --------------------- ------------------------
 1          Test1  100, 200              Physics, Chemistry
 2          Test2  300, 400              Maths

这是查询:

SELECT DISTINCT
       Person_ID,
       Name,
       STUFF((SELECT ', ' + CAST(S.Salary_Revisions AS VARCHAR(50))
              FROM Table1 S
              WHERE S.Person_ID = P.Person_ID
              FOR XML PATH ('')),1,2,'') AS [Salary Revision List],
       STUFF((SELECT ', ' + D.Department
              FROM Table2 D
              WHERE D.Person_ID = P.Person_ID
              FOR XML PATH ('')),1,2,'') AS [Department List]
FROM Table1 P

这是小提琴: http ://sqlfiddle.com/#!3/27c07/3/0


原始答案

好吧,这很容易做到,但是您的业务规则中有一部分没有意义。为什么这一行没有部门?

Person_ID             Name               Salary_Revisions       Department
--------------------- ------------------ ---------------------- --------------
2                     Test2              400

表 2 没有暗示 user 2at300和 user 2at之间的区别400

您系统中的哪些数据意味着您显示的结果是正确的——结果是否也可以是:

Person_ID             Name               Salary_Revisions       Department
--------------------- ------------------ ---------------------- --------------
1                     Test1              100                    Physics
1                     Test1              200                    Chemistry
2                     Test2              300                    
2                     Test2              400                    Maths

或者

Person_ID             Name               Salary_Revisions       Department
--------------------- ------------------ ---------------------- --------------
1                     Test1              100                    Physics
1                     Test1              200                    Chemistry
2                     Test2              300                    Maths
2                     Test2              400                    Maths

如果您没有数据可以从逻辑上选择正确的数据,则无法执行此查询。

也许行的顺序很重要(这对 SQL 来说很奇怪)。也许有些数据您没有放入您需要的数据模型中?

于 2013-05-19T12:20:08.867 回答
-1
    WITH salary_revision AS (
     SELECT person_id, name, salary_revisions sr, 
     ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY person_id) row
     FROM table1
    ), department AS (
     SELECT person_id, department,
     ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY person_id) row
    FROM table2
    )

SELECT COALESCE(sr.person_id,d.person_id),sr.name, sr.sr, d.department 从salary_revision sr FULL OUTER JOIN department d ON sr.person_id = d.person_id AND sr.row = d.row

给 COALESCE 它考虑第一个非空值。所以我们在每一行中得到 peson_id。

于 2013-05-23T18:23:48.430 回答