I have a big task that I need some help with.
My goal is the following table structure:
type: type of car
number_of_cars: number of cars for each car type
number: number of people driving each car type
average: average number of people driving each car type
median: number of people driving each car type
max: max value of people driving each car type
min: min value of people driving each car type
standard deviation: standard deviation of number of people driving each car type
My data table looks like the following:
id type people
-----------------------
1 subaru 1
2 bmw 5
3 tesla 2
4 tesla 3
5 subaru 4
6 tesla 1
7 tesla 3
8 subaru 1
9 bmw 5
10 subaru 7
11 subaru 7
12 ford 2
13 ford 4
14 subaru 6
15 ford 3
16 tesla 2
17 tesla 1
18 tesla 1
19 tesla 1
Where id is a unique identifier, type is the type of car, and people is the number of people driving this car.
How do I create one giant MySQL query that gives me the results I need for my table?
Help is appreciated!
Ps. I know that MySQL is not necessarily the best approach to gather statictical data like this, but it should be possible, right?
All data the statistical data you want can be gathered using GROUP BY statement and built-in functions of mysql. Simply read about aggregate functions in mysql.
Only thing you won't find there is median. Mysql doesn't have built-in function for that but you can easily find some way to do that just by googling it
Related
I am stuck, and can't find any solution.
productId
shop.title
Qty
productName
rpId
15
Mark
20
Bread
2
2
John
10
Jame
3
4
Tonny
5
Eggs
4
4
Grey
5
Eggs
5
15
Boby
25
Bread
6
4
Ruby
15
Eggs
7
$request->rpId = [3,4,8,9];
$purchaseOrders = DB::table('request_purchase_detail')
->join('request_purchase','request_purchase.id','=','request_purchase_detail.rpId')
->join('shop','shop.id','=','request_purchase.shopId')
->select('request_purchase_detail.productId', 'shop.title',
'request_purchase_detail.productQuantity as reqQty', 'request_purchase_detail.rpId',
DB::raw('sum(request_purchase_detail.productQuantity) as productQuantity'))
->whereIn('rpId',explode(",", $request->rpId ))
->get();
Explanation:
Let's Suppose I am making a purchase Order for all shop's required Items, and need the sum of total qty but at the same time I need individual too, i,e. Ruby required 7 packs of eggs.
My Query does not understand this 😣
return sum of the qty i,e. 20 packs for eggs, but shop.title and individual shop required qty selected first one only i.e, Tonny required 4 packs of eggs.
Ruby required 7 packs of eggs OR Grey required 5 packs of eggs are not returning in that query.
Please try to understand my problem, Maybe I am a little bit stupid, just like my English.
But it's 5:00 AM here and you people are now my only Hope. 🤐
I have a this Table in MS Access:
Table1
ID EMP ROLE ASSESS
1 JOE Weld 4
2 TOM Weld 4
3 JIM Ship 4
4 PAT Ship 3
5 JAY Weld 4
6 TIM Ship 4
"ROLE" is short text and "ASSESS" is a number field. "ASSESS" is assessing employees' roles on a scale of 1-4. I want to collect and total assessments that are "4" for each role.
Returning something like:
ROLE TOTAL
Weld 3
Ship 2
I however have around 100 different roles that I am needing to do this with. Is there a way with SQL or a combination of query and macro to make this work? I am at a loss.
Thank you.
You could use a where clause to filter just the the assesses that are 4 and a group by clause to aggregate them:
SELECT role, COUNT(*)
FROM table
WHERE assess = 4
GROUP BY role
Table
id name(varhcar)
2 15
3 15,23
4 1315,424
5 1512,2323
6 23,15,345
7 253,234,15
I need to find out those values which contains 15 which mean i need 2,3,6,7 not 4,5.
Above is sample data, in real time it can be any number.
Can anyone please help me?
If your database is small, consider using find_in_set function:
select * from your_table
where find_in_set('15',name);
Consider change the model to master-detail table to increase the speed if you have a big table.
This is the kind of relational model you could adopt to make this an easy problem to solve:
TABLE: records
id
2
3
4
5
6
7
TABLE: values
record_id value
2 15
3 15
3 23
4 1315
4 424
5 1512
5 2323
6 23
6 15
6 345
7 253
7 234
7 15
Then you can query:
SELECT DISTINCT id FROM records
INNER JOIN values ON records.id = values.record_id AND values.value = 15
This is the only way you can take good advantage of MySQL's query optimizer.
Not that it's impossible to do what you're trying to do, but it kind of misses the point.
If you're already storing data in this format, you should write a one-time migration to transfer it to this "normalized" format in the programming language of your choice, using something like Java's split or PHP's explode.
I'm running a site with user ranking-list based on elo-rating.
I want to provide more statistics to users and I have pretty much covered, but cant really figure out how to make queries for these ones.
Players highest ranking points
Players ranking points history (for graph)
MySQL db has two tables for statistics: ranking_statistics which holds overall statistics:
id, ranking, wins, losses, draws, total6m, total8m, total10m
and ranking_matches which holds statistics for matches played:
id, home_id, away_id, home_ranking, away_ranking, home6m, away6m, home8m, away8m, home10m, away10m, datetime
Here is some sample data from ranking_matches:
46 442 456 30 -30 6 6 5 3 3 4 2013-10-14 21:22:58
54 456 480 34.0391 -34.0391 6 4 6 4 2 1 2013-10-16 17:33:37
55 473 475 30 -30 9 9 7 8 6 4 2013-10-17 03:06:41
and from ranking_statistics:
442 1029.97 7 2 6 120 89 55
456 1003.93 6 2 5 99 84 65
I would want to retrieve players highest ranking points on history (ranking_statistics.ranking holds current points) and that could be retrieved from ranking_matches by quering all matches with players id as home or away and then calculating all ranking changes with highest score remembered (starting points is 1000). With this query, a graph of points history would be drawn also.
I have tried to understand how this is done but could not get it by myself and there doesnt seem to be any similar questions posted (or atleast I did not found any)
Results could be also calculated with PHP because all the data is output with it.
Sample output:
Player id: 442
Current rating: 1029.97
Highest rating: 1054.32 (on 10-23-2013)
For history graph, 2 values need to be retrieved to be able to draw a history line graph, date and rankingpoints.
This is somewhat of an open question, however my main reason for asking is to either know the 'name' of such a task, or to be pointed in the right direction of what I should be looking for.
What I am trying to achieve is a database of items which can be tagged. The format to which will be strings of text.
For example, if we are talking about Movies.
table_movies
movie_id
movie_title
movie_genre
table_tags
tag_id
movie_id
tag_text
The Movie will only be entered into its respective table once. In the table_tags table, the movie_id can be listed an unlimited amount of times, one for each tag.
table_movies
movie_id movie_title movie_genre
1 Notting Hill Romantic Comedy
2 Jurassic Park Science Fiction, Action Adventure
3 Grease Musical
4 Salt Action Adventure
5 Gladiator Drama, Epic, Historical, Adventure, Action
6 E.T. Science Fiction, Action Adventure, Fantasy
table_tags
tag_id movie_id tag_text
1 1 Notting
2 1 Hill
3 1 London
4 1 Market
5 2 Jurassic
6 2 Park
7 2 90s
8 2 T-Rex
9 2 Amber
10 2 Egg
11 3 Grease
12 3 Cars
13 3 Diner
14 4 Salt
15 4 Undercover
16 4 Twist
17 4 Agent
18 5 Gladiator
19 5 Shield
20 5 Roman
21 1 Boy gets girl
22 3 Varsity
23 3 Rock & Roll
24 5 Honour
25 5 Arena
26 6 E.T.
27 6 boy
28 6 alien
29 6 phone home
30 7 Home
31 7 Alone
32 7 robbers
33 7 boy
...And so on. This table would be massive.
When a user visits the site, they can search for one word or a string of text. For example:
"boy gets girl"
What I would like to know how to do, or what it is called is to search the database for:
"boy", "gets", "girl", "boy gets", "gets girl", "boy girl", "boy gets girl".
Can this be done automatically? Is there a function for this?
Your question is really split into two sections:
Searching a database for arbitrary text
The name for this is "pattern matching", and in SQL it is implemented in a variety of ways:
The LIKE condition
Regular expressions
MATCH-AGAINST
Of the three, the last is probably the most powerful. You can easily do what you want with:
SELECT * FROM table_tags WHERE MATCH(tag_text) AGAINST('boy gets girl');
The best part? Rows are returned in order of relevancy.
Getting a movie based on a tag
I will assume because you are using this two-table model that you already know how to do joins, but just in case here's a resource for you. Once you have the tag ID, you can easily get the movie ID and from there the movie entity itself.