I have created a multi-page survey form that collects a value of 1 to 10 on each page.
With that data I need to insert 3 different calculations into 3 different columns in the database.
I have created a trigger that adds up all the numbers from the 20 pages and inserts it into the total score column but I need 2 other subscale scores and my version of mysql limits 1 trigger with the same action per table.
is it possible to have one trigger that can insert values into 3 different columns?
I am trying to do this via phpMyAdmin
My deinition below that works for total score:
[![enter image description here][1]][1]
SET NEW.total_score = NEW.answer_01 + NEW.answer_02 + NEW.answer_03 + NEW.answer_04 + NEW.answer_05 + NEW.answer_06 + NEW.answer_07 + NEW.answer_08 + NEW.answer_09 + NEW.answer_10 + NEW.answer_11 + NEW.answer_12 + NEW.answer_13 + NEW.answer_14 + NEW.answer_15 + NEW.answer_16 + NEW.answer_17 + NEW.answer_18 + NEW.answer_19 + NEW.answer_20;
The mysql site - http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
mysql> CREATE TRIGGER ins_transaction BEFORE INSERT ON account
-> FOR EACH ROW PRECEDES ins_sum
-> SET
-> #deposits = #deposits + IF(NEW.amount>0,NEW.amount,0),
-> #withdrawals = #withdrawals + IF(NEW.amount<0,-NEW.amount,0);
comma separating the columns seems to be the solution
So for your code it would be like...
SET NEW.total_score = NEW.answer_01 + ... + NEW.answer_20,
NEW.other_column = NEW.answer_01 + NEW.answer_02;
Related
Lets say that I have data rows that look like this:
+----------------------------+
+ test_table +
+----+--------+--------------+
+ id + word + updated_word +
+----+--------+--------------+
+ 1 + g00gle + ggle +
+ 2 + bread0 + bread +
+ 3 + 0bject + bject +
+ 4 + d0d0 + dd +
+-------------+--------------+
What statement could I use to take out the zeroes in the word column, so that it looks like the updated_word column? I thought of using substring, but didn't know how to proceed after that.
try:
UPDATE test_table
SET word = REPLACE(word, '0', '');
replace the 2nd blank '' with anything you want to change with.
I am trying to convert an old local Crystal Report. Basically, for a given order, there are multiple line items. The report needs to show Order and Vendor in the left two columns, then a variable number of columns (that fit on a page) to display the line numbers.
To do this, I added a Matrix, and adding the groupings and all that. However, I get only one line item for each Order/Vendor combination, even though each combination has at least two line items.
Current:
============================================
+ Order + Vendor + Item 1 + Item 2 ...
============================================
+ OrdValue1 + VendValue1 + Value + [blank]
============================================
+ OrdValue2 + VendValue2 + [blank]+ Value
============================================
Desired:
============================================
+ Order + Vendor + Item 1 + Item 2 ...
============================================
+ OrdValue1 + VendValue1 + Value + Value
============================================
+ OrdValue2 + VendValue2 + Value + Value
============================================
Edit:
I would go back to the source Dataset (assuming it is SQL) and add a Column_Number calculation which resets to 1 for each Order + Vendor combination, e.g.
ROW_NUMBER () OVER ( PARTITION BY Order , Vendor ORDER BY Line_Number ) AS Column_Number
Then I would edit the Column Group defintion to use that Column_Number column.
Query 1 :
SELECT
SUM(aol_int) AS AOL,
SUM(android_phone_int) AS Android_Phone,
SUM(androidTablet_int) AS Android_Tablet,
SUM(apple_mail_int) AS Apple_Mail,
SUM(blackberry_int) AS Blackberry,
SUM(Eudora_int) AS Eudora,
SUM(gmail_int) AS Gmail,
SUM(hotmail_int) AS Hotmail,
SUM(lotus_notes_int) AS Lotus_Notes,
SUM(other_int) AS Other,
SUM(other_webmail_int) as Other_Web_Mail,
SUM(Outlook_int) AS Outlook,
SUM(Postbox_int) AS Postbox,
SUM(sparrow_int) AS Sparrow,
SUM(thunderbird_int) AS Thunderbird,
SUM(windowsLiveMail_int) AS Windows_Live_Mail,
SUM(yahoo_int) AS Yahoo,
SUM(iPad_int) AS iPad,
SUM(iPhone_int) AS iPhone,
SUM(iPod_int) AS iPod
FROM mytable;
Query 2:
select sum(aol_int + android_phone_int + androidtablet_int+apple_mail_int+blackberry_int+Eudora_int+gmail_int+hotmail_int+lotus_notes_int+other_int+other_webmail_int+Outlook_int+Postbox_int+sparrow_int+thunderbird_int+windowsLiveMail_int+yahoo_int+iPad_int+iPhone_int+iPod_int)
as total_percentage
FROM mytable;
When I am summing up the results of Query 1 I am getting different sum as compared to what I am getting via Query2. The value in Query2 is less than Query 1. Why is it like that?
TROUBLESHOOTING:
I tried to write my query like this:
SELECT SUM( ISNULL(aol_int,0) +
ISNULL(android_phone_int,0) +
ISNULL(androidtablet_int,0) +
ISNULL(apple_mail_int,0) +
ISNULL(blackberry_int,0) +
ISNULL(Eudora_int,0) +
ISNULL(gmail_int,0) +
ISNULL(hotmail_int,0) +
ISNULL(lotus_notes_int,0) +
ISNULL(other_int,0) +
ISNULL(other_webmail_int,0) +
ISNULL(Outlook_int,0) +
ISNULL(Postbox_int,0) +
ISNULL(sparrow_int,0) +
ISNULL(thunderbird_int,0) +
ISNULL(windowsLiveMail_int,0)+
ISNULL(yahoo_int,0) +
ISNULL(iPad_int,0) +
ISNULL(iPhone_int,0) +
ISNULL(iPod_int,0) )AS total_percentage
FROM mytable;
However, I am getting an error after running above query in the MySQL workbench:
Error Code: 1582. Incorrect parameter count in the call to native function 'ISNULL'. What is wrong here?
This could happen if some columns in some of the rows contain nulls. When this happens, all columns in a row with even a single null column would produce null in the chain of additions, so the row will add nothing to the total.
Here is a short demo of this effect. Setup:
create table test(x int null, y int null);
insert into test(x,y) values (1,null);
insert into test(x,y) values (null,2);
insert into test(x,y) values (3,3);
Queries:
select sum(x+y) from test; -- Shows 6
select sum(x)+sum(y) from test -- Shows 9
See this demo on sqlfiddle: link.
In reporting services, I have used
=IIf(Fields!Weekday.Value="Su"
or Fields!Weekday.Value="Sa"
or Len(Fields!HOLIDAY.Value)>0,
(Fields!GENERAL.Value
+ Fields!LAUNCH.Value
+ Fields!SHIFT.Value
+ Fields!OCESAWE.Value
+ Fields!OCESAWD.Value
+ Fields!WEPHWORK.Value
+ Fields!OCREMWE.Value
+ Fields!OCREMWD.Value) * 1.5,
(Fields!GENERAL.Value
+ Fields!LAUNCH.Value
+ Fields!SHIFT.Value
+ Fields!OCESAWE.Value
+ Fields!OCESAWD.Value
+ Fields!WEPHWORK.Value
+ Fields!OCREMWE.Value
+ Fields!OCREMWD.Value)
)
To get a columnn called "Total Weighted" and this column have several rows.
1. ID Total Weighted
2. 111 21
3. 121 49
How can I get the total of the "Total Weighted?
Create a calculated field in your dataset, and use the formula you provided as the value of that field. Now you can refer to that field elsewhere as if it came from your database.
So then you'd use an expression like =SUM(Fields!TotalWeighted.Value) in your tablix.
We have 2500 products on our site, ranked between 60 different categories. Our DB scheme is 61 columns, labled "product_id", and then the categories: "category_1", "category_2"... "category_60", and 2500 rows, one for each product. If a product is not ranked in a specific cateogry, that corresponding field is marked "0". If it is ranked, the field is an INT with whatever rank it is: "1" is 1st, "2" is second, etc.
Usually products are only ranked in 2-3 categories, so there are 57+ columns with a "0" in the field. My current query is:
mysql_query("SELECT AVG(category_1 + category_2 + category_3 + category_4 + category_5 + category_6 + category_7 + category_8 + category_9 + category_10 + category_11 + category_12 + category_13 + category_14 + category_15 + category_16 + category_17 + category_18 + category_19 + category_20 + category_21 + category_22 + category_23 + category_24 + category_25 + category_26 + category_27 + category_28 + category_29 + category_30 + category_31 + category_32 + category_33 + category_34 + category_35 + category_36 + category_37 + category_38 + category_39 + category_40 + category_41 + category_42 + category_43 + category_44 + category_45 + category_46 + category_47 + category_48 + category_49 + category_50 + category_51 + category_52 + category_53 + category_54 + category_55 + category_56 + category_57 + category_58 + category_59 + category_60) as 'cat_avg' FROM products.rankings WHERE product_id = '$product_id'");
With this, I'm just getting the sum of the columns, not the AVG. Maybe this has something to do with selecting rows instead of columns, I'm not sure. I tried SUM as well, instead of AVG, same thing.
I'm not really sure where to go from here. What i would like is the Average ranking across all columns for one product, where the column doesn't equal 0. So if a product_id 123 is ranked 7, 9 and 11, and then the other 57 columns are 0, the average returned would be 9 ((7+9+11)/3), not .45 ((7+9+11+0+0+0....+0))/60)
Note: I did not design this DB, I'm sure there is a better way to design it, but at this point it's too deeply integrated to change up quickly.
This may be a lot of stress on the query, but I don't know many other ways to do this, given the schema you have to work with.
One option is to sub-query the columns and union them, where the given columns are not 0:
SELECT AVG(
SELECT *
FROM (
SELECT category_1 AS category FROM table
UNION
SELECT category_2 AS category FROM table
UNION
...
) cats
WHERE category <> 0
)
FROM products.rankings
WHERE product_id = '$product_id'
It probably makes more sense to do this math within the page (assuming PHP given the query decorations) and on a per-row basis. Doing the above will put a lot of strain on the server depending the number of rows we're talking.
Restructured the whole DB... wasn't as bad as I though, just did a bunch of MySQL queries/updates that got me what I needed. Strained the server for a few hours, but it was well worth it in the end.