2015-11-20 210 views
0

我已经尝试了几个我的问题的解决方案在网站上,但找不到一个工作。请帮忙! 除了对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') 
+3

什么问题和问题是什么? –

+0

为什么不总和(netprofit)* -1 AS net_profit' ..一切都看起来不错..有一种感觉,你留下一些信息,因为你的查询不符合你当前的结果 – JamieD77

+0

为什么偶乘以-1在所有?看起来对我来说是一个简单的总结。除此之外,一切看起来都应该给你想要的结果。 – xQbert

回答

0

唯一的问题我见与您目前的查询是,你有一个逗号在这一行的末尾,会给你一个语法错误:

concat(d2.lastname,'.',d2.firstname,'.','new') AS report_name, 

如果你想在虽然设置你想要的结果显示领域的空白file_number,你可以离开了逗号,并通过增加其与空白字段遵循它:

concat(d2.lastname,'.',d2.firstname,'.','new') AS report_name, 
'' file_number 
0

我想通但如果不在这个论坛上发表意见,就无法做到这一点。在我的实际查询中,我包含了“file_name”列,所以我有“count(file_name)”和“file_name”列......但在我的查询示例中,我只有“count(file_name)”柱。当我从我的实际查询中删除“file_column”列时,它工作正常。附注......很明显,我在查询中排除了关键组件。对于任何未来的查询问题,我将包括完整的查询,但用col1,col2,db1,db2等替换实际的列名称......非常感谢您回答我的问题。