I have a table
user_name id status calls
===========================================
apple 21 cars 67
apple 21 bikes 85
apple 21 skates 6
apple 21 pajs 56
orange 34 bikes 9
orange 34 disks 3
orange 34 cars 21
orange 34 pajs 76
I want to add up all the calls for status's that are cars, bikes and skates only and for each user_name. I have experminted with SUM but after several hours of late night, earnest attempts its pretty clear to me that this is going to require more than that. Can anyone point me in the right direction please?
My Table
select user_name, status, sum(calls) as calls
from table
where status in ('cars', 'bikes', 'skates')
group by user_name, status
Related
So, I'm managing a table where it's stored the scores of a particular competition.
The table looks like this:
ENTRY_ID TEAM_ID DATE PLACE SCORE
1 1 2021-10-12 Ireland 64
2 2 2021-10-12 Ireland 31
3 3 2021-10-12 France 137
4 2 2021-10-12 France 61
5 5 2021-10-12 France 38
6 1 2021-10-12 France 66
7 2 2021-10-12 Italy 17
8 3 2021-10-12 Italy 61
9 1 2021-10-12 Italy 74
The competition is held at three different places at the same time, with technically all teams being able to have teams in all of them.
Each team however can only win one point so, in the example, it's possible to see that Team 1 would win both in Italy and Ireland, but it should be awarded only one point for the highest score, so only Italy. The point in Ireland should go to the second place.
The query I was trying to get the results is:
SELECT `TEAM_ID`, `PLACE`
FROM `COMPETITION`
WHERE `date` = "2021-10-12"
GROUP BY `PLACE`
ORDER BY `SCORE` DESC, `id` ASC
LIMIT 3
So I could retrieve all three winners with no further processing.
The results I'm trying to achieve should repeat neither the TEAM_ID nor PLACE, in this particular example it should output:
3 FRANCE (Since it has the highest score in France at 137)
1 ITALY (For the highest score in Italy at 74)
2 IRELAND (For the second-highest score in Ireland, since Team 1 already won in Italy)
The production model of this table has far more entries so it's unlikely there would be any clashes with too many second-places.
How can I achieve that?
I have two tables Users and Reputation.
users table:
id name sites intsize extsize
------- ------------------ ------------ ---------- ---------
1 Ryan phish.com 10 60
2 Janice test.com 20 50
3 Tori yahoo.com 30 40
4 John phish.com 40 30
5 Brett facebook.com 50 20
6 Henry google.com 60 10
reputation table:
score sites
------- ------------
10 phish.com
25 test.com
87 yahoo.com
85 facebook.com
87 google.com
12 badsite.net
3 g00gl3.xyz
I want to return all the name, the sum of the intsize and extsize columns from the users table as well as the associated score column from my reputation table that is associated with my sites column such that this is my result:
name sites score total size
---------- ------------ ------------ --------------
Ryan phish.com 10 70
Janice test.com 25 70
Tori yahoo.com 87 70
John phish.com 10 70
Brett facebook.com 85 70
Henry google.com 87 70
Would the query be:
SELECT sitecount.name, sitecount.sites, sitecount.sum, reputation.score
FROM (SELECT name, site, SUM(intsize+extsize) FROM users GROUP BY sites)
AS sitecount, reputation AS reputation
WHERE sitecount.sites = reputation.sites
Or should my query include a join? From research it seems like I can use left join (https://www.w3schools.com/sql/sql_join.asp) since I am only trying to return the all the sites that exists in my users table?
Use join
select name, u.sites, score,intsize+extsize as totalsize
from users u join reputation r on u.sites=r.sites
So I have two tables, table 1:Music Styles being with fields with StyleID and StyleName, with StyleName being a genre and table 2:entertainers with fields SyleID and EntertainerID.
I need help finding is:
1) What’s the most popular genre of music style in the database of entertainers?
This is what i have so far:
SELECT StyleID, StyleName
from EA_Music_Styles
WHERE StyleID = (SELECT StyleID
from EA_Entertainer_Styles
WHERE StyleID = (SELECT MAX(StyleID)
FROM EA_Entertainer_Styles));
But i got an error code of "Subquery returns more than 1 row"
I need help, don't know exactly what to do? First time beginner.
Here are the tables:
Table 1:
StyleID StyleN
1 40's Ballroom Music
2 50's Music
3 60's Music
4 70's Music
5 80's Music
6 Country
7 Classical
8 Classic Rock & Roll
9 Rap
10 Contemporary
11 Country Rock
12 Elvis
13 Folk
14 Chamber Music
15 Jazz
16 Karaoke
17 Motown
18 Modern Rock
19 Rhythm and Blues
20 Show Tunes
21 Standards
22 Top 40 Hits
23 Variety
24 Salsa
25 90's Music
Table 2:
StyleID EntertainerID
3 1003
3 1008
4 1010
6 1007
6 1008
7 1009
7 1011
7 1012
8 1003
10 1001
10 1013
11 1007
13 1004
13 1012
14 1009
14 1011
15 1005
15 1013
17 1002
19 1002
19 1005
20 1001
20 1011
21 1001
21 1009
21 1010
22 1006
22 1010
23 1002
23 1006
24 1005
24 1006
SO the result would output the two StyleID's, 7 & 21 and StyleName which would be Classical & Standards
You just need to join your two tables and get the max count for entertainers
SELECT ms.StyleName, count(*) most_popular
from EA_Music_Styles ms
INNER JOIN A_Entertainer_Styles es
ON ms.StyleID = es.StyleID
group by ms.StyleName
order by 2 desc
limit 1
This will give you the most popular (I take that it is the one that has more rows) StyleName. I count the ocurrences of StyleName and order it desc getting only the first result of this.
Here is how you can solve the problem.
In your entertainers table user group by StyleID while also selecting the count. This will give you the styleID count which you can sort by descending and limit 1 to find to most popular one
select StyleID, count(StyleID) c from EA_Entertainer_Styles
group by StyleID
order by c desc
limit 1;
Now use the styleID form the first step to get the StyleName from your table Music.
select StyleName from EA_Music_Styles
where StyleID = id_from_step1
You can use this two in a single query by joining them as in the answer shown by Jorge Campos
Max gives you the highest number in the database, not the most used. You should use count for that.
Next example should give correct result.
select StyleName from EA_Music_Styles
where StyleID = (
select StyleID
from EA_Entertainer_Styles
group by StyleID
order by count(StyleID) desc
limit 1)
I have a db for a menu which tracks clicks on it. The menu has categories and subcategories and I'm trying to get the amount of clicks for each category but in the db, the clicks will register to the subcategory if the item is in one, otherwise the clicks are counted in the category. I have a query that will get clicks for all subcategories (category_type 3) but I need to add them with the clicks from their parent category (category_type 2). There is a table called CategoryHierarchy that maps each category to it's parent category. This is what I have:
SELECT IFNULL(SUM(`MenuEntryAnalytics`.`opened`), 0) AS `clicks`,
`Categories`.`id`,
`Categories`.`name`,
`Categories`.`category_type`,
`CategoryHierarchy`.`parent_id` AS `parent`
FROM `MenuEntryAnalytics`
INNER JOIN `MenuEntries`
ON `MenuEntryAnalytics`.`menu_entry_id` = `MenuEntries`.`id`
LEFT JOIN `MenuEntryToCategory`
ON `MenuEntryAnalytics`.`menu_entry_id` = `MenuEntryToCategory`.`menu_entry_id`
RIGHT JOIN `Categories`
ON `MenuEntryToCategory`.`category_id` = `Categories`.`id`
RIGHT JOIN `CategoryHierarchy`
ON `Categories`.`id` = `CategoryHierarchy`.`category_id`
WHERE `Categories`.`category_type` = 3
GROUP BY `id`;
Results:
clicks id name type parent
=============================================
2032 3 Appetizers 3 2
455 4 Salads 3 2
680 6 Sandwiches 3 5
424 7 Burgers 3 5
584 9 Pizza 3 8
466 10 Kids Menu 3 8
1445 12 Soda 3 11
1089 13 Signature Cocktails 3 11
391 14 Bottled Beer 3 11
167 15 Wine 3 11
0 17 Events 3 16
0 18 Sponsors 3 16
186 19 Dessert 3 11
621 26 Restaurants 3 22
263 27 Bars 3 22
112 28 Services 3 25
254 29 Amenities 3 25
67 30 Exclusive Benefits 3 25
190 31 Area Attractions 3 24
14 32 Entertainment 3 24
2 33 Shopping 3 24
117 34 Transportation & Tours 3 24
471 35 Mixed Drinks 3 11
541 36 Draft Beer 3 11
if I GROUP BY parent then I can get most of what I need (all the clicks from subcategories of each category) but this doesn't get the clicks counted towards categories (as opposed to subcategories, i.e. category_type 2). I'm stuck trying to add that part in, all I can think of is using a subquery but there's no way of identifying which category I'm looking at, thus I get a subquery with multiple rows.
PS I do not have permission to restructure the db.
Since the parent ID is in the same namespace as the ID, you can simply use IFNULL to pick the parent if it exists, or otherwise the ID. And use that as your grouping strategy.
You may also want to select the same data out as an actual column.
GROUP BY
IFNULL(CategoryHierarchy.parent_id, Categories.id, CategoryHierarchy.parent_id)
I have a table that holds timestamped messages from a sender to a receiver, and it looks like:
mes timestamp sender receiver
10 2014-04-13 12:22:25.000 1 72
10 2014-04-13 12:22:25.000 1 91
10 2014-04-13 12:22:25.000 1 58
16 2014-02-20 20:09:06.000 3 35
16 2014-02-20 20:09:06.000 3 54
17 2014-03-05 14:55:28.000 1 65
18 2014-03-07 14:55:28.000 2 97
19 2014-03-09 14:55:28.000 2 97
My table holds 3 millions rows like these, and I am trying to group results according to timestamp intervals, counting the number of messages in each month between each pair of sender-receiver. Something like:
timestamp sender receiver count
2014-04 1 72 1
2014-04 1 91 1
2014-04 1 58 1
2014-02 3 35 1
2014-02 3 54 1
2014-03 1 65 1
2014-03 2 97 2
I really have no clue how to specify my clause in mysql, so I apologize for not providing a snippet of my not-working code... should I use something as a switch control and manually specify the time interval? or is there something more specific in mysql to manage tasks like this?
This is fairly straight-forward GROUP BY with multiple levels:
SELECT CONCAT(YEAR(tstamp),'-',MONTH(tstamp)) as tstamp, sender, receiver, COUNT(*) AS cnt
FROM yourtable
GROUP BY YEAR(tstamp), MONTH(tstamp),sender,receiver;
I'm using tstamp as field name for "timestamp" to avoid conflicting with reserved words (timestamp).