Perform sum, average, and multiplication across all columns in a table? - mysql

I have a table that looks like this:
id test1 test2 test3 test4 ... test200
1 90 87 85 86 70
2 100 95 83 92 80
.
.
18000
I know there are standard operations to perform sums and averages on a single column and multiply the value of two columns together but is it possible to do it across all columns in a row with a given id? If its not clear, I want to do something like this across rows instead of across columns. Thanks

You might be better off redesigning the table so that it does not have 200 columns.
e.g.
Id testnum score
1 1 90
1 2 87
...
2 1 100
2 2 95
...
180000
Now you can do a query like this:
select sum(score) as totalscore
from mynewtable
where id=1

How about:
select id, sum(test1 + test2 + ...) as summation
group by id
Is the problem that you have so many columns? This solution doesn't handle many columns smoothly.

There might be some contorted SQL to work on the contents of the whole row (I doubt that), but you still have to specify the column names, otherwise you'd have the ID numbers included in the calculation.

Related

Create new column storing average of rows from another column in MySQL

I am trying to create a new column that has stores the 'Average weight of the field'. For example, the answer for RaceID = 123 would be 54.5. The RaceID's are not organised from smaller to largest and are displayed randomly like the example below.
RaceID
Weight
No. Starters
123
56
2
124
58
2
123
53
2
125
60
2
125
51
2
124
62
2
Try below query, It will display current table data along with average column :
select t.*,
avg(Weight) over(partition by raceID order by raceID ) avg_raceID
from table t;
SELECT RaceID, AVG(Weight) AS val_1
FROM dataset_name
GROUP BY RaceID;
By using above code we can get the average value of weights for every unique RaceID. Check the below image for better understanding.
https://i.stack.imgur.com/kMA68.png
Let me know if there are any modifications or error.

MySQL: Select a fixed value based on UNIQUE only values of another column

I have a table with values in a column called OrderID. The values aren't all unique and often two or more rows will share the same value. What I'm trying to do, when I make a Select, I want to add a fixed value based only on UNIQUE or DISTINCT values from the OrderID column. I've tried a SELECT DISTINCT but of course that just gives me distinct only rows, as I still need to return all of the data. I played around with UNION by having one query return all of the results and the other return only DISTINCT results, but I still didn't get the final result I was looking for.
Below is basically what I'm trying to accomplish. It seems like it should be easy, but I'm not sure how to tell my fixed value SELECT to only look for distinct values in a specific column. Keep in mind, my full dataset is much greater, I've just paired it down to what I think is necessary.
OrderID
123
456
789
246
357
456
123
Result I'm looking for:
OrderID || Price
123 3
456 3
789 3
246 3
123
357 3
456
579 3

MySQL Trouble with a query

I have a table that for example it contain 1000 records. The query that I'm trying to do, is for get some like this:
substring_part_name number_of_warehose number_of_parts
156 1 50
156 2 140
156 3 300
180 3 130
120 1 80
120 2 300
And so obtain the 1000 records.
The trouble is this, the part_name is something like this: x_156, b_156, d_156, h_120, f_120 and so on. Every part has its corresponding warehouse.
The first column i get it on this way: distinct(substring(part_name,3)) as substring_part_name, I only want the last part of the name, How i can obtain that result??
My query is this:
select distinct(substring(part_name, 3)) as substring_part_name, count(#the number of parts by ware_house), ware_house from ware_houses
group by substring_part_name;
Use a negative integer for the SUBSTRING (-3) to get the last three characters.
select
distinct(substring(part_name, -3)) as substring_part_name,
number_of_warehouse,
number_of_parts
from table
You can also use RIGHT:
distinct(right(part_name, 3)) as substring_part_name

Mysql pivot table (concat) misuse (processing time)

I want to pivot the example below, I'm using:
SELECT Person, GROUP_CONCAT( Var ) , GROUP_CONCAT( Val )
FROM table
GROUP BY Person
This works fine, but... it takes about 20 seconds per line and my table has +/- 2.500.000 records :-P
(BTW; the table below is an example, not the actual one)
id person Var Val
-------------------------------
1 Bob Height 185
2 Bob Weight 74
3 Bob Age 40
4 Hank Height 193
5 Hank Weight 90
6 Hank Age 45
7 Bert Height 180
8 Bert Weight 85
9 Bert Age 43
PS:
Besides an answer (what would make you awesome) I also would like to know what is 'wrong' with this example (makes you even more awesomer)
There's nothing wrong with it, except that I'd write it like this
SELECT Person, GROUP_CONCAT(CONCAT(Var, ': ', Val))
FROM table
GROUP BY Person
because when you split it up in two columns, you won't know which Val belongs to which Var.
Apart from that, do you have indexes defined on those columns? If not, play around to see which index works best, separate indexes for each column or compound indexes on person, var, val.

MySQL SELECT query - one cell to multiple rows

In MySQL I have one particular cell with data something like this
5,6,7,8,9
If I need to search for specific 2 numbers one after another I do a query with LIKE statement for those 2 particular numbers. For ex. I need to check if there's a row with numbers 6 & 7 *in a row* I do
SELECT * FROM table WHERE column LIKE '%,6,7,%' OR column LIKE '%,6,7' OR column LIKE '6,7,%'
It's little redundant and clumsy. If I 'convert' those numbers into multiple rows, for ex. every number would become it's own row with column 'numbers' ordered with 'sort' column so I know the order of rows.
id numbers sort
55 8 4
56 6 2
57 5 1
58 7 3
59 9 5
...
What's the identical query for this case? So I would have the same result as with the query above. I need to order the query with sort column and check if the numbers 6,7 are occurring one after another with that sorting.
Should be something like this. (if I understood right your problem). It will return nothing if the 2 numbers are not in sequence by the sort column.
select *
from table t1
join table t2 on t1.sort=t2.sort+1
where t1.numbers=6 and t2.numbers=7
if you do not know which one should be first you can use it like this:
select *
from table t1
join table t2 on t1.sort=t2.sort+1 or t1.sort+1=t2.sort
where t1.numbers=6 and t2.numbers=7
Are the numbery always incremented by one or can they have any value?
I'm proposing the following table structure
id col1 col2 rowid
1 1 2 1
2 2 3 1
3 1 4 2