Select data from three table in sql - sql

I have three table Like
Student : Sid, SName, SEmail
Fees_Type : Fid, FName, FPrice
StudentFees : Sid(FK from Student),Fid(FK from Fees_Type), FDate
Data of Each Table :
Student :
SID |SName | SEmail
1 | ABC | ABC#www.com
2 | XYZ | xyz#www.com
Fees_Type:
Fid | FName | FPrice
1 | Chess | 100
2 | Cricket | 200
StudentFees:
Sid | Fid| FDate
1 | 1 | 5/2
1 | 2 | 6/2
2 | 1 | 7/2
2 | 2 | 8/2
1 | 1 | 6/2
Now I want to Get data Like
SID|SName|SEmail | Total_Chess_Played|Total_Cricket_Played | ToTal_Fees
1 | ABC |ABC#www.com | 2 | 1 | 400
2 | XYZ |xyz#www.com | 1 | 1 | 300
I have tried these following query but can not get Group by or perfect result
SELECT s.sId, SEmail, SName, FName ,FPrice
FROM Student s
INNER JOIN StudentFees sf ON s.sId = sf.EId
INNER JOIN Fees_Type f ON f.fId = sf.fId
WHERE MONTH(pr.TDDate) = MONTH(dateadd(dd, 1, GetDate())) AND
YEAR(pr.TDDate) = YEAR(dateadd(dd, -1, GetDate()))
I am new in SQL. So Please Help Me.
Thank You.

You could do something like this:
SELECT
Student.SID,
Student.SName,
Student.SEmail,
SUM(CASE WHEN Fees_Type.FName='Chess' THEN 1 ELSE 0 END) AS Total_Chess_Played,
SUM(CASE WHEN Fees_Type.FName='Cricket' THEN 1 ELSE 0 END) AS Total_Cricket_Played,
SUM(Fees_Type.FPrice) AS ToTal_Fees
FROM
Student
JOIN StudentFees ON Student.sId = StudentFees.EId
JOIN Fees_Type ON Fees_Type.fId = StudentFees.fId
WHERE
MONTH(StudentFees.TDDate) = MONTH(dateadd(dd, 1, GetDate())) AND
YEAR(StudentFees.TDDate) = YEAR(dateadd(dd, -1, GetDate()))
GROUP BY
Student.SID,
Student.SName,
Student.SEmail

try this
SELECT
s.SID,
s.SName,
s.SEmail,
SUM(CASE WHEN ft.FName='Chess' THEN 1 ELSE 0 END) AS Total_Chess_Played,
SUM(CASE WHEN ft.FName='Cricket' THEN 1 ELSE 0 END) AS Total_Cricket_Played,
SUM(ft.FPrice) AS ToTal_Fees
FROM
Student s
JOIN StudentFees sf ON s.sId = sf.Sid
JOIN Fees_Type ft ON ft.fId = sf.fId
GROUP BY
s.SID,
s.SName,
s.SEmail

Related

Optimized Query that selects 3 salaries from the past 2 years

