【问题标题】:Select max data with 3 tables选择 3 个表的最大数据
【发布时间】:2016-03-24 02:43:32
【问题描述】:

我无法得到正确的结果。 我有 3 张桌子:

table: Aluno
id_aluno    nome
1           Bruno
2           Carlos

table: Serie
id_serie    id_aluno    descricao
1           1           Tipo A
2           1           Tipo B
3           2           Tipo A

table: Treino
id_treino   id_serie    data
1           1           2015-12-10
2           2           2015-12-12
3           3           2015-12-10

我想要以下结果:

nome     descricao    data
Bruno    TIPO B       2015-12-12
Carlos   TIPO A       2015-12-10       

问题是 GROUP BY 子句应该有列“id_aluno”,但它不是具有日期的表的外键。它们之间有一个中间表。我不知道该怎么做。 我希望你能帮助我。

【问题讨论】:

  • 抱歉,请选择最长日期
  • 在问题中包含 SQL 是个好主意,即使它不太有效。

标签: sql date select max


【解决方案1】:

您可以根据现有键加入表格,但您需要指定仅基于人员的最大日期,如下所示:

SELECT
    a.nome,
    s.descricao,
    t.data
FROM Aluno a
    JOIN Serie s
        ON s.id_aluno = a.id_aluno
    JOIN Treino t
        ON t.id_serie = s.id_serie
WHERE t.data = ( --get max date by person, excluding serie
                SELECT MAX(t1.data)
                FROM Aluno a1
                JOIN Serie s1
                    ON s1.id_aluno = a1.id_aluno
                JOIN Treino t1
                    ON t1.id_serie = s1.id_serie
                WHERE s1.id_aluno = s.id_aluno 
               )

【讨论】:

  • 使用此查询确保Treino 中有唯一的data 值,否则结果集中可能存在重复。
【解决方案2】:

使用这个查询

Select a.nome, s.descricao, t.data
FROM Aluno a
join Serie s on (s.id_aluno = a.id_aluno)
join Treino t on (t.id_serie = s.id_serie)

【讨论】:

    【解决方案3】:

    您可以在From 表的任何列上使用Group By,包括那些通过Join 链接的列。您可以使用Join 链接任意数量的表。

    Select Aluno.nome, Serie.descricao, Max(Treino.data) As data
    From Aluno
    Inner Join Serie On Serie.id_aluno = Aluno.id_aluno
    Inner Join Treino On Treino.id_serie = Serie.id_serie
    Group By Aluno.nome, Serie.descricao
    

    好的,考虑到 MS SQL 的注释,它可以这样写:

    Select nome, descricao, data
    From (
        Select Aluno.nome, Serie.descricao, Treino.data,
            RANK() OVER (Partition By Aluno.nome Order By Treino.data Desc) AS Ordinal
        From Aluno
        Inner Join Serie On Serie.id_aluno = Aluno.id_aluno
        Inner Join Treino On Treino.id_serie = Serie.id_serie
    ) Ranked
    Where Ordinal = 1
    

    顺便说一句,如果你在 Treino 有另一条记录会发生什么: 4, 1, 2015-12-125, 2, 2015-12-12 就此事? 你也需要一种方法来优先考虑意甲。就这样说吧:

    Select nome, descricao, data
    From (
        Select Aluno.nome, Serie.descricao, Treino.data,
            ROW_NUMBER() OVER (Partition By Aluno.nome Order By Treino.data Desc, Serie.descricao) AS Ordinal
        From Aluno
        Inner Join Serie On Serie.id_aluno = Aluno.id_aluno
        Inner Join Treino On Treino.id_serie = Serie.id_serie
    ) Ranked
    Where Ordinal = 1
    

    【讨论】:

    • 我已经尝试过了,但是它返回了每个系列的所有最大日期(因为它在 group by 子句中),我需要每个 aluno 的所有最大日期。
    【解决方案4】:

    这里我使用了多个公用表表达式。

    脚本

    WITH CTE
    AS
    (
        SELECT TOP 2 id_Treino,id_serie,data
        FROM Treino
        ORDER BY DATA DESC
    ), 
    cte2 
    as
    (
        SELECT a.nome,s.descricao,c.data
        FROM aluno AS a
        INNER JOIN Serie AS s
        ON a.id_aluno = s.id_aluno
        INNER JOIN cte AS c
        ON s.id_serie = c.id_serie
    )
    SELECT * FROM cte2;
    

    输出

    nome        descricao       DATA
    ------------------------------
    Bruno       Tipo B      2015-12-12
    Carlos      Tipo A      2015-12-10
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      相关资源
      最近更新 更多