SQL update multiple rows with same value - mysql

I use this to update (add points) rows which mgroup is 15
UPDATE ibf_members SET points = points + 500 WHERE mgroup = 15
What can I use to update (add points + 500) for rows which has its id as 5, 7, 10, 11, 16, 25 and also has mgroup as 15?

You can use the IN clause for this, which is easier to read (and possibly more efficient?) than building a giant OR list. Try something like:
UPDATE ibf_members
SET points = points + 500
WHERE mgroup = 15
AND id IN (5, 7, 10, 11, 16, 25);

Just add another condition to your WHERE clause:
UPDATE ibf_members SET points = points + 500 WHERE mgroup = 15 AND id IN (5, 7, 10, 11, 16, 25)

Related

How to fix 'subquery returns more than 1 row' with variable tables

I have a table RAW_SCORES that contains a bunch of homework/exam grades. There is a row in RAW_SCORES that contains the max points for each assignment.
('6410', 'Rivera', 'Rhonda', '315', 64, 64, 28, 85, 98, 152),
('0001', 'MAX', 'POINTS', '415', 100, 80, 32, 100, 120, 200),
I want to create a procedure that prints a table with the (Raw Score / Max Score) for each assignment. So for assignment 1:
SET hw1M = (SELECT HW1 FROM RAW_SCORES WHERE (SSN = '0001'));
SELECT RAW_SCORES.SSN,
RAW_SCORES.FName,
RAW_SCORES.LName,
ROUND(RAW_SCORES.HW1 / hw1M, 2)
FROM RAW_SCORES WHERE NOT (RAW_SCORES.SSN = '0001' OR RAW_SCORES.SSN = '0002');
Gives me the correct result for HW1, but the table header says Round(bla bla bla) and the question wants it to be printed as HW1Pct. So I tried:
SET hw1M = (SELECT HW1 FROM RAW_SCORES WHERE (SSN = '0001'));
SET HW1Pct = (SELECT ROUND(RAW_SCORES.HW1 / hw1M, 2) FROM RAW_SCORES WHERE hw1M IN (SELECT HW1 FROM RAW_SCORES WHERE (SSN = '0001')));
SELECT RAW_SCORES.SSN,
RAW_SCORES.FName,
RAW_SCORES.LName,
HW1Pct
FROM RAW_SCORES WHERE NOT (RAW_SCORES.SSN = '0001' OR RAW_SCORES.SSN = '0002');
But this gives me the Subquery returns more than 1 row error. Most of the other answers to this error are a JOIN statement, but I'm not sure how I would implement that in my case. Any help is appreciated. Sorry for it being a dumb question.
Just add a column alias to your original query:
ROUND(RAW_SCORES.HW1 / hw1M, 2) AS HW1Pct
See the manual

SSRS Case Statement

I have created a table in SSRS of various grades, but I need to look at the value of 2 grades together and assign it an alternate grade. I cannot put this as a CASE in the SELECT as the way the database is designed the values are not stored in one row, but in multiple rows, therefore it cannot combine the data in a new column. For example, this is one students grades represented in the DB
634 Attainment *#1#2#3#4#N/A NULL 1 2
636 Effort A*#A#B#C#N/A NULL A 2
637 Focus EX#ME#WB#N/A NULL EX 1
638 Participation EX#ME#WB#N/A NULL ME 2
639 Groupwork EX#ME#WB#N/A NULL ME 2
640 Rigour EX#ME#WB#N/A NULL ME 2
641 Curiosity EX#ME#WB#N/A NULL ME 2
642 Initiative EX#ME#WB#N/A NULL ME 2
643 Self Organisation EX#ME#WB#N/A NULL ME 2
644 Perseverance EX#ME#WB#N/A NULL ME 2
I have created a table that has grouped the grades based on the pupil ID and it is now represented as one row and column headings for each grade (effort, Focus etc).
I have tried to do a sum using the ReportItems!Textbox1.Value but I can't use this method as it is not an aggregate function. What I wanted to do was
IF (ReportItems!Textbox104.Value + ReportItems!Textbox105.Value = 2) THEN 5
Is there a way to do this?
ADDITIONAL:
I have just tried:
=SWITCH(ReportItems!Textbox104.Value + ReportItems!Textbox105.Value = 2, 5,
ReportItems!Textbox104.Value + ReportItems!Textbox105.Value = 3, 4,
ReportItems!Textbox104.Value + ReportItems!Textbox105.Value = 4, 3,
ReportItems!Textbox104.Value + ReportItems!Textbox105.Value = 5, 2,
ReportItems!Textbox104.Value + ReportItems!Textbox105.Value = 6, 1,
ReportItems!Textbox104.Value + ReportItems!Textbox105.Value = 7, 0,
"NULL"
)
This is returning an Error.
I finally resolved this by using this expression:
=IIF(ReportItems!Textbox104.Value + (ReportItems!Textbox105.Value = 2), 5,
IIF(ReportItems!Textbox104.Value + (ReportItems!Textbox105.Value = 3), 4,
IIF(ReportItems!Textbox104.Value + (ReportItems!Textbox105.Value = 4), 3,
IIF(ReportItems!Textbox104.Value + (ReportItems!Textbox105.Value = 5), 2,
IIF(ReportItems!Textbox104.Value + (ReportItems!Textbox105.Value = 6), 1,
IIF(ReportItems!Textbox104.Value + ReportItems!Textbox105.Value = 7, 0,
"NULL"))))))

Does mysql query cache the dynamically calculated columns

