2015-11-20 208 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等替換實際的列名稱......非常感謝您回答我的問題。