列值匹配时的 SQL sum 字段 [英] SQL sum field when column values match

查看:21
本文介绍了列值匹配时的 SQL sum 字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题很难说,但问题很简单.我在这里搜索了所有内容,但找不到针对我的特定问题的内容,所以就在这里.我使用的是 Microsoft SQL Server Management Studio 2010.

The title was hard to word but the question is pretty simple. I searched all over here and could not find something for my specific issue so here it is. I'm usuing Microsoft SQL Server Management Studio 2010.

表格目前看起来像这样

  | Value | Product Name|
  |  300  | Bike        |
  |  400  | Bike        |
  |  300  | Car         |
  |  300  | Car         |

我需要表格来显示产品名称匹配的值的总和 - 像这样

I need the table to show me the sum of Values where Product Name matches - like this

  | TOTAL | ProductName |
  |  700  | Bike        |
  |  600  | Car         |

我试过一个简单的

 SELECT
      SUM(Value) AS 'Total'
      ,ProductName
 FROM TableX

但是上面的方法不起作用.我最终得到了列中所有值的总和.如何根据产品名称匹配求和?

But the above doesn't work. I end up getting the sum of all values in the column. How can I sum based on the product name matching?

谢谢!

推荐答案

SELECT SUM(Value) AS 'Total', [Product Name]
FROM TableX  
GROUP BY [Product Name]

SQL 小提琴示例

这篇关于列值匹配时的 SQL sum 字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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