sql server - Select data from three table in sql -


i have 3 table like

student : sid, sname, semail

fees_type : fid, fname, fprice

studentfees : sid(fk student),fid(fk 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 want data

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 can not group or perfect result

select s.sid, semail, sname, fname ,fprice   student s    inner join studentfees sf on s.sid = sf.eid    inner join fees_type f on f.fid = sf.fid  month(pr.tddate) = month(dateadd(dd, 1, getdate())) , year(pr.tddate) = year(dateadd(dd, -1, getdate()))  

i new in sql. please me.

thank you.

you this:

select     student.sid,     student.sname,     student.semail,     sum(case when fees_type.fname='chess' 1 else 0 end) total_chess_played,     sum(case when fees_type.fname='cricket' 1 else 0 end) total_cricket_played,     sum(fees_type.fprice) total_fees     student      join studentfees on student.sid = studentfees.eid     join fees_type on fees_type.fid = studentfees.fid      month(studentfees.tddate) = month(dateadd(dd, 1, getdate())) ,     year(studentfees.tddate) = year(dateadd(dd, -1, getdate()))  group     student.sid,     student.sname,     student.semail 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -