按聚合筛选 SQL 个查询结果
Filter SQL query results by aggregrate
我需要一个查询来显示工作人员未获得报酬但公司已获得报酬的 JobID。以下是 table 列和示例数据:
tblInvoices columns:
-------------------
JobID
InvoiceID
WorkerPaidAmountTotal
CompanyPaidAmountTotal
Sample data
-----------
JobID | InvoiceID | WorkerPaidAmountTotal | CompanyPaidAmountTotal
1 30 100 150
1 31 0 100
2 32 0 75
3 33 25 50
3 34 10 30
4 35 0 0
我知道如何计算支付给工人或公司的金额的总和。结果如下所示:
JobID Worker Company
1 100 250
2 0 75
3 35 80
4 0 0
但我需要的只是工作人员获得 0 且公司 >0 的 JobID 的结果。我想要的结果应该是这样的,但是我想不出这样做的查询:
JobID Worker Company
2 0 75
使用HAVING
子句过滤组。试试这个:
SELECT jobid,
Worker=Sum(WorkerPaidAmountTotal),
Company=Sum(CompanyPaidAmountTotal)
FROM tablename
GROUP BY jobid
HAVING Sum(WorkerPaidAmountTotal) = 0
AND Sum(CompanyPaidAmountTotal) > 0
select jobid, worker, company where WorkerPaidAmountTotal = 0 and CompanyPaidAmountTotal
看起来很简单...可能是我没理解问题
我需要一个查询来显示工作人员未获得报酬但公司已获得报酬的 JobID。以下是 table 列和示例数据:
tblInvoices columns:
-------------------
JobID
InvoiceID
WorkerPaidAmountTotal
CompanyPaidAmountTotal
Sample data
-----------
JobID | InvoiceID | WorkerPaidAmountTotal | CompanyPaidAmountTotal
1 30 100 150
1 31 0 100
2 32 0 75
3 33 25 50
3 34 10 30
4 35 0 0
我知道如何计算支付给工人或公司的金额的总和。结果如下所示:
JobID Worker Company
1 100 250
2 0 75
3 35 80
4 0 0
但我需要的只是工作人员获得 0 且公司 >0 的 JobID 的结果。我想要的结果应该是这样的,但是我想不出这样做的查询:
JobID Worker Company
2 0 75
使用HAVING
子句过滤组。试试这个:
SELECT jobid,
Worker=Sum(WorkerPaidAmountTotal),
Company=Sum(CompanyPaidAmountTotal)
FROM tablename
GROUP BY jobid
HAVING Sum(WorkerPaidAmountTotal) = 0
AND Sum(CompanyPaidAmountTotal) > 0
select jobid, worker, company where WorkerPaidAmountTotal = 0 and CompanyPaidAmountTotal
看起来很简单...可能是我没理解问题