比较 SQL 和 SUM [英] Comparing in SQL and SUM

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

问题描述

我实在想不出这个问题的好标题,但我有一个问题,我相信你可以帮我解决!

I really couldn't figure out a good title for this question, but I have a problem that I'm sure you can help me with!

我有一个查询输出如下:

I have a query which outputs something like this:

Month | Year | Subcategory | PrivateLabel | Price
-------------------------------------------------
  1   | 2010 | 666         | No           | -520
  1   | 2010 | 666         | No           | -499,75
  1   | 2010 | 666         | No           | -59,95
  1   | 2010 | 666         | No           | -49,73
  1   | 2010 | 666         | No           | -32,95

我想对价格进行SUM,因为所有其他数据都相同.我以为我可以用 SUMGROUP BY 做到这一点,但我不知道如何去做,或者至少它没有输出正确的结果.

I want to SUM on the price because all the other data is the same. I thought I could do this with SUM and GROUP BY, but I can't figure out how to do it or at least it doesn't output the right result.

如果有帮助,查询是两个表之间的内连接.

The query is an inner join between two tables, if that helps.

推荐答案

select
   month
   ,year
   ,subcategory
   ,privatelabel
   ,sum(price) as [total sales]
from
   a inner join b ...
where
   any where clauses 
group by
   month
   ,year
   ,subcategory
   ,privatelabel

如果我理解正确的话应该可以工作.. select 中的每个列都需要是 group by 的一部分或组中所有行的聚合函数

should work if i am understanding you correctly.. every colum in the select either needs to be part of the group by or an aggregate function on all rows in the group

添加了一个小提琴......主要是因为我不知道他的文本到 DDL 功能并想测试它;-)(感谢 Michael Buen)

added a fiddle.. mainly as i didn't know about he text to DDL functionality and wanted to test it ;-) (thanks Michael Buen)

http://sqlfiddle.com/#!3/35c1c/1

注意 where 子句是一个占位符..

note the where clause is a place holder..

这篇关于比较 SQL 和 SUM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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