Search code examples
cassandracql

CQL UPDATE a set<bigint> with join query


I have to delete few data from a table using CQL based on some condition which will fetch data from another table. But I am unable to form the query.

Here is the table details from where I need to delete data :

Table Name : xyz_group
Columns : dept_id [int] , sub_id [set<bigint>]
PRIMARY KEY (Partition key) : dept_id

There can be same sub_id for multiple dept_id. The data is something like below :

dept_id     |     sub_id
-------------------------------
1098        |  345678298, 24579123, 8790455308
2059        |  398534698, 24579123, 8447659928
3467        |  311209878, 24579123, 8790455308, 987654321,

I need to remove only ---> 24579123, 8790455308 from all the rows.

And here is my SELECT query which will fetch the data from another table abc_list which is to be removed from table xyz_group

select sub_id from abc_list where sub_name='XYZ';

The output for the above query will give me a list of sub_id which I want to remove from the table xyz_group. So basically I want to update the set by removing data from the set. Something like below :

UPDATE xyz_group SET sub_id = sub_id - [ query result from above select query ] WHERE dept_id in (1098, 2059, 3467, ...);

I have tried to remove one element from the set, but I am getting the below error :

UPDATE xyz_group SET sub_id = sub_id - [ 24579123 ] WHERE dept_id in (1098, 2059, 3467, ...);

Error : Column sub_id type set<bigint> is not compatible with type list<int>

The tables has around >50k records. Can anyone please help to form the single correct query to update.


Solution

  • The below query is working for me now :

    UPDATE xyz_group SET sub_id = sub_id - { 24579123 } WHERE dept_id in (1098, 2059, 3467, ...);
    

    But I am doing a 2 step process to update the table. First collecting the required sub_id and then using a separate UPDATE query to update the table.Not able to do in a single query.