I have a mysql query:
SELECT my_table.* WHERE SOUNDEX(my_col)='whatever' OR SUBSTR(SOUNDEX(my_col),4)='whatever' ORDER BY SUBSTR(SOUNDEX(my_col),4)='whatever',SOUNDEX(my_col)='whatever'
How many times will the substring function and soundex functions will actually be called? I mean for exactly same inputs will mysql cache the results over the span of one query?
If not, how can I make the change in the query so that each function is called minimum times possible.
MySQL would call this function four times for every returned row, to avoid this you can use a subquery, so instead of
SELECT *
FROM song
ORDER BY Substr(pre_calculated_soundex, 1, 1) =
Substr(Soundex("aaaa"), 1, 1)
+ Substr(pre_calculated_soundex
, 2, 1) =
Substr
(Soundex("aaaa"), 2, 1)
+ Substr(pre_calculated_soundex, 3, 1)
= Substr(Soundex("aaaa"), 3, 1)
+ Substr(pre_calculated_soundex, 4, 1
)
= Substr(Soundex("aaaa"), 4, 1)
You can do
SELECT * from (select *, Soundex("aaaa") as current_soundex from song)
ORDER BY
Substr(pre_calculated_soundex, 1, 1) = Substr(current_soundex , 1, 1)
+ Substr(pre_calculated_soundex, 2, 1) = Substr(current_soundex , 2, 1)
+ Substr(pre_calculated_soundex, 3, 1) = Substr(current_soundex , 3, 1)
+ Substr(pre_calculated_soundex, 4, 1) = Substr(current_soundex , 4, 1)

Fastest way to calculate correlation in every row?

Well, I have a table data of millions of rows. I want to carry out correlation study for every row (from the 1st to the current row minus 1). For e.g. the 1st rows is omitted. The 2nd row's result column is to be supplied with the correlation using the 1st row. The 3rd row's result column is to be supplied with the correlation using the 1st and 2nd row. And so on.
Correlation for the entire table can be calculated using:
SELECT (Count(*)*Sum(x*y)-Sum(x)*Sum(y))/
(sqrt(Count(*)*Sum(x*x)-Sum(x)*Sum(x))*
sqrt(Count(*)*Sum(y*y)-Sum(y)*Sum(y))) AS TotalCorelation FROM Data;
I want to avoid using Joins as much as possible as it takes lots of time, sometimes even timeout error, above 300 seconds). What's the other alternative?
Example table Data Structure:
id, x, y, result
1 , 4, 2, null
2 , 6, 3, -0.2312
3 , 5, 5, 0.42312
4 , 6, 2, -0.5231
5 , 5, 5, 0.22312
6 , 3, 7, -0.2312
7 , 2, 9, 0.42231
8 , 7, 2, 0.32253
9 , 9, 5, 0.32431
id : primary key
x and y : The data
result: correlation
I think this is it:
SELECT d2.ID, d2.x, d2.y, d2.result,
(Count(*)*Sum(d1.x*d1.y)-Sum(d1.x)*Sum(d1.y))/
(sqrt(Count(*)*Sum(d1.x*d1.x)-Sum(d1.x)*Sum(d1.x))*
sqrt(Count(*)*Sum(d1.y*d1.y)-Sum(d1.y)*Sum(d1.y))) AS TotalCorelation
FROM Data d1
RIGHT JOIN Data d2 ON d1.id < d2.id
GROUP BY d2.ID
ORDER BY d2.ID
Without a closed form for calculating correlation of N+1 from N rows, you have to use a quadratic join like this.
I'm assuming that your basic formula is correct. But I'm not sure it is -- when I just run it on the total dataset, I don't get the result 0.32431, I get -0.552773693079.
Here's a linear implementation:
SET #SumX = 0;
SET #SumY = 0;
SET #Count = 0;
SET #SumX2 = 0;
SET #SumY2 = 0;
SET #SumXY = 0;
SELECT id, x, y,
#SumX := #SumX + x AS SumX,
#SumY := #SumY + y AS SumY,
#Count := #Count + 1 AS ct,
#SumX2 := #SumX2 + x*x AS SumX2,
#SumY2 := #SumY2 + y*y AS SumY2,
#SumXY := #SumXY + x*y AS SumXY,
IF(#Count > 1,
(#Count*#SumXY-#SumX*#SumY)/
(sqrt(#Count*#SumX2-#SumX*#SumX)*
sqrt(#Count*#SumY2-#SumY*#SumY)), NULL) AS TotalCorelation
FROM DATA
ORDER BY id
SQLFIDDLE

sql loop and set properties

In SQL, I would like to query a list, in order by pageNumber
SELECT * FROM `comics`
WHERE 1
ORDER BY pageNumber ASC
Then, I would like to set their pageNumbers based on their index in the query (starting with 1 instead of 0).
Here is my pseudo code for the functionality as desired; where list is the return value of the Select Query above.
for(var n:int = 0; n<list.length; n++){
if(list[n].pageNumber != n+1){
list[n].pageNumber = n+1
}
}
For example I might have pageNumbers 5, 17, 23, 24, 18, 7
The ORDER BY pageNumber ASC will sort this to 5, 7, 17, 18, 23, 24
I would then like to alter the pageNumbers in order to be 1, 2, 3, 4, 5, 6
edit:
#fortheworld MySQL
#cyberkiwi UPDATE
sorry for being unclear. guess i need to learn more for my questions to be clear :)
thanks for all your help
SET #I := 0;
SELECT *,
#I := #I + 1 AS newPageNumber
FROM comics
ORDER BY pageNumber ASC
I don't understand why peops insist on writing an SQL batch when a single statement will do.
SELECT comics.*, #n := #n + 1 AS PageNumber2
FROM (SELECT #n := 0) X CROSS JOIN comics
ORDER BY pageNumber ASC