a SQL query for codeigniter project [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 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(...).

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.

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

Trying to union these together but I can't figure out what it's telling me: Error converting data type varchar to bigint [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 years ago.
Improve this question
This is the code that I have going:
SELECT e.EncounterID, YEAR(e.EntryDate) AS 'Year', MONTH(e.EntryDate) AS 'Month', ac.Description AS 'PresentingProblem', c.ClinicName
FROM encounters AS e
JOIN clinics AS c
ON e.clinic = c.ClinicID
JOIN activitycodes AS ac
ON e.PresentingProblem = ac.ActivityCode
UNION
SELECT ec.Description, dc.Reason AS 'Discharge_Reason', a.Duration, a.ExtraTime, nc.Description AS 'No Show Code'
FROM encountercodes AS ec
JOIN dischargecodes AS dc
ON ec.EncounterCode = dc.DischargeCode
JOIN appointments AS a
ON dc.DischargeCode = a.DischargeCode
JOIN noshowcodes AS nc
ON a.NoShowCode = nc.NoShowCode
There are several problems.
Top select shows EncounterID, does that match with description from the second select? I doubt it.
Then you have Year matched up with reason. Also not good.
Then Month lined up with duration.
etc.
Just match up your types on your SELECT columns.
SELECT e.EncounterID, YEAR(e.EntryDate) AS 'Year', MONTH(e.EntryDate) AS 'Month', ac.Description AS 'PresentingProblem', c.ClinicName
SELECT ec.Description, dc.Reason AS 'Discharge_Reason', a.Duration, a.ExtraTime, nc.Description AS 'No Show Code'

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'

SQL query for calculating percentage completion of a survey

Here I have a survey and survey has many questions.Question has many answers.This is very basic relation of my survey.Every question is having a 'sequence number'.This 'sequence number' is used for defining 'next question sequnce number' in answers.So based on few answers the next question will get decided.For example,
---------------------- first page -----------------------
1ques[page1])Are you married?(seq_num:1)
Answer) a.Yes (next question sequnce number = 4)
b.No (next question sequnce number = 5)
2ques[page1])Are you interested in colors ?(seq_num:2)
Answer) a.Yes (next question sequnce number = 6)
b.No
3ques[page1])Are you interested in colors ?(seq_num:3)
Answer) a. < 25 years
b. >= 25 years
=========================end===========================
If I answer like this,1ques[page1]=> b.No , 2ques[page1]=> b.Yes , 3ques[page1]=> b. >= 25 years ,then
---------------------- second page -----------------------
1ques[page2])Is there any plan for your wedding in next one year?(seq_num:5)
Answer) a.Yes (next question sequnce number = 2)
b.No (next question sequnce number = 3)
2ques[page2])Which type of color do you like?(seq_num:6)
Answer) a.light
b.dark
=========================end===========================
If I answer like this,1ques[page1]=> b.Yes , 2ques[page1]=> b.No , 3ques[page1]=> a. < 25 years ,then
---------------------- second page -----------------------
1ques[page2])How many children do you have?(seq_num:4)
Answer) a.Yes (next question sequnce number = 2)
b.No (next question sequnce number = 3)
2ques[page2])What is your interensts?(seq_num:7)
Answer) a.Arts
b.science
=========================end===========================
the survey question and answer structure will be like this for so many pages.If the survey has quit in the middle or survey submitted without answering.By calculating all the cases survey completion should be calculated.Clearly I mean,For calculating completion percentage I need total questions.So In any of case How many total question does user will get is needed.Or any other way to calculate to completion percentage.For a reference , Here I am providing DB structure of surveys.
Thanks!
What exactly you want ?
I guess you want to calculate percentage for restricting the submission means like if completion percentage should be more than 50%, something like that or what ?
Because the structure you shown here is so simple, but i am not getting your requirement.