2016-03-07 87 views
-1

這是從我的數據庫SQL爲特定工作創建美元金額的總和?

Job 1 $3780.00 
Job 1 $15187.50 
Job 1 $8155.00 
Job 2 $1485.00 
Job 2 $8156.00 
Job 3 $21642.00 
Job 3 $15620.47 
... 

這是目前用來拉出來的數據查詢(編輯以保密)的一些樣本數據。我需要喬布斯添加子查詢縮小到獨特的價值觀和總結所有一個作業相關聯成一個總價值金額的

SELECT FN.full_name As [Job], transaction_date, Set1.Due_Date As [Due Date],(CASE WHEN SUM(amount_amt) > 0 THEN SUM(amount_amt) ELSE 1 END) AS [Amount], (SUM(amount_amt))-Set1.Invoice_amt AS [Balance] FROM 
(((SELECT T.due_date, T.customer_id, T.transaction_id, sum(ISNULL(amount_amt_sum,0)) As Invoice_amt FROM 
(SELECT estimate.due_date, estimate.transaction_date, estimate.customer_id, estimate.transaction_id, link.transaction_2_id, (SELECT SUM(invoice.amount_amt) FROM invoice WHERE link.transaction_2_id=invoice.transaction_id AND invoice.doc_num IS NOT NULL AND invoice.doc_num<>'' AND invoice._amount_amt>0) AS amount_amt_sum FROM estimate LEFT JOIN link ON estimate.transaction_id=link.transaction_1_id GROUP BY estimate.due_date, estimate.transaction_date, estimate.customer_id, estimate.transaction_id, link.transaction_2_id) AS t GROUP BY due_date, customer_id, transaction_id) AS Set1 LEFT JOIN estimate ON Set1.id=estimate.id) LEFT JOIN customer_fullname as FN ON Set1.customer_id=FN.id) GROUP BY Set1.Invoice_amt, Set1.Due_Date, FN.full_name, FN.name, transaction_date Having (MAX(class_id)=1 Or MAX(class_id)=2 Or MAX(class_id)=6 Or MAX(class_id)=7 Or MAX(class_id)=8 Or MAX(class_id)=9 Or MAX(class_id)=10 Or MAX(class_id)=11) 

我不是在SQL偉大,不知道在哪裏插入我的子查詢來獲得我需要的。請幫助?

+0

爲什麼MySQL的標籤? – Strawberry

+0

我不知道它是什麼樣的SQL –

回答

0
SELECT FN.full_name As [Job], transaction_date, Set1.Due_Date As [Due Date], 
(CASE WHEN SUM(amount_amt) > 0 THEN SUM(amount_amt) ELSE 1 END) AS [Amount], 
(SUM(amount_amt))-Set1.Invoice_amt AS [Balance] 
FROM 
(((SELECT T.due_date, T.customer_id, T.transaction_id, 
sum(ISNULL(amount_amt_sum,0)) As Invoice_amt FROM 
(SELECT estimate.due_date, estimate.transaction_date, estimate.customer_id, 
estimate.transaction_id, link.transaction_2_id, (SELECT SUM(invoice.amount_amt) 
FROM invoice WHERE link.transaction_2_id=invoice.transaction_id AND 
invoice.doc_num IS NOT NULL AND invoice.doc_num<>'' AND invoice._amount_amt>0) 
AS amount_amt_sum FROM estimate LEFT JOIN link ON 
estimate.transaction_id=link.transaction_1_id GROUP BY estimate.due_date, 
estimate.transaction_date, estimate.customer_id, estimate.transaction_id, 
link.transaction_2_id) AS t GROUP BY due_date, customer_id, transaction_id) AS Set1 LEFT JOIN estimate ON Set1.id=estimate.id) LEFT JOIN customer_fullname as 
FN ON Set1.customer_id=FN.id) GROUP BY Set1.Invoice_amt, Set1.Due_Date, 
FN.full_name, FN.name, transaction_date Having (MAX(class_id)=1 Or 
MAX(class_id)=2 Or MAX(class_id)=6 Or MAX(class_id)=7 Or MAX(class_id)=8 Or 
MAX(class_id)=9 Or MAX(class_id)=10 Or MAX(class_id)=11) 

Whew .. now that formatting done ..你需要一個Group By子句和一個和。所以......最簡單的方法來做到這一點,儘管可能不是最優的(我想到了很多該查詢可以優化)

