这是我的面试问题 [英] it was my interview question

查看:104
本文介绍了这是我的面试问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有桌子
id |值
==== =======
1 | 100
1 | 200
2 | 300
4 | 50
4 | 100

我想要具有相似ID列的值的总和

意味着结果看起来像
ID值
== | ====
1 | 300
2 | 300
4 | 150

如何使用sql查询来实现
请给我解决方法
这是我的面试问题
提前感谢

There is table
id | value
==== =======
1 | 100
1 | 200
2 | 300
4 | 50
4 | 100

I want sum of values which having similar id column

means result look like
ID value
==| ====
1 | 300
2 | 300
4 | 150

How to do it with sql query
please give me solution
it was my interview question
Thanx in advance

推荐答案

尝试一下

Try this

SELECT
id,
value = sum(value)
FROM [table]
GROUP BY id



他们在测试您的是您对SQL中的 Aggregation 的理解.

SQL中有很多聚合方法. Avg, Sum, Max, Min等.

通常,您可以使用GROUP BY子句来判断查询中是否包含聚合.以下是向您介绍此子句的文章.

http://www.w3schools.com/sql/sql_groupby.asp [



What they were testing you on was your understanding of Aggregation in SQL.

The are lots of aggregation methods in SQL. Avg, Sum, Max, Min and more.

You can generally tell if a query has an aggregation in it by the use of the GROUP BY clause. The following is an article which introduces you to this clause.

http://www.w3schools.com/sql/sql_groupby.asp[^]




试试下面的代码块

Hi ,

try the following code block

SELECT ID, SUM(value) As Value FROM YourTable GROUP BY ID


谢谢


这篇关于这是我的面试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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