Question: I'm wanting to group my data and count the number of transactions(FOCUS) for each 7 day period (Saturday-Friday). I think if I change getdate() to getdate(-2) that would do the Sat-Fri, however, I can't test it because I'm getting an error that states "is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause." I tried moving the datediff to a GROUP BY and that didn't work either. Any advice? Thanks! 

```
SELECT COUNT(FOCUS), datediff(day, ENTRY_DATE, getdate())/7 AS WEEK
FROM ACCOUNTHISTORY
WHERE FOCUS = 6050
AND PRINCIPAL_AMT > 0
AND ENTRY_DATE >= '2020-01-01'

```

Answer: 
You can't achieve this with `MERGE` because you need to update non-matching rows in `target_tb`.  

If you still want to do this with a single statement, you may use select from data-change operation like below:

```
create table target_tb (key int not null, val int);
create table source_tb (key int not null, val int);
insert into target_tb values (1, 1), (2, 2);
insert into source_tb values (3, 3), (2, 2);

with 
  u as 
(
select key 
from old table
(
update target_tb t
set val = -1
where not exists (select 1 from source_tb s where s.key = t.key)
)
)
, i as
(
select key 
from new table
(
insert into target_tb
select key, val
from source_tb s
where not exists (select 1 from target_tb t where t.key = s.key)
)
)
select a
from (values 1) t(a);

select * from target_tb;

```

The result is:

```
|KEY        |VAL        |
|-----------|-----------|
|1          |-1         |
|2          |2          |
|3          |3          |

```

[dbfiddle link](https://dbfiddle.uk/?rdbms=db2_11.1&fiddle=2d25dc0940094d904d6205881be40bba).
I'd suggest using DB Fiddle..
<https://dbfiddle.uk/?rdbms=db2_11.1>

But no I don't believe your statement will work. Specifically the 

> 
> 2. For records coming from target\_tb but not coming from source\_tb, expire the records aka set event\_type = ‘EXPIRED’
> 
> 
> 

Db2 merge doesn't support `WHEN NOT MATCHED BY SOURCE` clause that SQL Server supports...