SQL查询汇总数据 [英] SQL query to sum the data

查看:102
本文介绍了SQL查询汇总数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格数据如下

TaxTypeCode1   TaxTypeCode2  PNO   Amount 
-----------------------------------------
TX01           TX02           124     600
TX02           null           124     700 
TX03           TX04           124     200 
TX04           null           124     300
TX05           TX06           126     400 
TX06           null           127     500 
TX07           null           128     800 

我想编写SQL查询来检索数据。

I would like to write SQL query to retrieve data.

如果pno相同,则条件适用和TaxTypeCode1包含TaxTypeCode2,然后对amt求和,否则显示实际的amt

Conditions apply IF pno is same and TaxTypeCode1 contain TaxTypeCode2 then sum the amt, otherwise display actual amt

我的预期输出是

PNO      Amount 
---------------
 124     1300
 124      500
 126      400
 127      500
 128      800

124具有1300,因为pno相同,并且TaxTypeCode2(TX02)Tax TypeCode1(TX02)相同,然后相加

124 has 1300 because pno is same and TaxTypeCode2 (TX02) TaxTypeCode1 (TX02) are same then sum

TX01           (TX02)           124     600
(TX02)           null           124     700 

126有400,因为pno不同,而TaxTypeCode2(TX02)TaxTypeCode1(TX02)相同不要求和

126 has 400 because pno is different and TaxTypeCode2 (TX02) TaxTypeCode1 (TX02) are same don't sum

TX05           (TX06)           (126)     400 
(TX06)           null           (127)     500

有人可以告诉如何编写查询来检索该数据吗?

Can anyone tell how to write query to retrieve that data?

推荐答案

SELECT PNO,SUM(Amount)
FROM YOURTABLE
GROUP BY PNO;

这篇关于SQL查询汇总数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