我已经尝试了几个我的问题的解决方案在网站上,但找不到一个工作。请帮忙! 除了对report_names采取一些自由之外,数据对于我正在尝试完成的事情是现实的,并且仅仅是我所反对的一小部分,大约97K行数据具有相同类型的分支重复,file_count, report_name ...文件号码是唯一的并且不重要。这是为了我的问题的信息目的,并解释了为什么金额是唯一的 - 他们绑定到file_name 我正在寻找一个report_name与两个金额的总和。使用多个聚合函数 - 总数和计数
以下是当前结果我的查询:
branch file_count file_volume net_profit report_name file_number
Northeast 1 $200,000.00 $200,000.00 bogart.hump.new 12345
Northeast 1 $195,000.00 $197,837.00 bogart.hump.new 23456
Northeast 1 $111,500.00 $113,172.00 bogart.hump.new 34567
Northwest 1 $66,000.00 -$1,500.18 jolie.angela.new 45678
Northwest 1 $159,856.00 -$2,745.58 jolie.angela.new 56789
Northwest 1 $140,998.00 -$2,421.69 jolie.angela.new 67890
Southwest 1 $74,000.00 $73,904.00 Man.bat.net 78901
Southwest 1 $186,245.00 -$4,231.25 Man.bat.net 89012
Southwest 1 $72,375.00 $73,641.00 Man.bat.net 9
Southeast 1 $79,575.00 -$1,821.76 zep.led.new 1234A
Southeast 1 $268,600.00 $268,600.00 zep.led.new 2345A
Southeast 1 $77,103.00 -$1,751.68 zep.led.new 3456A
这就是我要找:
branch file_count file_volume net_profit report_name file_number
Northeast 3 $506,500.00 $511,009.00 bogart.hump.new
Northwest 3 $366,854.00 -$6,667.45 jolie.angela.new
Southwest 3 $332,620.00 $143,313.75 Man.bat.net
Southeast 3 $425,278.00 $265,026.56 zep.led.new
我的查询:
SELECT
branch,
count(filenumber) AS file_count,
sum(fileAmount) AS file_amount,
sum(netprofit*-1) AS net_profit,
concat(d2.lastname,'.',d2.firstname,'.','new') AS report_name,
FROM user.summary u
inner join user.db1 d1 ON d1.loaname = u.loaname
inner join user.db2 d2 ON d2.cn = u.loaname
WHERE d2.filedate = '2015-09-01'
AND filenumber is not null
GROUP BY branch,concat(d2.lastname,'.',d2.firstname,'.','new')
什么问题和问题是什么? –
为什么不总和(netprofit)* -1 AS net_profit' ..一切都看起来不错..有一种感觉,你留下一些信息,因为你的查询不符合你当前的结果 – JamieD77
为什么偶乘以-1在所有?看起来对我来说是一个简单的总结。除此之外,一切看起来都应该给你想要的结果。 – xQbert