[英]distinct and sum in single join query
我有以下連接查詢。我希望記錄與 c.checkid 以及收入總和不同。我如何在一個查詢中同時執行這兩項?
select c.CheckID,cv.CheckID,Earnings,TaxableEarnings,Amount,EmployeeCode, CheckDate FROM [Customers0].[pay].[CheckVendorCalc] as cv
inner join [Customers0].[pay].[Checks] as c
on cv.checkid=c.checkid
where
c.CheckDate BETWEEN '2022-01-01' AND '2022-12-31' and
c.CustomerID=360 and
c.EmployeeCode='01' and
(cv.TaxableEarnings !=null or cv.TaxableEarnings!=0)
您可以使用GROUP BY
。 SELECT
子句中的任何非聚合列都必須出現在GROUP BY
子句中。 您可以在此處找到更多信息。
SQL:
select
c.CheckID,
SUM(Earnings) as totalearning
FROM
[Customers0].[pay].[CheckVendorCalc] as cv
inner join [Customers0].[pay].[Checks] as c on cv.checkid = c.checkid
where
c.CheckDate BETWEEN '2022-01-01'
AND '2022-12-31'
and c.CustomerID = 360
and c.EmployeeCode = '01'
and (
cv.TaxableEarnings != null
or cv.TaxableEarnings != 0)
group by
c.CheckID
order by
c.CheckID
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.