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
爲什麼MySQL的標籤? – Strawberry
我不知道它是什麼樣的SQL –