how to get the rest of the positions on my position table - mysql

Hello everyone I am trying to get the full count on my table because right now it is showing only one position instead of 5 if anyone can help that would really be appreciated
select position_name, state,count(*) -> from positions -> join employees on (employees.position_id = positions.position_id) -> ;
I am trying to get a table count of all the positions in each state.

You need to group by position_name and state to get each count
select p.position_name, p.state, count(*)
from positions p
join employees e on e.position_id = p.position_id
group by p.position_name, p.state

Related

How do add data to multiple columns at the same time

I got this last task before I can go to bed...
Make a query that shows the name(not the id) of players who have won the lottery more than once, how many times they've won and the name(not the id) of the municipality they live in.
Players-table: PlayerNum, Name, Address, MunicipalityID
Winners-table: PlayerNum, DrawID
Municipality-table: MunicipalityID, County, Population, Name
Thank you sooo much in advance!!
You need to join the tables and do a sub query on the winner table using count and group by the join the result set with player
Not sure what the draw table does
You really should make an attempt instead of just asking for the solution.
Your starting point is to find the users who have won more than once. This is a simple GROUP BY of PlayerNum and the HAVING clause to limit the result based on the COUNT -
SELECT PlayerNum, COUNT(DrawID) AS num_wins
FROM Winners
GROUP BY PlayerNum
HAVING num_wins > 1
The next step is to add the names of the players. For this you need to join to the Players table and I have added table aliases (w & p) to avoid retyping the full table name each time -
SELECT p.Name, COUNT(DrawID) AS num_wins
FROM Winners w
INNER JOIN Players p
ON w.PlayerNum = p.PlayerNum
GROUP BY w.PlayerNum
HAVING num_wins > 1
And then finally the join to Municipality to get the Name with a column alias as we already have a Name column -
SELECT p.Name, COUNT(DrawID) AS num_wins, m.Name AS MunName
FROM Winners w
INNER JOIN Players p
ON w.PlayerNum = p.PlayerNum
INNER JOIN Municipality m
ON p.MunicipalityID = m.MunicipalityID
GROUP BY w.PlayerNum
HAVING num_wins > 1

top 10 scorers by season

How I can get top 10 scorers by seasons.
So it shows last season top 10 scorers...
I've tryed left join into table, but it goes broken showing 2 player and counts all goals to first player.
My sqlfiddle:
http://sqlfiddle.com/#!9/b5d0a78/1
You got it almost right.
You want to group match_goals by player ID (match_player_id), but then you should not select goal_minute or any other per goal data.
After grouping by player, then you can create a column for COUNT(match_player_id) this will give you the number of goals, you can also use this column to order the results.
Your joins and conditions are correct I think.
EDIT
I think your schema needs a few tweaks: check this http://sqlfiddle.com/#!9/f5a75b/2
Basically create direct relations in the match_players and match_goals to the other tables.
I think the query you want looks like this:
SELECT p.*, count(*) as num_goals
FROM match_goals g INNER JOIN
match_players p
ON g.match_player_id = p.id INNER JOIN
matches m
ON m.id = p.match_id
WHERE p.is_deleted = 0 AND
g.is_own_goal = 0 AND
m.seasion_id = <last season id>
GROUP BY p.id
ORDER BY num_goals DESC
LIMIT 10;
Note that the teams table is not needed. The SELECT p.* is allowed because p.id (the GROUP BY key) is unique.

Removing an item from the result if it has a particular parameter somewhere in the table

