在没有行数改变的情况下,列的值匹配的总和

时间:2017-07-28 12:06:28

标签: sql sql-server

我正在尝试列的值,其他列的值匹配。以下是我的数据样本。

DT                      No_of_records    LD_VOY_N  LD_VSL_M
2017-05-06 04:00:00.000         7          0002W   pqo emzmnwp
2017-05-06 20:00:00.000         6          0002W   pqo emzmnwp
2017-05-02 04:00:00.000         1          0007E   omq ynzmeoyn
2017-05-01 08:00:00.000         2          0016W   rmhp sunhpnw
2017-05-01 12:00:00.000         1          0016W   rmhp sunhpnw
2017-05-05 12:00:00.000         2          0019N   omq wqmsy
2017-05-06 04:00:00.000         12         0019N   omq wqmsy

以下是我想要的输出

DT                      No_of_records    LD_VOY_N   LD_VSL_M      Total_no_of_records
2017-05-06 04:00:00.000         7          0002W    pqo emzmnwp   13
2017-05-06 20:00:00.000         6          0002W    pqo emzmnwp   13
2017-05-02 04:00:00.000         1          0007E    omq ynzmeoyn  1
2017-05-01 08:00:00.000         2          0016W    rmhp sunhpnw  3
2017-05-01 12:00:00.000         1          0016W    rmhp sunhpnw  3
2017-05-05 12:00:00.000         2          0019N    omq wqmsy     14
2017-05-06 04:00:00.000         12         0019N    omq wqmsy     14

我正在尝试查找Total_no_of_records列。你有什么想法?

2 个答案:

答案 0 :(得分:1)

您似乎想要LD_VOY_N的窗口功能:

select t.*,
       sum(No_of_records) over (partition by LD_VOY_N) as Total_no_of_records
from t;

答案 1 :(得分:0)

select DT,No_of_records,LD_VOY_N,LD_VSL_M ,COUNT(DISTINCT (DT,No_of_records,LD_VOY_N,LD_VSL_M )) as Total_no_of_records from tablename
group by DT,No_of_records,LD_VOY_N,LD_VSL_M