I have a query that will show the past 3 salaries within a 2 year period (if there are three) I have the query up and running the problem is, is its extremely slow... I'm wondering if there was a better way to write this query. I'm some-what new to oracle.
Here are my Tables
TABLE 1: Salary
ASSIGN_ID | start_date | end_date | salary |
1 | 11/27/2017 | 1/05/2018 | 50000.0 |
2 | 1/06/2018 | 6/08/2018 | 76000.0 |
3 | 6/09/2018 | 12/31/4712 | 80500.0 |
TABLE 2: Assignments
ASSIGN_ID | per_ID | start_date | end_date |
1 | 1 | 11/2/2017 | 1/05/2018 |
2 | 1 | 1/06/2018 | 6/08/2018 |
3 | 1 | 6/09/2018 | 12/31/4712 |
4 | 2 | 5/12/2016 | 7/18/2017 |
5 | 2 | 7/19/2017 | 12/31/4712 |
Table 3: Person
per_id | first_name | last_name |
1 | John | Smith |
2 | Jane | Doe |
Our end dates default to 12/31/4712 if they're are currently active in the assignment
My Query looks like this:
SELECT
per.first_name,
per.last_name,
(CASE WHEN sal1.start_date >= add_months(CURRENT_DATE, -24)
THEN sal1.salary
ELSE NULL END) oldest_salary,
(CASE WHEN sal2.start_date >= add_months(CURRENT_DATE, -24)
THEN sal2.salary
ELSE NULL END) prior_salary,
sal3.salary current_salary,
FROM
person per
INNER JOIN assignments asg1 ON asg1.per_id = per.per_id
INNER JOIN assignments asg2 ON asg2.per_id = asg1.per_id
INNER JOIN assignments asg3 ON asg3.per_id = asg2.per_id
INNER JOIN salary sal1 ON sal1.assign_id = asg1.assign_id
INNER JOIN salary sal2 ON sal2.assign_id = asg2.assign_id
INNER JOIN salary sal3 ON sal3.assign_id = asg3.assign_id
WHERE asg3.start_date =
(SELECT MAX(asg.start_date
FROM assignments asg
WHERE asg.assign_id = asg3.assign_id)
AND (asg3.start_date - 1) BETWEEN asg2.start_date and asg2.end_date
AND (asg2.start_date - 1) BETWEEN asg1.start_date and asg1.end_date
AND sal1.salary != sal2.salary
AND sal2.salary != sal3.salary
ORDER BY 2,1
Is there a simpler way to do this? because when I run my script it processes forever. I think I might need better joins. like I said i'm new and my understanding of joins is weak.
A simpler form:
SELECT
z.first_name,
z.last_name,
--typical cross-db compatible pivot method
MAX(CASE WHEN z.rown = 1 THEN z.salary END) as recentsalary,
MAX(CASE WHEN z.rown = 2 THEN z.salary END) as oldersalary,
MAX(CASE WHEN z.rown = 3 THEN z.salary END) as oldestsalary
FROM
(
SELECT
per.first_name,
per.last_name,
--number assignments from 1=recent to N older
row_number() over(partition by a.per_id order by a.start_date desc) rown
s.salary
FROM --join up all
person p
INNER JOIN assignments a ON a.per_id = p.per_id
INNER JOIN salary s ON s.assign_id = s.assign_id
WHERE a.end_date > ADD_MONTHS(SYSDATE, -36) --only recent 3 years
) z
WHERE z.rown <= 3 --only the most recent 3 assignments
GROUP BY first_name, last_name --achieve pivot
It works by:
Join up all data so people, assignments and salaries are known
Only consider assignments ended since 3 years ago
Number the assignments in youngest to oldest order (1=youngest)
Pivot the top 3 numberings into 3 columns for recent, older and oldest salary, per person

SQL - Counting one attribute, grouping by another

I have a table staff
staff
pt | ward
P | 1
P | 1
T | 1
P | 2
T | 2
I want to produce a table that counts how many P's and T's there is for each ward like this:
staff
ward | P | T
1 | 2 | 1
2 | 1 | 1
I have tried this
WITH cte(ward, P, T) AS(
SELECT ward,
(SELECT COUNT(PT) FROM staff WHERE PT = 'P' ),
(SELECT COUNT(PT) FROM staff WHERE PT = 'T' ) FROM staff GROUP BY ward)
SELECT * FROM cte
but then I get this table
staff
ward | P | T
1 | 3 | 2
2 | 2 | 2
Any help would be appreciated
Case statements will work here:
SELECT
ward,
SUM(CASE WHEN pt = P THEN 1 ELSE 0 END) AS P,
SUM(CASE WHEN pt = T THEN 1 ELSE 0 END) AS T
FROM
table
GROUP BY
ward
Use conditional aggregation:
select ward, sum( (pt = 'P')::int ) as p, sum ( (pt = 'T')::int ) as t
from t
group by ward;

row counter with condition in two different columns

I have the following tables with sport results (e.g. football):
tblGoals (RowId, GameRowIdm PlayerRowId, TeamRowId, GoalMinute)
RowId | GameRowId | PlayerRowId | TeamRowId | GoalMinute
--------------------------------------------------------
1 | 1 | 1 | 1 | 25
2 | 1 | 2 | 2 | 45
3 | 1 | 3 | 1 | 66
tblPlayers (RowId, PlayerName)
RowId | PlayerName
------------------
1 | John Snow
2 | Frank Underwood
3 | Jack Bauer
tblGames (RowId, TeamHomeRowId, TeamGuestRowId)
RowId | TeamHomeRowId | TeamGuestRowId | GameDate
---------------------------------------------------
1 | 1 | 2 | 2015-01-01
Now I want get a list of all goals. The list should look like this:
GoalMinute | PlayerName | GoalsHome | GoalsGuest
-----------------------------------------------------
25 | John Snow | 1 | 0
45 | Frank Underwood | 1 | 1
66 | Jack Bauer | 2 | 1
GoalsHome and GoalsGuest should be a counter of the shot goals for the team. So e.g. if you check the last row, the result is 2:1 for home team.
To get this list of goals, I used this statement:
SELECT t_gol.GoalMinute,
t_ply.PlayerName,
CASE WHEN
t_gol.TeamRowId = t_gam.TeamHomeRowId
THEN ROW_NUMBER() OVER (PARTITION BY t_gam.TeamHomeRowId ORDER BY t_gam.TeamHomeRowId)
END AS GoalsHome,
CASE WHEN
t_gol.TeamRowId = t_gam.TeamGuestRowId
THEN ROW_NUMBER() OVER (PARTITION BY t_gam.TeamGuestRowId ORDER BY t_gam.TeamGuestRowId)
END AS GoalsGuest
FROM dbo.tblGoalsFussball AS t_gol
LEFT JOIN dbo.tblPlayersFussball AS t_ply ON (t_ply.RowId = t_gol.PlayerRowId)
LEFT JOIN dbo.tblGames AS t_gam ON (t_gam.RowId = t_gol.GameRowId)
WHERE t_gol.GameRowId = #match_row
But what I get is this here:
GoalMinute | PlayerName | GoalsHome | GoalsGuest
-----------------------------------------------------
25 | John Snow | 1 | NULL
45 | Frank Underwood | NULL | 2
66 | Jack Bauer | 3 | NULL
Maybe ROW_NUMBER() is the wrong approach?
I would do the running total using sum() as a windowed aggregate function with the over ... clause, which works in SQL Server 2012+.
select
g.RowId, g.GameDate, t.GoalMinute, p.PlayerName,
GoalsHome = COALESCE(SUM(case when TeamRowId = g.TeamHomeRowId then 1 end) OVER (PARTITION BY gamerowid ORDER BY goalminute),0),
GoalsGuest = COALESCE(SUM(case when TeamRowId = g.TeamGuestRowId then 1 end) OVER (PARTITION BY gamerowid ORDER BY goalminute),0)
from tblGoals t
join tblPlayers p on t.PlayerRowId = p.RowId
join tblGames g on t.GameRowId = g.RowId
order by t.GameRowId, t.GoalMinute
Another approach (that also works in older versions) is to use a self-join and sum up the rows with lower goalminutes. For ease of reading I've used a common table expression to split the goals into two columns for home and guest team:
;with t as (
select
g.GoalMinute, g.PlayerRowId, g.GameRowId,
case when TeamRowId = ga.TeamHomeRowId then 1 end HomeGoals,
case when TeamRowId = ga.TeamGuestRowId then 1 end GuestGoals
from tblGoals g
join tblGames ga on g.GameRowId = ga.RowId
)
select
g.RowId, g.GameDate, t.GoalMinute, p.PlayerName,
GoalsHome = (select sum(coalesce(HomeGoals,0)) from t t2 where t2.GoalMinute <= t.GoalMinute and t2.GameRowId = t.GameRowId),
GoalsGuest = (select sum(coalesce(GuestGoals,0)) from t t2 where t2.GoalMinute <= t.GoalMinute and t2.GameRowId = t.GameRowId)
from t
join tblPlayers p on t.PlayerRowId = p.RowId
join tblGames g on t.GameRowId = g.RowId
order by t.GameRowId, t.GoalMinute
The CTE isn't necessary though, you could just as well use a derived table
Sample SQL Fiddle
I think the easiest way is with subqueries..
SELECT
tgs.GoalMinute,
tpl.PlayerName,
( SELECT
COUNT(t.RowId)
FROM
tblgoals AS t
WHERE t.GoalMinute <= tgs.GoalMinute
AND t.GameRowId = tgm.RowId
AND t.TeamRowId = tgm.TeamHomeRowId
) AS HomeGoals,
( SELECT
COUNT(t.RowId)
FROM
tblgoals AS t
WHERE t.GoalMinute <= tgs.GoalMinute
AND t.GameRowId = tgm.RowId
AND t.TeamRowId = tgm.TeamGuestRowId
) AS GuestGoals
FROM
tblgoals AS tgs
JOIN tblplayers AS tpl ON tgs.RowId = tpl.RowId
JOIN tblGames AS tgm ON tgm.RowId = tgs.GameRowId
ORDER BY tgs.GoalMinute

SQL Finding multiple values difficulty

I'm trying to generate the correct SQL for a project.
Here is a sample dataset:
DateTime | EmpID | Function | Location
--------------------------------------------------
1/23/2015 2:00PM | 123 | 1 | 1
1/23/2015 2:10PM | 123 | 2 | 1
1/23/2015 2:20PM | 123 | 1 | 2
1/23/2015 2:40PM | 123 | 2 | 2
1/24/2015 2:00PM | 321 | 1 | 2
1/24/2015 2:15PM | 321 | 2 | 2
1/24/2015 2:30PM | 321 | 1 | 3
I need to pull a count of all records where functionid = 1 and location MUST EQUAL both 1 and 2. So the first row and the third row would be returned and considered a count of 1.
Hopefully I'm making sense with this. Basically I need to know how many times an employee was at two locations. Any help would be appreciated.
Group by EmpId and count locations.
SELECT *
FROM MyTable T1
WHERE Function = 1 AND
NOT EXISTS (SELECT 1
FROM MyTable T2
WHERE T1.EmpId = T2.EmpId AND
T1.Function = T2.Function AND
T2.Location NOT IN (1, 2))
GROUP BY EmpId
HAVING Count(DISTINCT Location) > 1
Have not tested it but think this will work
SELECT EmpID, COUNT(EmpID) AS NumOfTimes
From [Table Name]
WHERE FunctionID = 1 AND (Location = 1 OR Location = 2)
GROUP BY EmpID
HAVING NumOfTimes = 2
SELECT EmpID , COUNT(*) total, COUNT (CASE WHEN Location = 1 THEN 1 END) was_in_1,COUNT (CASE WHEN Location = 2 THEN 1 END) was_in_2
FROM table
WHERE Function = 1
GROUP BY EmpID
HAVING MAX(CASE WHEN Location = 1 THEN 1 ELSE 0 END) = MAX(CASE WHEN Location = 2 THEN 1 ELSE 0 END)
but if you would like to know if the employee was in any 2 locations then jarlh gave the right comment
group by EmpID
having count(distinct location) >= 2
SELECT A.EmpID, COUNT(*)
FROM YOURTABLENAME A
INNER JOIN YOURTABLENAME B
ON A.EmpID= B.EmpID
WHERE A.Location = 1 and B.Location = 2
and A.Function = B.Function and A.Function = 1
GROUP BY A.EmpID

Help with a SQL query joining multiple tables

First I will explain the case, I have a table tbl_game with a structure as such. This table contains, the time where the game was started and pair playing the game
| id | time | pair_id |
-----------+--------------+ ---------------
1 | 123123123 | 1 |
2 | 123168877 | 1 |
and I have another table tbl_throws which holds the score for each player. In case you are wondering, this a basic dice rolling game
| id | game_id | player_id | score |
-----------+--------------+---------------+---------|
| 1 | 1 | 1 | 2 |
| 2 | 1 | 2 | 5 |
| 3 | 1 | 1 | 9 |
| 4 | 1 | 2 | 11 |
| 5 | 2 | 1 | 7 |
| 6 | 2 | 2 | 6 |
Now, id here is the throw id, not the game id. Here each player with player_id 1 and 2 has throws the dice twice and got the respective score as presented all in same game and just one time in another
Now, using these two table, I need to create a record set, that the total score of each player in one game
| game_id | game_time | player1_total | player2_total|
|------------+-----------+---------------+--------------|
| 1 | 123123123 | 11 | 16 |
| 2 | 123168877 | 7 | 6 |
I tried lots of mumbo jumbo queries, but nothing is giving corrent result?
What is the correct query for this?
Update
Since, most of the answers were bounded by a fact that, player1id and player2id had to be known or fixed.
So may be the information I am about to provide will help to clear the confusion.
there is another table, which holds the information of the player. tbl_pupil
Structure is like the following
| id | unique_id | name |
|---------+---------------+----------|
| 1 | 001 | some |
| 2 | 002 | another |
and these player are collectively called, a pair in another table tbl_pair
| id | player1 | player2 |
|---------+---------------+----------|
| 1 | 1 | 2 |
So, now
select
g.id
g.time
p1.id as player1id
p1.name as player1name
t.score as player1score
p2.id as player2id
p2.name as player2name
t.score as player2score
FROM
tbl_game g,
inner join tbl_pair as pair on g.pair_id = pair.id
inner join tbl_pupil as p1 on p1.id = pair.player1
inner join tbl_pupil as p2 on p2.id = pair.player2
inner join tbl_throw as t on g.id = t.game_id
This is my preliminary query, which brings the record set, on a way as such
| id | time | player1id | player1name | player1score | player2id | player2name | player2score |
----------------------------------------------------------------------------------------------------------------------------
| 1 | 12 | 1 | some | 5 | 2 | another | 2 |
| 1 | 12 | 1 | some | 5 | 2 | another | 5 |
| 1 | 12 | 1 | some | 9 | 2 | another | 9 |
| 1 | 12 | 1 | some | 11 | 2 | another | 11 |
Now I am just showing the results of one game id by the way. I don't save sufficient knowledge, to group the above record into one, with player1 separate sum score in one column and playe2's separate sum of score in another column.
Try this:
SELECT
tbl_game.id AS game_id,
tbl_game.time AS game_time,
SUM(CASE WHEN player_id = 1 THEN score ELSE 0 END) AS player1_total,
SUM(CASE WHEN player_id = 2 THEN score ELSE 0 END) AS player2_total
FROM tbl_game JOIN tbl_thorws ON tbl_game.id = tbl_throws.game_id
GROUP BY tbl_game.id
This is similar to some of the other answers, but crucially doesn't depend on the player IDs being 1 and 2.
select
game_id = g.id,
game_time = g.time,
player1_total = SUM(case t.player_id when p.player1_id then t.score else 0 end),
player2_total = SUM(case t.player_id when p.player2_id then t.score else 0 end)
from
tbl_game g
join tbl_throws t on g.id = t.game_id
join ( --Get the player IDs for this game
select
game_id,
player1_id = MIN(player_id),
player2_id = MAX(player_id)
from
tbl_throws
group by game_id
) p
on p.game_id = t.game_id
group by
g.id, g.time
Just for fun I've generalized the above out to allow more > 2 players:
The 2 CTE tables just show the test data I'm using
;WITH tbl_game as (
select ID = 1, time = 123123123, pair_id = 1
union select ID = 2, time = 123168877, pair_id = 1
),
tbl_throws as (
select id = 1, game_id = 1, player_id = 1, score = 2
union select id = 2, game_id = 1, player_id = 2, score = 5
union select id = 2, game_id = 1, player_id = 3, score = 5
union select id = 3, game_id = 1, player_id = 1, score = 9
union select id = 4, game_id = 1, player_id = 2, score = 11
union select id = 5, game_id = 2, player_id = 1, score = 7
union select id = 6, game_id = 2, player_id = 2, score = 6
)
select
game_id = g.id,
game_time = g.time,
player1_id = MAX(case x.player_no when 1 then t.player_id else 0 end),
player1_total = SUM(case x.player_no when 1 then t.score else 0 end),
player1_id = MAX(case x.player_no when 2 then t.player_id else 0 end),
player2_total = SUM(case x.player_no when 2 then t.score else 0 end),
player3_id = MAX(case x.player_no when 3 then t.player_id else 0 end),
player3_total = SUM(case x.player_no when 3 then t.score else 0 end),
player4_id = MAX(case x.player_no when 4 then t.player_id else 0 end),
player4_total = SUM(case x.player_no when 4 then t.score else 0 end)
/* Add more rows for the number of players permitted in a single game */
from
tbl_game g
join tbl_throws t on g.id = t.game_id
cross apply (
select player_no = COUNT(distinct player_id)
from tbl_throws sub
where sub.player_id <= t.player_id
and Sub.game_id = t.game_id
) x
group by
g.id, g.time
You need to inner join the two tables, and aggregate your scores. To do the basic pivot you are after I used a CASE statement to aggregate by player.
SELECT G.Id as Game_Id,
G.time as Game_Time,
SUM(CASE WHEN t.Player_id = 1 THEN t.score ELSE 0 END) as Player1_total,
SUM(CASE WHEN t.Player_id = 2 THEN t.score ELSE 0 END) as Player2_total
FROM tbl_game G
INNER JOIN tbl_throws T
ON g.id = t.game_id
GROUP BY g.ID, g.time
I think this should do pretty much what you want to do.
SELECT
tbl_game.id as game_id,
tbl_game.time as game_time,
SUM(player1.score) as player1_total,
SUM(player2.score) as player2_total
FROM tbl_game
INNER JOIN tbl_throws player1 ON player1.game_id = tbl_game.id AND player1.player_id = 1
INNER JOIN tbl_throws player2 ON player2.game_id = tbl_game.id AND player2.player_id = 2
GROUP BY tbl_game.id, tbl_game.time
SELECT t.game_id
, t.game_time
, ( SELECT SUM(t.score)
FROM tbl_throws AS t
WHERE t.game_id = g.id
AND player_id = 1
) AS player1_total
, ( SELECT SUM(t.score)
FROM tbl_throws AS t
WHERE t.game_id = g.id
AND player_id = 2
) AS player2_total
FROM tbl_game AS g