如何使用SQL查询获取总小计 [英] How do I get total subtotal using SQL query

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

问题描述

我有一张桌子叫Table1

有两个colom

名字 - varchar(50)

得分 - Float



我需要根据名字的小计

如果名字出现超过一次,我需要他们的总得分



例如

名称分数

n 1.0000

n 2.0000

b 6.0000



我需要结果



名称分数

n 3.0000

b 6.0000





简而言之,我需要来自它的夏季!!



我尝试过:



I have one table Called Table1
have two colom
name - varchar(50)
score - Float

I need subtotal of according to the name
if name occoured more than one time than I need their total of score

for example
Name Score
n 1.0000
n 2.0000
b 6.0000

I need result as

Name Score
n 3.0000
b 6.0000


In short I need summery from it !!

What I have tried:

select Name, Score
from (
    select Name, Score, Name as o
    from Table1 as a
    union all
    select 'Subtotal', sum(Score), Name as o
    from Table1 as a
    group by Name
) as a
order by o, score

推荐答案

尝试:

Try:
SELECT [Name], SUM(Score) FROM Table1
GROUP BY [Name]

但是......我强烈建议您在表格中添加ID列,让它成为身份。如果不这样做,SQL将不允许您输入另一行n,1.0000,因为它已经存在。

But ... I'd strongly suggest you add an ID column to your table, and make it IDENTITY. If you don't, SQL will not allow you to enter another row "n, 1.0000" as it already exists.


这篇关于如何使用SQL查询获取总小计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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