I've been looking all around stack overflow and can't seem to find a question like this but its probably super simple and has been asked a million times. So I am sorry if my insolence offends you guys.
I want to remove an attribute from the result if it appears anywhere in the table.
Here is an example: I want display every team that does not have a pitcher. This means I don't want to display 'Phillies' with the rest of the results.
Example of table:
Here is the example of the code I have currently have where Players is the table.
SELECT DISTINCT team
FROM Players
WHERE position ='Pitcher' Not IN
(SELECT DISTINCT position
FROM Players)
Thanks for any help you guys can provide!
You can use NOT EXISTS() :
SELECT DISTINCT s.team
FROM Players s
WHERE NOT EXISTS(SELECT 1 FROM Players t
where t.team = s.team
and position = 'Pitcher')
Or with NOT IN:
SELECT DISTINCT t.team
FROM Players t
WHERE t.team NOT IN(SELECT s.team FROM Players s
WHERE s.position = 'Pitcher')
And a solution with a left join:
SELECT distinct t.team
FROM Players t
LEFT OUTER JOIN Players s
ON(t.team = s.team and s.position = 'pitcher')
WHERE s.team is null
Use NOT EXISTS
Query
select distinct team
from players p
where not exists(
select * from players q
where p.team = q.team
and q.position = 'Pitcher'
);
Probably the easiest way to approach this problem is to aggregate players over team:
select team
from players
group by team
having sum(case when position = 'Pitcher' then 1 else 0 end) = 0;
The big advantage is that you'd read the table only once.
Another advantage is that you can easily adjust the HAVING clause to have more elaborate queries, e.g. get all teams that have at least one catcher, two outfielders, and no pitcher.

mysql left join after inner join gives wrong record set

SELECT
op.sub_order_id,
s.supplier_id,
GROUP_CONCAT(opb.box_id SEPARATOR ','),
op.delivery_country
FROM
order_p op
INNER JOIN
supplier s ON s.supplier_id = op.supplier_name
LEFT JOIN
order_boxes opb ON op.sub_order_id = opb.sub_order_id
WHERE
op.order_active=0
AND op.ship_date>='2013-01-01'
AND op.ship_date<='2013-04-24'
ORDER BY op.ship_date DESC
I am not very good at joins, so bear with me.
I have this query where I need to select all data from order table between the given date range, such that a matching supplier mapping exists in the supplier table. Now along with this info, I need to also fetch any boxes, if it exists in the order_boxes table linked by sub_order_id fields.
Now, if I just join the order and supplier tables, I get the correct record-set of about 1000 records, but as soon as I try to add in the boxes table, I get only one row. I am guessing this happens because there is only one record in the boxes table, but I need it to fetch all records from order table along with the box table. If I try INNER JOIN instead of LEFT JOIN I get only 1 record of the matching suborder as mapped in the box table, but with Left join, I get the first record within the date range, irrespective of the sub_order_id and the value in the boxes table.
Please help. Also if you need any more info, please let me know.
EDIT : I am sorry I didn't post the full thing before, but I think the problem is with GROUP_CONCAT, which groups the entire recordset where as I want to only group the records in the boxes table....maybe I should use a subquery?
Current output
179-1 | 2 | 2,4,3 | Canada
Expected output
168-1 | 1 | | Texas
.....
......
179-1 | 2 | 2,4,3 | Canada
.....
......
Try Converting Date
WHERE
op.order_active=0
AND op.ship_date>=
convert(datetime,cast('2013-01-01' as varchar),101)
AND op.ship_date<=
convert(datetime,cast('2013-04-24' as varchar),101)
ORDER BY op.ship_date DESC
SELECT * FROM
(
SELECT * FROM order op INNER JOIN
supplier s ON s.supplier_id = op.supplier_name
WHERE
op.order_active=0
AND op.ship_date>='2013-01-01'
AND op.ship_date<='2013-04-24'
ORDER BY op.ship_date DESC
) AS a
LEFT JOIN order_boxes opb ON a.sub_order_id = opb.sub_order_id
Try this and tell me whether this help you or not.

Left join, how to specify 1 result from the right?

This one is fairly specific, so I'm hoping for a quick fix.
I have a single result in my leaderboard table for each team. In my teams table, I have several results for each team (one result per game to enable team development history).
I want to show each team in the leaderboard once, and have teamID replaced by strName. Problem is, my left join is giving me one record for each team result; I just want a single record.
SELECT * , a.strName AS teamName
FROM bb_leaderboards l
LEFT JOIN bb_teams a ON ( l.teamID = a.ID )
WHERE l.season =8
AND l.division =1
ORDER BY l.division DESC , points DESC , wins DESC , l.TDdiff DESC
LIMIT 0 , 30
What do I need to do to this to get a 1:1 output?
You could do a SELECT DISTINCT instead, but you'll have to narrow down your select a bit. So:
SELECT DISTINCT l.*, a.strName AS teamName
...
That should filter out the duplicates.