当前位置:网站首页>Leecode-SQL 1393. Capital Gains and Losses on Stocks
Leecode-SQL 1393. Capital Gains and Losses on Stocks
2022-08-07 21:32:00 【Windy Pro】

Two solutions, the first is subtle
An if solution
sum is the sum in the field, and the if tag is used to judge, if it is a buy, it will automatically become a negative sum, and if it is sold, it will be automatically added
select stock_name,sum(if(operation='Buy',-price,price))span> as capital_gain_lossfrom Stocksgroup by stock_nameThe other is case
case functionThe syntax of MySQL's case function is:1.Simple functionCASE [column_name] WHEN [value1] THEN[result1]... ELSE [default] ENDBe careful when querying multiple tables.CASE hero_nameWHEN 'Galen' THEN 'Top order'WHEN 'Lux' THEN 'Mid single'WHEN 'Jinx' THEN 'ADC'WHEN 'Piano Girl' THEN 'Auxiliary'WHEN 'Blind monk' THEN 'Jungle'ELSE 'Bitch' ENDThe effect is similar to if
select stock_name,sum(casewhen operation='buy' then -priceelse price end) as 'capital_gain_loss'from Stocksgroup by stock_name边栏推荐
猜你喜欢
随机推荐
The basics of database learning
【LeetCode】875. Keke who loves bananas
电脑内存怎么装 台式内存条怎么安装
现在网上开户安全么?想知道证券公司开户如何得到优惠?
服务网格(istio)istio流量管理
autocad2007激活码序列号_激活cad2007的方法
Unity 最新UnityWebRequest下载,同时显示下载进度,和 显示网速,今天贴出来和大家分享
The difference between Icon and Ico pictures
即将开幕!阿里云飞天技术峰会邀您一同探秘云原生最佳实践
Linux下centos7.9版本下安装mysql及遇见的一些问题
复印机扫描功能怎么用 复印机里面的扫描怎么使用
基金开户有什么风险?哪里办理基金开户最安全?
如何使用 ABAP 创建包含不同字体大小的 Word 文档
UE4 Sequence添加基础动画效果 (02-切换动作)
如何看懂万行代码
What risk fund accounts?Handle the fund account the safest?
软考 --- 软件工程(5)软件质量
golang切片slice详解:介绍、内存分析、定义、遍历、切片可以继续切片、切片可以动态增长、切片的拷贝及底层原理
golang方法的使用细节:参数默认是值拷贝,不仅仅是struct自定义数据类型也可以绑定方法、方法名称首字母大写为public权限、String()方法的使用
Is it safe to open an account with Big Wisdom?









