Finding First Instance of Multiple Variable in Single Column in SQL [closed] - mysql

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 days ago.
Improve this question
I've been trying to write some queries and subqueries but have struggled with one in particular.
Using the database at the below link, I am trying to find a way to separate multiple variables from a single column in to two.
https://www.w3resource.com/sql-exercises/soccer-database-exercise/subqueries-exercises-on-soccer-database.php
I can return the first match with a player scoring a goal from a penalty
`
SELECT a.player_id,MIN(b.play_date) AS firstmatchscoringpenalty FROM goal_details a
JOIN match_mast b ON a.match_no = b.match_no
WHERE a.goal_type = 'P'
GROUP BY a.player_id`
Player_ID
Firstmatchscoringpenalty
160333
2016-06-26
160235
2016-07-03
and the first match in which a player scored a "normal" goal`
`SELECT a.player_id,MIN(b.play_date) AS firstmatchscoringnormal FROM goal_details a
JOIN match_mast b ON a.match_no = b.match_no
WHERE a.goal_type = 'N'
GROUP BY a.player_id`
Player_ID
firstmatchscoringnormal
160236
2016-06-27
160316
2016-07-01
I would, however, like to return a result for all players who scored both a penalty and normal goal that includes both results across three columns, so something like:
Player_ID
Firstmatchscoringpenalty
firstmatchscoringnormal
Cell 1
Cell 2
Cell 3
Cell 4
Cell 5
Cell 6
Any ideas on how to do this would be much appreciated.

Related

a SQL query for codeigniter project [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
In my codeigniter project, User enter course code and regional center by using drop downs. According to this both data, system should calculate total income of the last 3 years for relevant course code AND regional center.
As an example,
In “budget” table, course code-ABC1123 and Perth regional center has 4 data. The system should find the latest 3 years of income for the "Sum of income" column. The course code and regional center are selected by user.
Could you please tell me the SQL query for the above mentioned statement using CodeIgniter?
select sum(income)
from budget
where code = ? and center = ?
and year > (
select max(year) - 3
from budget
where code = ? and center = ?
)
The inner query gets the latest year for code & center and then subtracts 3. And the outer query then sums up all incomes for code & center for years that are greater than this calculated year, so for the last 3 years.
For CodeIgniter3 this would be:
$this->db->query(
'select sum(income)
from budget
where code = ? and center = ?
and year > (
select max(year) - 3
from budget
where code = ? and center = ?
)', array($code, $center, $code, $center)); // ->row() or ->row_array()
And for CodeIgniter4 use $db->query(...) instead of $this->db->query(...).

Why is my average query showing incorrect values? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I asked a question a few hours ago that was marked as closed and referred me a link that did not clear up my confusion.
I am trying to query a sports database in MySQL to list the names of players who are above average age compared to their teammates. Ideally, I want to group by team, find the average of each team, and compare that to each respective player on that team.
My results from this query seem to be comparing players to the entire databases' average, rather than the average of the team. Can anyone correct my query or propose an alternate query to get the correct data? A friend of mine suggested perhaps using two copies of the tables, but that is beyond the scope of my limited MySQL skills.
My relational schema are as follows:
player(player_name, age, position)
plays_for (player_name, team_name)
SELECT player.player_name, player.age
FROM
plays_for
INNER JOIN player ON player.player_name=plays_for.player_name
WHERE (SELECT AVG(age) FROM player
GROUP BY plays_for.team_name1)< player.age
Your WHERE statement does not include the team grouping. I personally like WITH statements which seems to be the direction your friend was going.
> WITH average_ages AS ( SELECT AVG(p.age) AS average_age, pf.team_name
> FROM player p join plays_for pf on p.player_name = pf.player_name
> GROUP BY pf.team_name) aa
> SELECT player.player_name, player.age
> FROM plays_for
> INNER JOIN player ON player.player_name=plays_for.player_name
> INNER JOIN average_ages ON plays_for.team_name = average_ages.team_name
> WHERE player.age > average_ages.average_age;
The WITH statement at the top creates a temporary table of average ages and then joins it to the plays_for table.
The first few rows of the entire SELECT query before the WHERE statement would look like this
Name Age Team Average_age
Tara 51 KOs 25
Bomb 45 KOs 25
Jess 20 BES 30
Buster 40 BES 30

I have this query that is driving me crazy [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have this damn query that I'm trying to figure out but it driving me totally crazy cause I can't find a way to start building it.
This is the table structure on which I will query data.
----------------------------------------------------------------------------
| id | id_user | id_game| id_question | user_answer | answer_time | points |
----------------------------------------------------------------------------
the table is called game_user_answers and I'm supposed to find the winner of a quiz game.
I have another table called games in which i store id game and the game_start_time.
If the answer has 0 points than the user didn't answer correctly.
Basically what I want to do is to get the id_user that has more points combined from all answers he's given and his last answers time is closest to the game_start_time.The game id and the game_start_time will be passed as parameters to the function in which the query will be executed so don't worry about them.
Please help me out.
This is not the answer, but the start point for you.
Here is sqlfiddle: http://sqlfiddle.com/#!9/197dd9/1
You should add more data into that fiddle.
After that use my query to see some results and play with that query until you get to the closest result of what you expect to get. And once you get to the point when you can't get your goal, come back with your fiddle and query prepared and ask community for the help again.
SELECT
g.*,
gua.*
FROM games g
LEFT JOIN game_user_answers gua
ON g.id = gua.id_game
AND gua.answer_time>g.game_start_time
AND points>0
WHERE g.id = 1
AND g.game_start_time = '2016-01-01 00:00:00'

Count number of occurrences by Name [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
First timer here so please excuse me if my question is somewhat confusing. I am attempting to display things in a dynamic table using data pulled from a SQL table and I am having difficulty figuring out the logic.
The table structure looks something like this
NAME Homeruns Hits Bunts Total
Jeff 0 3 1 4
Sally 2 4 0 6
John 3 7 0 10
The data in the table is structured in a way that the type of play being made (Home run, hit, bunt, etc) is in a single column. I'll call this column PLAY. The name is in a separate column. This column will be called NAME. Table name is BASEBALL.
This is called a pivoting query. You can do this in standard SQL with aggregation:
select name,
sum(case when play = 'HomeRun' then 1 else 0 end) as HomeRun,
sum(case when play = 'hit' then 1 else 0 end) as Hit,
sum(case when play = 'bunt' then 1 else 0 end) as Bunt,
count(*) as Total
from baseball bb
group by name;
This is a pretty simple SQL query, so I'm guessing your expertise is less on the database side than on the programming side. I would suggest that you take the time to learn the SQL language properly.

Executing "simple" sql query [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Here is data in my table :
id name surname place
1 test1 isdad YES
2 test2 spreda YES
3 test3 me NO
4 test4 smallvile YES
What I'd like to get, all ids where place='YES'. So Ids are
1
2
4
and the result count is 3. I want to append that number to the end of the results, so the result should look like this:
1
2
4
3
There is almost definitely a better way to do this. That said, you could do:
SELECT id FROM myTable WHERE place = 'YES'
UNION ALL
SELECT COUNT(*) FROM myTable WHERE place = 'YES'
UNION ALL will append the result number as a new row rather than attempting to add it as another column.
Please don't do this; the results will be mixed, it stands in stalwart defiance of good software design and will be baffling to anyone who comes along later and has to work on this piece of code.
In MySQL, you can do something similar to this in a single query using WITH ROLLUP:
select id, count(*)
from myTable
where place='YES'
group by id
with rollup
SELECT id FROM TABLE WHERE place='YES'
will return with the ids what you want, and the rowcount will store the count number, if you use this with php or c++/c#