SQL连接。一对多关系

时间:2015-08-17 06:25:12

标签: sql sql-server

我有两张表格如下

Table 1
-----------------------------------
UserID   | UserName | Age | Salary
-----------------------------------
1        | foo      | 22  | 33000       
-----------------------------------
Table 2
------------------------------------------------
UserID   | Age      | Salary     | CreatedDate
------------------------------------------------
1        | NULL     | 35000      | 2015-01-01
------------------------------------------------
1        |  28      | NULL       | 2015-02-01
------------------------------------------------
1        |  NULL    | 28000      | 2015-03-01
------------------------------------------------

我需要这样的结果。

Result
-----------------------------------
UserID   | UserName | Age | Salary
-----------------------------------
1        | foo      | 28  | 28000
-----------------------------------

这只是一个例子。在我的真实项目中,我在上表中有大约6列,如Age和Salary。

在表2中,每条记录只有一个值,如果Age有值,则Salary将为NULL,反之亦然。

更新:

表2包含CreatedDate列。所以我想得到最新的" NOTNULL" CELL值而不是最大值。

4 个答案:

答案 0 :(得分:4)

您可以使用简单的MAX()GROUP BY

完成此操作
select t1.userid,t1.username, MAX(t2.Age) as Age, MAX(t2.Salary) as Salary
from table1 t1 join
     table2 t2 on t1.userid=t2.userid
group by t1.userid,t1.username

结果:

userid  username    Age   Salary
--------------------------------
1       foo         28    35000

SQL Fiddle

中的示例结果

答案 1 :(得分:3)

注意:我怀疑您知道自己在做什么,而且您还没有告诉我们您的架构的所有信息。

看起来Table 2实际上是"更新"表,其中每行包含要应用于Table 1中的基本实体的更改增量。在这种情况下,您可以使用相关联接(技术上是外部应用)检索每个列的数据并将结果放在一起。如下所示:

select a.UserID, a.UserName, 
    coalesce(aAge.Age, a.Age),
    coalesce(aSalary.Salary, a.Salary)
from [Table 1] a
outer apply (
    select Age
    from [Table 2] x
    where x.UserID = a.UserID 
    and x.Age is not null
    and not exists (
        select 1
        from [Table 2] y
        where x.UserID = y.UserID
        and y.Id > x.Id
        and y.Age is not null
    )
) aAge,
outer apply (
    select Salary
    from [Table 2] x
    where x.UserID = a.UserID 
    and x.Salary is not null
    and not exists (
        select 1
        from [Table 2] y
        where x.UserID = y.UserID
        and y.Id > x.Id
        and y.Salary is not null
    )
) aSalary

请注意我假设您Id中至少有一个Table 2列,每个插入都会单调增加。如果你有'#34;更改时间"列,使用它来获取最新的行,因为它更好。

答案 2 :(得分:2)

要根据CreatedDate获取最新值,您可以使用ROW_NUMBER过滤最新行。此处的分区基于UserID,其他列AgeSalary

SQL Fiddle

;WITH Cte AS(
    SELECT
        UserID,
        Age = MAX(Age),
        Salary = MAX(Salary)
    FROM(
        SELECT *, Rn = ROW_NUMBER() OVER(
                        PARTITION BY 
                            UserID, 
                            CASE 
                                WHEN Age IS NOT NULL THEN 1
                                WHEN  Salary IS NOT NULL THEN 2
                            END
                        ORDER BY CreatedDate DESC
                        )
        FROM Table2
    )t
    WHERE Rn = 1
    GROUP BY UserID
)
SELECT
    t.UserID,
    t.UserName,
    Age = ISNULL(c.Age, t.Age),
    Salary = ISNULL(c.Salary, t.Salary)
FROM Table1 t
LEFT JOIN Cte c
    ON t.UserID = c.UserID

答案 3 :(得分:0)

以下查询应该可以正常工作(在MSSQL中正常工作):

select a.userID,a.username,b.age,b.sal from <table1> a
inner join 
(select userID,MAX(age) age,MAX(sal) sal from <table2> group by userID) b
on a.userID=b.userID