Select Job, Sum(Amount) TotalAmount From 
(
    SELECT FN.full_name As [Job], transaction_date, Set1.Due_Date As [Due Date], 
    (CASE WHEN SUM(amount_amt) > 0 THEN SUM(amount_amt) ELSE 1 END) AS [Amount], 
    (SUM(amount_amt))-Set1.Invoice_amt AS [Balance] 
    FROM 
    (((SELECT T.due_date, T.customer_id, T.transaction_id, 
    sum(ISNULL(amount_amt_sum,0)) As Invoice_amt FROM 
    (SELECT estimate.due_date, estimate.transaction_date, estimate.customer_id, 
    estimate.transaction_id, link.transaction_2_id, (SELECT SUM(invoice.amount_amt) 
    FROM invoice WHERE link.transaction_2_id=invoice.transaction_id AND 
    invoice.doc_num IS NOT NULL AND invoice.doc_num<>'' AND invoice._amount_amt>0) 
    AS amount_amt_sum FROM estimate LEFT JOIN link ON 
    estimate.transaction_id=link.transaction_1_id GROUP BY estimate.due_date, 
    estimate.transaction_date, estimate.customer_id, estimate.transaction_id, 
    link.transaction_2_id) AS t GROUP BY due_date, customer_id, transaction_id) AS Set1 LEFT JOIN estimate ON Set1.id=estimate.id) LEFT JOIN customer_fullname as 
    FN ON Set1.customer_id=FN.id) GROUP BY Set1.Invoice_amt, Set1.Due_Date, 
    FN.full_name, FN.name, transaction_date Having (MAX(class_id)=1 Or 
    MAX(class_id)=2 Or MAX(class_id)=6 Or MAX(class_id)=7 Or MAX(class_id)=8 Or 
    MAX(class_id)=9 Or MAX(class_id)=10 Or MAX(class_id)=11) 
) as subquery group by Job 

這應該做的伎倆..

編輯我忘了你必須命名子查詢。查詢編輯顯示此。

編輯2考慮了一下,在您的查詢仔細看看,你是分組在外部查詢已經如此,我們也許能夠把事情簡單化了一點,但我肯定會覈實的款項,以確保一切都是正確的分組..如果你不熟悉它,分組在一堆不同的列上可能會有意想不到的結果。我個人可以這樣說,我只是在做這件事而已。下面是你將如何擴展我原來的帖子來處理多個領域。正如我在下面提到的那樣,在多列上進行分組時,您需要非常小心,因爲在此過程中您可以很容易地獲得意想不到的結果。

Select Job,transaction_date, FN.name, FN.full_name, Set1.Due_Date, Sum(Amount) TotalAmount From 
(
    SELECT FN.full_name As [Job], transaction_date, Set1.Due_Date As [Due Date], 
    (CASE WHEN SUM(amount_amt) > 0 THEN SUM(amount_amt) ELSE 1 END) AS [Amount], 
    (SUM(amount_amt))-Set1.Invoice_amt AS [Balance] 
    FROM 
    (((SELECT T.due_date, T.customer_id, T.transaction_id, 
    sum(ISNULL(amount_amt_sum,0)) As Invoice_amt FROM 
    (SELECT estimate.due_date, estimate.transaction_date, estimate.customer_id, 
    estimate.transaction_id, link.transaction_2_id, (SELECT SUM(invoice.amount_amt) 
    FROM invoice WHERE link.transaction_2_id=invoice.transaction_id AND 
    invoice.doc_num IS NOT NULL AND invoice.doc_num<>'' AND invoice._amount_amt>0) 
    AS amount_amt_sum FROM estimate LEFT JOIN link ON 
    estimate.transaction_id=link.transaction_1_id GROUP BY estimate.due_date, 
    estimate.transaction_date, estimate.customer_id, estimate.transaction_id, 
    link.transaction_2_id) AS t GROUP BY due_date, customer_id, transaction_id) AS Set1 LEFT JOIN estimate ON Set1.id=estimate.id) LEFT JOIN customer_fullname as 
    FN ON Set1.customer_id=FN.id) GROUP BY Set1.Invoice_amt, Set1.Due_Date, 
    FN.full_name, FN.name, transaction_date Having (MAX(class_id)=1 Or 
    MAX(class_id)=2 Or MAX(class_id)=6 Or MAX(class_id)=7 Or MAX(class_id)=8 Or 
    MAX(class_id)=9 Or MAX(class_id)=10 Or MAX(class_id)=11) 
) as subquery group by Job, transaction_date, FN.name, FN.full_name, Set1.Due_Date 

您可以通過聲明向工作組添加到最後一個現有的組,但你必須發揮它,特別是因爲它看起來像您選擇看起來是針對不同的多個集合函數每個訂單項(指的是金額和餘額)。請記住,分組作品如何高度依賴於你試圖組合在一起,所以如果不知道,我可以簡單地提供基本知識,或者一些建議,並讓你隨它一起運行。

Grouping on multiple columns

+0

該查詢是嚴格編輯的機密性。我沒有意識到你可以把整個查詢放在子查詢中。我會試試 –

+0

很好,謝謝! –

+0

還有一件事 - 我仍然需要顯示我爲保密而隱藏的到期日,交易日期和其他幾個字段。它一直告訴我「函數或列對」交易日期「的引用也必須出現在GROUP BY中,但是當我將這些字段添加到GROUP BY時,我失去了我以前不同的Job查詢 –