We're having an issue writing a trigger function that involves 3 tables: CustomerProfile, DropdownStats and OrderDetails. A simplified schema of each is as follows...
CustomerProfile table...this table has a row for all of the column names of the OrderDetails table.
CustID ColumnName IsDropDown
------ ---------- ----------
1 firstcolumn false
1 ...
1 orderid false
1 format false
1 department false
1 ...
1 lastcolumn false
DropdownStats table...
OrderID ColumnName ColumnValues CountOfColumnValues
------- ---------- ------------ -------------------
1 format circle 15
1 format square 23
1 format triangle 3
2 format circle 10
3 format square 5
3 format triangle 10
OrderDetails table...
firstcolumn ... orderid format department ... lastcolumn
----------- --- ------- ------ ---------- --- ----------
data dta 1 circle 123 dta data
data dta 1 square 134 dta data
...
data dta 2 circle 354 dta data
...
The trigger is written for the CustomerProfile table as an AFTER UPDATE trigger.
If the 4th row in the CustomerProfile table (where columnname = format) transitions the IsDropDown from false to true, then we have a to ADD rows to the DropdownStats table as they are pulled from the OrderDetails table.
The trigger function as written is producing results in the DropdownStats table like below but we want results like the sample above.
DropdownStats table...
OrderID ColumnName ColumnValues CountOfColumnValues
------- ---------- ------------ -------------------
1 format format 41
2 format format 10
3 format format 15
The problem is having the trigger function insert a static column name and then have a separate row for each of the distinct values found for that column and a count of rows that hold those values. Here is one (of several) attempt at this...the problem is the two new.columnname They should not be the same.
if old.isdropdown <> new.isdropdown then --if there was a change
if new.isdropdown = true then --and the change was from false to true
insert into dropdownstats
(orderid,columnname,columnvalues,countofcolumnvalues)
select distinct
orderid
, new.columnname -- we want this to be the static actual name of the column, repeated for every record inserted (Ex: 'format')
, new.columnname -- we want a row for each of the values found for the column in the database (Ex: 'circle', 'square', 'triangle', etc.)
, count(new.columnname) -- the count of the records found for the value (Ex: for 'circle, count was 15)
from orderdetails
group by orderid, new.columnname;
else --the transition was from true to false
delete from dropdownstats as dd where dd.columnname = new.columnname;
end if;
end if;
If I just use the query tool and manually create and run the select statement shown below, I get what I want.
select distinct orderid, 'format', format, count(format) from orderdetails group by orderid, format;
But how do we translate the manual query into an equivalent trigger function? How do we distinguish the two "new.columnname" from each other?
The structure of OrderDetails does not change. It is very stable.