Q:

How could I calculate sums with group by?

I am by far no expert in SQL, however I am trying my best to make the best out of it.
After cleaning and transforming data within SQL, I have a large table in SQL named PaymentData. A piece of my data looks like the following:
--------------------------------------------------------------------
Date                     | Code                | User ID   | Payment Amount                                
2018-09-26               | 111                 | 1         | 125                                  
2018-09-26               | 122                 | 1         | 300                                 
2018-09-26               | 177                 | 1         | 150                                
2018-09-26               | 116                 | 1         | 130                                  
2018-09-26               | 118                 | 1         | 180                                
2018-09-26               | 111                 | 2         | 290                                 
2018-09-26               | 122                 | 2         | 200                                
2018-09-26               | 188                 | 2         | 200                                
2018-09-26               | 199                 | 2         | 310  

Note that the dataset is somehow recurrent. Users get paid each month and these payments are allocated to certain allocation codes. So it is quite possible that, for example, user 1 has the same amounts and codes at 2018-10-26. But is also possble that a certain user has just a few (or more) codes and different payment amounts in another month. 
Therefore, I would like to create a new table that groups the data by ID and Date and then takes the aggregate sum by certain codes. For example, I would like to have a column that sums the payment amounts of code 111 and 122 as X, another column Y that sums only the amount for 177 grouped by ID and Date and so on. Something like this, with only user 1 and 2:
--------------------------------------------------------------------
Date                     | X                   | Y         | User ID                               
2018-09-26               | 425                 | 150       | 1                                                                
2018-09-26               | 490                 | 0         | 2                                 

Briefly:
The new table creates columns that contain an accumulated amount based on a list of codes, grouped by date and ID. For example, in column X there is an added amount of the codes 111 and 112 per Date and ID, in column Y there is an summed amount based on for example 5 other codes per Date and ID and so on.
What is the best way to aggregate this so that in principle I can only pass my list of codes per column to SQL so that it processes it neatly for me?

A:

You can use conditional aggregation here
select
    [Date]
    ,X = sum(case when Code in (111,222) then [Payment Amount] end)
    ,Y = sum(case when Code in (177) then [Payment Amount] end)
    ,[User ID]
from
    SomeTable
group by
   [Date], [User ID]

Or using IIF
select
    [Date]
    ,X = sum(iif(Code in (111,222),[Payment Amount],0))
    ,Y = sum(iif(Code = 177,[Payment Amount],0))
    ,[User ID]
from
    SomeTable

And instead of storing it in another table, just create a view.
create view MyViewName
as
select
    [Date]
    ,X = sum(case when Code in (111,222) then [Payment Amount] end)
    ,Y = sum(case when Code in (177) then [Payment Amount] end)
    ,[User ID]
from
    SomeTable

And then call it...
select * from MyViewName

