SQL为特定作业创建美元金额的SUM?

时间:2016-03-07 22:27:54

标签: mysql sql vb.net datagridview

以下是我的数据库中的一些示例数据(为机密性编辑)

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中不是很好,并且不知道在哪里插入我的子查询以获得我需要的东西。请帮忙吗?

1 个答案:

答案 0 :(得分: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)

哇..现在格式化已完成..你需要一个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

您可以将作业组添加到最后一个现有的group by语句中,但是您必须使用它,特别是因为看起来您正在为每个订单项选择多个看起来不同的聚合函数(指金额和余额)。请记住,分组的工作方式在很大程度上取决于您尝试组合在一起的内容,所以我不知道,我可以简单地提供基础知识或一些建议,让您随意使用。

Grouping on multiple columns