如何通过条件检索列值的总和.使用sql查询. [英] How to retrieve sum of a column values by a condition. using sql query.

查看:107
本文介绍了如何通过条件检索列值的总和.使用sql查询.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表主,其属性为:ID,名称,金额,日期

在2010年4月5日,我将数据输入为(100,''Albert'',50000,''04/05/2010'')
在2010年5月5日,我将数据输入为(101,``Perera'',20000,``05/05/2010'')
在06/05/2010上,我将数据输入为(105,''Dixon'',50000,''06/05/2010'')
在2010年7月5日,我将数据输入为(100,''Albert'',80000,''07/05/2010'')
在08/05/2010中,我将数据输入为(105,''Dixon'',10000,''08/05/2010'')
在2010年9月5日,我将数据输入为(100,''Albert'',90000,''09/05/2010'')
在10/05/2010上,我输入的数据为(100,''Albert'',20000,''10/05/2010'')

我需要以下结果

ID名称金额

100艾伯特240000
101 Perara 20000
105 Dixon 60000

I have a table master with attributes:ID,name,amount,date

on 04/05/2010 I entered the the data as (100,''Albert'',50000,''04/05/2010'')
on 05/05/2010 I entered the the data as (101,''Perera'',20000,''05/05/2010'')
on 06/05/2010 I entered the the data as (105,''Dixon'',50000,''06/05/2010'')
on 07/05/2010 I entered the the data as (100,''Albert'',80000,''07/05/2010'')
on 08/05/2010 I entered the the data as (105,''Dixon'',10000,''08/05/2010'')
on 09/05/2010 I entered the the data as (100,''Albert'',90000,''09/05/2010'')
on 10/05/2010 I entered the the data as (100,''Albert'',20000,''10/05/2010'')

I required the result as below

Id Name Amount

100 Albert 240000
101 Perara 20000
105 Dixon 60000

推荐答案

您需要使用分组依据"子句. :)
在这里,你去:
You need to use "Group By" clause. :)
Here you go :
select ID,name,sum(amount) from master group by ID,name


Select ID, name, SUM(amount)
From myTable
Where date between ('04/05/2010') and ('10/05/2010')
Group By ID, name



请买一本书并阅读.它将帮助您学习新事物和发展.



Please buy a book and read. It will help you in learning new things and development.


这篇关于如何通过条件检索列值的总和.使用sql查询.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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