sql - SQL如何对唯一键对的列求和
问题描述
我是 SQL 新手,我不知道该怎么做。我想为类似对(vin,action)总结一个名为“total_spending”的库,并选择第一个dealer_name和参考月份年份(因此它不会创建重复项)并具有类似示例的输出:输入
action dealer_name vin Total_spending reference month year
A1 D1 V1 T1 R1 M1 Y1
A2 D2 V2 T2 R1 M1 Y1
A2 D2 V2 T3 R2 M2 Y2
A3 D2 V1 T4 R1 M1 Y1
A4 D1 V2 T5 R1 M1 Y1
A2 D1 V2 T6 R1 M1 Y1
A1 D1 V1 T7 R2 M2 Y2
A4 D1 V2 T8 R2 M2 Y2
A1 D1 V1 T9 R3 M3 Y3
A3 D2 V2 T10 R1 M1 Y1
A3 D2 V1 T11 R2 M2 Y2
输出
action dealer_name vin Total_spending reference month year
A1 D1 V1 T1 + T7 + T9 R1 M1 Y1
A2 D2 V2 T2 + T3 R1 M1 Y1
A3 D2 V1 T4 + T11 R1 M1 Y1
A4 D1 V2 T5 + T8 R1 M1 Y1
A2 D1 V2 T6 R1 M1 Y1
A3 D2 V2 T10 R1 M1 Y1
解决方案
你可以试试下面 -
select action,dealer_name,vin,sum(Total_spending) as total_spending, reference ,month, year
from tablename
group by action, dealer_name , vin,reference ,month, year
推荐阅读
- python - 将数据框与映射中的多级列合并
- r - dplyr 在 mutate 中同时使用 rowwise 和 df-wise 值
- ios - iOS Swift 中的 ARKit 导航?
- javascript - 嵌入 YouTube 视频作为背景,没有广告
- qt - 不同事件的 Qt Quick 布局更改
- bash - 用于执行 linux 文件的 Bash 脚本
- javascript - SweetAlert2 : 单击按钮后基于 div 的选择执行“函数”或“异步函数”
- java - 通过从具有不同对象的重复元素的列表中删除对象来创建新的不同列表
- pytorch - 使用 PyTorch 运行单个预测的问题
- laravel - Laravel 中的授权策略参数错误