0

我有以下问题:

我有一个视图,其中包含 id、month_date、max_month_date 和余额。我现在想做一个自我加入,其中 id 与 id 进行比较,max_month_date 与month_date 进行比较。因此,只有具有最大 month_date 的行应写入新视图。这是起点:

id     month_date    max_month_date     balance 
1      2020-01-12    2020-01-31         100
1      2020-01-16    2020-01-31         150
1      2020-01-31    2020-01-31         200
1      2020-02-03    2020-02-23         100
1      2020-02-23    2020-02-23         150
1      2020-02-23    2020-02-23         -150

因此,我的加入是:

SELECT t.main_id,
   t.month_date,
   t.max_month_date,
   t.balance
FROM test_view t
INNER JOIN test_view s 
ON t.main_id = s.main_id
AND t.month_date = s.max_month_date

问题是有些行具有相同的 id 和相同的 max_month_date。余额不一样。由于这个原因,输出中写入了太多行。

从理论的角度来看,我必须将所有具有相同 id 和相同 max_month_date 的余额相加。在上述情况下,这适用于最后两行:

    id     month_date    max_month_date     balance
    1      2020-02-23    2020-02-23         0 

然后必须在主视图中删除重复的数据记录,并且只填充构成余额总数的行。像这样:

    id     month_date    max_month_date     balance
    1      2020-01-12    2020-01-31         100
    1      2020-01-16    2020-01-31         150
    1      2020-01-31    2020-01-31         200
    1      2020-02-03    2020-02-23         100
    1      2020-02-23    2020-02-23         0 

有没有人有想法可以帮助我?

提前致谢

4

3 回答 3

0

你只需要一个WHERE子句:

SELECT t.main_id, t.month_date, t.max_month_date,  t.balance
FROM test_view t
WHERE t.month_date = t.max_month_date;

没有JOIN是必要的。

如果视图没有max_month_date,那么您可以使用窗口函数来计算它:

SELECT t.main_id, t.month_date, t.max_month_date,  t.balance
FROM (SELECT t.*,
             MAX(month_date) OVER (PARTITION BY main_id) as max_month_date
      FROM test_view t
     ) t
WHERE t.month_date = t.max_month_date;
于 2020-08-12T11:12:43.827 回答
0

下面试试。COUNT窗口函数确保将相同的值分组在一起,无论是负数还是正数。然后,如果计数 >1 sum BALANCEcolumn grouping BY MAIN_ID,MONTH_DATE,MAX_MONTH_DATEelse BALANCE

工作解决方案

在此处输入图像描述

WITH TEST_VIEW AS (
SELECT 1 MAIN_ID,     CONVERT(DATETIME,'2020-01-12',102) AS MONTH_DATE,    CONVERT(DATETIME,'2020-01-31',102) AS MAX_MONTH_DATE,         100  AS balance  UNION ALL
SELECT 1 MAIN_ID,     CONVERT(DATETIME,'2020-01-16',102) AS MONTH_DATE,    CONVERT(DATETIME,'2020-01-31',102) AS MAX_MONTH_DATE,         150  AS balance  UNION ALL
SELECT 1 MAIN_ID,     CONVERT(DATETIME,'2020-01-31',102) AS MONTH_DATE,    CONVERT(DATETIME,'2020-01-31',102) AS MAX_MONTH_DATE,         200  AS balance  UNION ALL
SELECT 1 MAIN_ID,     CONVERT(DATETIME,'2020-02-03',102) AS MONTH_DATE,    CONVERT(DATETIME,'2020-02-23',102) AS MAX_MONTH_DATE,         100  AS balance  UNION ALL
SELECT 1 MAIN_ID,     CONVERT(DATETIME,'2020-02-23',102) AS MONTH_DATE,    CONVERT(DATETIME,'2020-02-23',102) AS MAX_MONTH_DATE,         150  AS balance  UNION ALL
SELECT 1 MAIN_ID,     CONVERT(DATETIME,'2020-02-23',102) AS MONTH_DATE,    CONVERT(DATETIME,'2020-02-23',102) AS MAX_MONTH_DATE,         -150 AS balance  
)
SELECT DISTINCT
       T.MAIN_ID,
       T.MONTH_DATE,
       T.MAX_MONTH_DATE,
       CASE
          WHEN BALC > 1
          THEN
             SUM (BALANCE)
                OVER (PARTITION BY T.MAIN_ID, T.MONTH_DATE, T.MAX_MONTH_DATE)
          ELSE
             BALANCE
       END
          SUMM
  FROM (SELECT T.MAIN_ID,
               T.MONTH_DATE,
               T.MAX_MONTH_DATE,
               T.BALANCE,
               COUNT (*)
                  OVER (PARTITION BY T.MAIN_ID,
                                     T.MONTH_DATE,
                                     T.MAX_MONTH_DATE,
                                     ABS (T.BALANCE))
                  BALC
          FROM TEST_VIEW T) T
于 2020-08-12T09:29:59.517 回答
0

尝试使用 SQL DISTINCT

SELECT DISTINCT t.main_id,
t.month_date,
t.max_month_date,
t.balance
FROM test_view t
INNER JOIN test_view s 
ON t.main_id = s.main_id
AND t.month_date = s.max_month_date
于 2020-08-12T10:02:12.993 回答