内部查询中的最大值(日期)

时间:2019-06-10 19:04:37

标签: sql db2

我得到的示例SQL似乎无法满足我的需求。

大表有400万行,小表有60万行。

/ *示例代码:(我由一位高级分析师提供了此示例)* /

SELECT SUM(BigTable.VALUE)
        FROM BigTable INNER JOIN SmallTable
        WHERE BigTable.ID = SmallTable.ID
        AND BigTable.VALUATION_DATE IN 
                (SELECT MAX(VALUATION_DATE)
                FROM BigTable)
       GROUP BY BigTable.ID

/ *我的代码:(我在内部查询中放置了一个WHERE)* /

SELECT BigTable.ID, SUM(BigTable.VALUE)
        FROM BigTable INNER JOIN SmallTable
        WHERE BigTable.ID = SmallTable.ID
        AND BigTable.VALUATION_DATE IN 
                (SELECT MAX(VALUATION_DATE)
                FROM BigTable INNER JOIN SmallTable
                    WHERE BigTable.ID = SmallTable.ID)
       GROUP BY BigTable.ID

如果ID xyz在最近的日期具有三个分别值为$ 1,$ 2,$ 3的帐户,我想返回该日期所有帐户的总和:xyz,$ 6

3 个答案:

答案 0 :(得分:0)

因此,我认为您使用的INNER JOIN语法不正确。在将要连接的INNER JOIN表之后,您需要在ON上声明希望将表连接到哪些列。

以下查询是正确语法(尽管可能不适用于您的实现)。

SELECT BigTable.ID, SUM(BigTable.VALUE)
        FROM BigTable INNER JOIN SmallTable
        ON BigTable.ID = SmallTable.ID
        WHERE BigTable.VALUATION_DATE IN 
                (SELECT MAX(VALUATION_DATE)
                FROM BigTable INNER JOIN SmallTable
                    ON BigTable.ID = SmallTable.ID)
       GROUP BY BigTable.ID

仅当您执行交叉联接自然联接时,才不使用ON关键字,而仅使用WHERE命令。

答案 1 :(得分:0)

您应避免使用where子句,而应使用ON子句

SELECT SUM(BigTable.VALUE)
FROM BigTable 
INNER JOIN SmallTable ON  BigTable.ID = SmallTable.ID
AND BigTable.VALUATION_DATE =  (
    SELECT MAX(VALUATION_DATE)
    FROM BigTable)

并且您不应该使用ID为..

的分组

答案 2 :(得分:0)

使用窗口功能:

SELECT b.ID, b.VALUE
FROM (SELECT b.*,
             ROW_NUMBER() OVER (PARTITION BY b.id ORDER BY b.VALUATION_DATE DESC) as seqnum
      FROM BigTable b
     ) b JOIN
     SmallTable s
     ON b.ID = s.ID
WHERE b.seqnum = 1;

我认为聚合不是必需的。但是,如果您在同一日期具有相同ID的多个值,则:

SELECT b.ID, SUM(b.VALUE)
FROM (SELECT b.*,
             RANK() OVER (PARTITION BY b.id ORDER BY b.VALUATION_DATE DESC) as seqnum
      FROM BigTable b
     ) b JOIN
     SmallTable s
     ON b.ID = s.ID
WHERE b.seqnum = 1
GROUP BY b.id;