IT박스

SQL 쿼리 내에서 두 필드를 합산하는 방법

itboxs 2020. 10. 31. 09:29
반응형

SQL 쿼리 내에서 두 필드를 합산하는 방법


동일한 행 내에있는 총 두 개의 필드를 가져 와서 동일한 행의 끝에있는 필드에 해당 숫자를 입력해야합니다.

이것은 내 코드입니다.

Sum(tbl1.fld1 + tbl1.fld2) AS [Total]

이것이 SUM 함수가 사용되는 용도입니까, 아니면 열 합계를 얻기 위해 SUM 함수 만 사용할 수 있습니까?

감사


SUM집계 함수입니다. 각 그룹의 합계를 계산합니다. +행에서 두 개 이상의 열을 계산하는 데 사용됩니다.

이 예를 고려하십시오.

ID  VALUE1  VALUE2
===================
1   1       2
1   2       2
2   3       4
2   4       5

 

SELECT  ID, SUM(VALUE1), SUM(VALUE2)
FROM    tableName
GROUP   BY ID

결과가 될 것이다

ID, SUM(VALUE1), SUM(VALUE2)
1   3           4
2   7           9

 

SELECT  ID, VALUE1 + VALUE2
FROM    TableName

결과가 될 것이다

ID, VALUE1 + VALUE2
1   3
1   4
2   7
2   9

 

SELECT  ID, SUM(VALUE1 + VALUE2)
FROM    tableName
GROUP   BY ID

결과가 될 것이다

ID, SUM(VALUE1 + VALUE2)
1   7
2   16

다음을 시도하십시오.

SELECT *, (FieldA + FieldB) AS Sum
FROM Table

SUM은 여러 행에 대한 열의 값을 합산하는 데 사용됩니다. 열을 함께 추가 할 수 있습니다.

select tblExportVertexCompliance.TotalDaysOnIncivek + tblExportVertexCompliance.IncivekDaysOtherSource AS [Total Days on Incivek]

ID  VALUE1  VALUE2
===================
1   1       2

1   2       2
2   3       4
2   4       5

select ID, (coalesce(VALUE1 ,0) + coalesce(VALUE2 ,0) as Total from TableName

sum 함수는 열의 합계 만 가져옵니다. 서로 다른 열의 두 값을 합산하려면 값을 int로 변환하고 +-연산자를 사용하여 더합니다.

Select (convert(int, col1)+convert(int, col2)) as summed from tbl1

도움이 되었기를 바랍니다.


If you want to add two columns together, all you have to do is add them. Then you will get the sum of those two columns for each row returned by the query.

What your code is doing is adding the two columns together and then getting a sum of the sums. That will work, but it might not be what you are attempting to accomplish.


Due to my reputation points being less than 50 I could not comment on or vote for E Coder's answer above. This is the best way to do it so you don't have to use the group by as I had a similar issue.
By doing SUM((coalesce(VALUE1 ,0)) + (coalesce(VALUE2 ,0))) as Total this will get you the number you want but also rid you of any error for not performing a Group By. This was my query and gave me a total count and total amount for the each dealer and then gave me a subtotal for Quality and Risky dealer loans.

SELECT 
    DISTINCT STEP1.DEALER_NBR
    ,COUNT(*) AS DLR_TOT_CNT
    ,SUM((COALESCE(DLR_QLTY,0))+(COALESCE(DLR_RISKY,0))) AS DLR_TOT_AMT
    ,COUNT(STEP1.DLR_QLTY) AS DLR_QLTY_CNT
    ,SUM(STEP1.DLR_QLTY) AS DLR_QLTY_AMT
    ,COUNT(STEP1.DLR_RISKY) AS DLR_RISKY_CNT
    ,SUM(STEP1.DLR_RISKY) AS DLR_RISKY_AMT
    FROM STEP1
    WHERE DLR_QLTY IS NOT NULL OR DLR_RISKY IS NOT NULL
        GROUP BY STEP1.DEALER_NBR

Just a reminder on adding columns. If one of the values is NULL the total of those columns becomes NULL. Thus why some posters have recommended coalesce with the second parameter being 0

I know this was an older posting but wanted to add this for completeness.

참고URL : https://stackoverflow.com/questions/14877797/how-to-sum-two-fields-within-an-sql-query

반응형