SUMIFS (Excel) to SQL

Hi.
I cant get this to work in SQL.


I got it work for first SUMIFS1(field in excel CG):
--SUMIFS1
--(CG:CG;B:B;B3;CH:CH;1)
SELECT ID_Client, SUM(UKIZL_za_SME_RET_CORP_IDX) AS CG
FROM RWA_Data_TMP
WHERE Related_party_Group = 'N/A' and UKIZL_POVos_SME_RET_CORP_IDX='1.0'
GROUP BY ID_Client

But cant figure out second SUMIFS2 (field in excel CI)
--SUMIFS2
--(CG:CG;G:G;G6;CH:CH;1)
SELECT Related_party_Group, SUM(UKIZL_za_SME_RET_CORP_IDX) as CG
FROM RWA_Data_TMP
*WHERE Related_party_Group <> 'N/A' and UKIZL_POVos_SME_RET_CORP_IDX='1.0' *
GROUP BY Related_party_Group

Here is something similar that i need: Update column based on criteria in row
but cant apply to my case.

I hope I made good description.

Since no answers yet, I am going to suggest you decipher what the SUMFI is supposed to be doing. I don't quite get what the criteria is.

To me, it seems like a CASE expression would actually make sense here, so something like what you have but SUM(CASE WHEN THEN else 0 END.

you have two different group by clauses, so that may not be needed, but writing it out would help. .

1 Like

Hi. Thanks for answer.

What is the syntax for SUM() inside CASE?

Is this correct?

CASE 
WHEN something THEN SUM() 
ELSE something 
END
CASE 
        WHEN AGE < 50 THEN SUM(sal) 
        ELSE                   0 
END
1 Like

You're going to need a GROUP BY somewhere in the code. You need to use...

SUM(CASE WHEN something = something THEN SomeCol ELSE 0 END).

You probably won't need a WHERE clause at all. You can do all the columns at once. It's known as a CROSS TAB (as opposed to a (ugh!) PIVOT). Here's an article that teaches you all about such things. In later versions of SQL, you can also use SUM(IIF) instead of SUM(CASE).

1 Like