I have a SQL table called 'orders' with the columns: 'id', 'name', 'type', 'price', and 'allow'. Let's say we have the following data in the 'orders' table:
|id|name|type|price|allow|
|1|Product A|Type A|10|false|
|2|Product A|Type B|15|false|
|3|Product B|Type A|20|false|
|4|Product C|Type B|25|false|
|5|Product C|Type C|30|false|
In this table, 'id' is the primary key (an incremental number), 'name' and 'type' are text, 'price' is a number, and 'allow' is a boolean.
We have several types of elements in this table. Now, let's assume we want to "allow" them as a group based on the 'name' column rather than individually because it would be time-consuming.
To achieve this, I use the following query:
SELECT name, allow, SUM(price) AS total_price
FROM orders
GROUP BY name, allow;
The result of this query, grouped by 'name', and 'allow', would be as follows:
before edit:
|name|allow|total_price|
|Product A|false|25|
|Product B|false|20|
|Product C|false|55|
Now, what I do is create a retool table called 'allow' and provide the user with the ability to edit the 'allow' column using checkboxes.
after edit:
|name|allow|total_price|
|Product A|true|25|
|Product B|false|20|
|Product C|true|55|
I make the 'allow' column editable and then attempt to perform a bulk update. This is where it becomes complicated because I typically do it in GUI mode using "Bulk update via a primary key" with an array like "{{allow.changesetArray}}".
However, since I used the GROUP BY function, I lost the primary key, and I am unsure how to resolve this issue. I attempted to switch from GUI mode to SQL and tried something like this:
UPDATE orders SET allow = CASE WHEN ({{allow.selectRow.name}} = orders.name) THEN {{allow.selectRow.allow}} ELSE false END;
I am using "selectRow" because I set the "Event handlers" as "Change cell". Unfortunately, {{allow.selectRow.allow}} always gives me false, even if the checkbox is true.
The result I want to obtain, in the table orders, is the following
|id|name|type|price|allow|
|1|Product A|Type A|10|true|
|2|Product A|Type B|15|true|
|3|Product B|Type A|20|false|
|4|Product C|Type B|25|true|
|5|Product C|Type C|30|true|
Could you please help me with this?
I found I similar topic but I could not make it work
Thanks