I have this query that is driving me crazy [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 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'

Related

Finding First Instance of Multiple Variable in Single Column in SQL [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 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.

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

MySQL - Calculating profit from joining two tables [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 2 years ago.
Improve this question
So I have two tables,
one has the product margin before tax, and
other table has the individuals products tax % (unfortunately in whole number format).
The code I'm using works, but some products are not taxed, and therefore have 0.
I'm trying to figure out how calculate their profit with the same formula I'm using.
SELECT o.product_id,
(((o.total_margin/1000)-(p.tax/100)*o.total_margin/1000)/1000) AS Profit
FROM merproduct_offers o
JOIN merchant_products p ON o.product_id = p.product_id
WHERE p.tax > '1'
Linked below are screenshots of the two tables I'm using. Thanks again!
You seem to want a left join and coalesce():
SELECT
o.product_id,
(((o.total_margin/1000)-(COALESCE(p.tax, 0)/100)*o.total_margin/1000)/1000) AS Profit
FROM merproduct_offers o
LEFT JOIN merchant_products p
ON o.product_id = p.product_id AND p.tax > 1
It is likely that the computation can be simplified, but it is hard to tell without an explanation of its purpose. Maybe something like this?
(o.total_margin / 1000 - COALESCE(p.tax, 0) * o.total_margin / 100000) / 1000 AS Profit

Complex SQL Query (sum entries) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am wondering if something like this could be implemented in SQL query.
Let say I have these tables:
Table Orders
id tax
01 800
02 255
Table DetailOrders
id price itemName
01 700 Book
01 500 Umbrella
01 100 Jacket
02 1000 Piano
Basically single entry of one table Orders corresponds to multiple entries in DetailOrders.
Is there are any way to write SQL query that would return something like this:
id tax sum-price all-names
01 800 1300 Book, Umbrella, Jacket
02 255 1000 Piano
It would sum the price of items with the same id, and somehow merge the names of the items with same id.
Could something like this be achieved?
How about something like
SELECT o.id,
o.tax,
sum(od.price) sum_price,
group_concat(itemName) all_names
FROM Orders o INNER JOIN
DetailOrders do ON o.id = do.id
GROUP BY o.id,
o.tax
Have a look at GROUP_CONCAT(expr)
This function returns a string result with the concatenated non-NULL
values from a group. It returns NULL if there are no non-NULL values.
It isn't hard:
select
o.id, o.tax,
sum(d.price),
group_concat(d.itemName)
from
orders as o
inner join detailOrders as d on o.id = d.id
group by
o.id