How do I sum multiple rows and show all columns? - mysql

Picture of the Query
SELECT *, SUM(match_attendance) FROM new_dashboard
Group BY worldcup_year;
I used this but don't know if it is correct? Is there like a proper way to do it? Via subquery or what not? Cause Im not sure showing if this is how people do it. I notice alot list all the column they wish to select then use the sum function but if you have like 20 columns that don't make sense.

Related

SQL query: search value in array inside cell

I have tried a lot of syntax but nothing is working.
I have MySQL 8.0.31 with phpMyAdmin 5.1.1.
My table looks like this (the column I want to filter by):
The thing is, I'm saving in this field some order numbers, and I need to make a SELECT statement in which I have one order number (for example, 1632) and I need to determine if this number is stored inside the "array" of orders in any of the rows (in this example, the desired result of the query is the third row).
I have tried the IN statement without result.
If I use the LIKE statement obviously works, but in case of larger numbers (like 16320), if I'm searching for 1632, I will find also this value.
The separator of the numbers, if works with another one, is able to be changed.
I would do it as such
SELECT * FROM Orders
WHERE Ordernes LIKE '%,$searchValue,%' OR Ordernes
LIKE '$searchValue,%' OR Ordernes LIKE '%,$searchValue';
If you add the seperators on both sides you will only find the value that you are looking for and not numbers that look like it.

How could SUM(column) give incorrect results in MySQL?

I have the following simple MySQL query, called from PHP:
SELECT foo_id, SUM(number_of_guests)
FROM guests
WHERE foo_id = $foo_id
GROUP BY foo_id
This works fine, except for one $foo_id, which returns about 2.5 times greater than the sum of the number_of_guests field.
What could cause this behavior for only a certain value of $foo_id?
Is there a better way to do this?
Your query should work. The error is most likely in the other method you are using to verify the result.
Is there a better way to do this?
Yes. Since you are only fetching one group there's no need for your GROUP BY clause:
SELECT SUM(number_of_guests)
FROM guests
WHERE foo_id = $foo_id
The problem is that there was one row with a large value for number_of_guests. I didn't see in in browsing the data because there are a few hundred rows. It didn't show up when I copied and pasted from HTML page into Excel because that row was missing most of the other columns, and the HTML page has all the columns.
Thanks for all your help!

Mysql sum as last row

Is it possible to have the SUM of all numaric fields in the last of a set of rows?
As of now, I'm using a very simple query such as:
SELECT
*,
SUM((UNIX_TIMESTAMP(end) - UNIX_TIMESTAMP(start))/3600)
FROM
times
in SQL you cant have a column that appears in only one row, likewise, you also cant have a row that doenst contain all the columns from the other rows.. So having a row that contains something unique is not possible. You can, however, add the calculated column to all rows in the dataset or do the calculation in the calling code after the data is returned.
I think what you are looking for is GROUP BY WITH ROLLUP you will find details on that in the MySQL manual.

MySQL: Use aggregate result in further calculation

I'm currently working on writing report generators. For one report I need to do a breakdown by a given characteristic (supplier, logging user, language, etc which for each row includes the name of the characteristic I'm interested in, the number of items that match that characterastic, and the percentage of total items this figure represents. The first two aren't a problem, the third is.
For example, to get a breakdown by language I'd be using a query like this.
SELECT lang_id,
COUNT(IF(open=TRUE,1,NULL)) AS lang_total
FROM table
GROUP BY lang_id;
This gives me the number of items per language.
I can get the total number of items in the table and store it in a variable simply enough with a plain count.
SELECT #totalOpen:=COUNT(*) FROM table WHERE open = TRUE;
Now I want to add a third column, which is the figure in lang_total divided by the value in #totalOpen multiplied by 100 (in other words, the percentage of all items that fit the criteria). Something along the lines of the following:
This is the bit I'm having trouble with, as because as far as I can tell you can't use aggregate columns in calculations.
SELECT lang_id,
COUNT(IF(open=true,1,NULL)) AS lang_total
(lang_total/#totalOpen)*100 as lang_percent
FROM table
GROUP BY lang_id;
I'm sure that there must be a way of doing this in MySQL, but I've not been able to track it down. Can anyone help out with this?
I read this question now for the first time. I know that probably it's too late to be useful for you but I would have solved in this way.
select lang_id,
sum(if(open= true,1,0)) as lang_total,
coalesce(sum(if(open= true,1,null)) / #r,0) as percentage
from table,(select #r:=count(*) from table where open = TRUE) as t
group by lang_id;

How to filter a list of items in a MySQL database?

I have list store in mysql table file1=(1,2,3,4,6,7) and other list file2 = (3,2,4,8,9,10,12) is not stored in table, i want compare both and result should be like
result=(6,7,8,9,10,12) then calculate the percentage. like 100*(result/file1+file2) in mysql data structure. i do not know how i will do it.
please know body know guide me or give me a small example.
thanks
Create a temporary table to store the "other" list that is not already in a table, and then use a join or union to get the result you want.
If you are wanting to select the values that are not in either list, then you can use GROUP BY and COUNT to count the occurrence of each number, and then HAVING to select those rows with a count of 1.
I don't think you can easily do that in pure sql. you need a procedural language to get the result in the first list, merge, sort, unique with the second list and compute the result.