SQL Query不会重复

时间:2016-09-27 21:20:34

标签: sql-server

我有一个主表1,它有重复的FA_PERIOD 3.表1是CTE输出。我需要在字段OBJECT_ID,FA_YEAR和FA_PERIOD上加入表2到表1,但表1中的重复字段会因重复而导致问题。我试图将两个表结合在一起,在一个表中一起显示CTRY runtot和Running Total字段。感谢任何帮助。

Table 1.
OBJECT_ID, AMOUNT, FA_YEAR, FA_PERIOD, CTRY runtot
22203   259 2015    5      $257.00
22203   259 2015    6      $514.00
22203   259 2015    7      $771.00
22203   259 2015    8    $1,028.00
22203   259 2015    9    $1,285.00
22203   259 2015    10   $1,542.00
22203   259 2015    11   $1,799.00
22203   259 2015    12   $2,056.00
22203   259 2016    1    $2,313.00
22203   259 2016    2    $2,570.00
22203   259 2016    3    $2,827.00
22203   -2825   2016    3         $.00
22203   2825    2016    3    $2,827.00
22203   259 2016    4    $3,084.00


Table 2.
OBJECT_ID,FA_YEAR,FA_PERIOD,RunningTOTAL
    22203   2016    1      $257.00
    22203   2016    2      $514.00
    22203   2016    3      $771.00
    22203   2016    4    $1,028.00

1 个答案:

答案 0 :(得分:0)

我假设您想要在 Object_Id,FA_Year,FA_Period 级别获得运行总计。进行分组时,将删除重复项。

我已在下方放置示例代码以测试您的方案。

CREATE TABLE #Table1
([OBJECT_ID] int, AMOUNT int, FA_YEAR int, FA_PERIOD int, CTRY_runtot money)
insert into #Table1
values
(22203,   259, 2015,    5,      '$257.00'),
(22203,   259, 2015,    6 ,     '$514.00'),
(22203,   259, 2015,    7 ,     '$771.00'),
(22203,   259, 2015,    8 ,   '$1,028.00'),
(22203,   259, 2015,    9 ,   '$1,285.00'),
(22203,   259, 2015,    10,   '$1,542.00'),
(22203,   259, 2015,    11,   '$1,799.00'),
(22203,   259, 2015,    12,   '$2,056.00'),
(22203,   259, 2016,    1 ,   '$2,313.00'),
(22203,   259, 2016,    2 ,   '$2,570.00'),
(22203,   259, 2016,    3 ,   '$2,827.00'),
(22203,   2825,    2016,    3,    '$2,827.00'),
(22203,   -2825,   2016,    3,        '$.00');
CREATE TABLE #Table2
([OBJECT_ID] int, FA_YEAR int, FA_PERIOD int, runningtotal money)
insert into #Table2
values
    (22203,   2016,    1,      '$257.00'),
    (22203,   2016,    2,      '$514.00'),
    (22203,   2016,    3,      '$771.00'),
    (22203,   2016,    4,    '$1,028.00');


SELECT t1.[OBJECT_ID]
       ,t1.FA_YEAR    
       ,t1.FA_PERIOD  
       ,sum(t1.CTRY_runtot) as CTRY_runtot
       ,SUM(t2.runningtotal) as runningtotal
from #Table1 t1
JOIN #Table2 t2
ON  t1.[OBJECT_ID] =   t2.[OBJECT_ID]
AND t1.FA_YEAR     = t2.FA_YEAR
AND t1.FA_PERIOD   = t2.FA_PERIOD
GROUP BY 
t1.[OBJECT_ID]
       ,t1.FA_YEAR    
       ,t1.FA_PERIOD 

修改

根据对预期结果集的评论,更新查询。

SELECT [OBJECT_ID]
       ,FA_YEAR    
       ,FA_PERIOD  
       ,CTRY_runtot
       ,CASE WHEN rnk=1 THEN CAST(runningtotal as VARCHAR(10)) ELSE ''  END as RunningTotal
FROM 
(SELECT t1.[OBJECT_ID]
       ,t1.FA_YEAR    
       ,t1.FA_PERIOD  
       ,t1.CTRY_runtot
       ,t2.runningtotal
       ,ROW_NUMBER() over(PARTITION BY t1.[OBJECT_ID]
       ,t1.FA_YEAR    
       ,t1.FA_PERIOD ORDER BY t2.runningtotal) as rnk
       from #Table1 t1
JOIN #Table2 t2
ON  t1.[OBJECT_ID] =   t2.[OBJECT_ID]
AND t1.FA_YEAR     = t2.FA_YEAR
AND t1.FA_PERIOD   = t2.FA_PERIOD
       )as t