Results 1 to 7 of 7

Thread: Need Help In Designing a query for returning students who did not pay fees

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    66

    Need Help In Designing a query for returning students who did not pay fees

    I need to list the names of student who did not pay the fees (there are mutiple fees types). The logic would be find those students who does not have the entry in the feespayment table for that particular fees type.

    --Here are the table structures
    'Student Table : studentid , name
    Fees Table : Feesid,Feesname
    Feespayment Table : studentid (from students table),feesid(from fees table),amount
    --
    So I need To Find those students who did not pay that fees (Remind, there are multiple fees ids), a.k.a. those students who are not in the feespayment table with the fees id. For Example , If student 1 did not pay the fees type 2, the he must be listed along with that fees name. If he has paid,no need to display him(with that fees )

    Help Please

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Need Help In Designing a query for returning students who did not pay fees

    Are you going to specify one fee type at a time or do you want a list of all students who haven't paid a particular fee type for all fee types at the same time?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    66

    Re: Need Help In Designing a query for returning students who did not pay fees

    Quote Originally Posted by jmcilhinney View Post
    Are you going to specify one fee type at a time or do you want a list of all students who haven't paid a particular fee type for all fee types at the same time?
    A list of all students who haven't paid any fees type. That is what I want. If a student has paid all the fees type, he should be not in the result. If other student has paid one, from total of four fee types, he must be listed in 3 rows along with those fees types he hasn't paid. It is called 'Fees Unpaid Report' or 'Fees Dues Report'
    T.You.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Need Help In Designing a query for returning students who did not pay fees

    I believe that something along these lines should work:
    sql Code:
    1. SELECT s.StudentId, s.Name, f.FeesId, f.FeesName
    2. FROM FeesPayment fp RIGHT JOIN Student s
    3. ON fp.StudentId = s.StudentId RIGHT JOIN Fees f
    4. ON fp.FeesId = f.FeesId
    5. WHERE fp.Amount IS NULL

  5. #5
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,802

    Re: Need Help In Designing a query for returning students who did not pay fees

    This returns the student who did not pay any type of fee (or none). If all fees are paid, it won't be returned
    sql Code:
    1. SELECT s.*
    2.     , CASE WHEN fp1.Feesid IS NULL THEN 'No' ELSE 'Yes' END AS [Fee 1 Paid]
    3.     , CASE WHEN fp2.Feesid IS NULL THEN 'No' ELSE 'Yes' END AS [Fee 2 Paid]
    4.     , CASE WHEN fp3.Feesid IS NULL THEN 'No' ELSE 'Yes' END AS [Fee 3 Paid]
    5.     , CASE WHEN fp4.Feesid IS NULL THEN 'No' ELSE 'Yes' END AS [Fee 4 Paid]
    6. FROM Student AS s
    7.     LEFT JOIN Feespayment AS fp1 ON fp1.studentid = s.studentid AND fp1.Feesid = 1
    8.     LEFT JOIN Feespayment AS fp2 ON fp2.studentid = s.studentid AND fp2.Feesid = 2
    9.     LEFT JOIN Feespayment AS fp3 ON fp3.studentid = s.studentid AND fp3.Feesid = 3
    10.     LEFT JOIN Feespayment AS fp4 ON fp4.studentid = s.studentid AND fp4.Feesid = 4
    11. WHERE fp1.studentid IS NULL
    12.     OR fp2.studentid IS NULL
    13.     OR fp3.studentid IS NULL
    14.     OR fp4.studentid IS NULL
    You have to hard-code the IDs of the fees (in the left joins), and the fees names in the "AS", so for "[Fee 1 Paid]", put the actual fee name

    PS. You did not mention for what database you need this, and the query might be different depending on the database type

    If you need the query to be dinamic, let me know, as it is much more complicated, and it depends on the database you are using

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    66

    Re: Need Help In Designing a query for returning students who did not pay fees

    Quote Originally Posted by jmcilhinney View Post
    I believe that something along these lines should work:
    sql Code:
    1. SELECT s.StudentId, s.Name, f.FeesId, f.FeesName
    2. FROM FeesPayment fp RIGHT JOIN Student s
    3. ON fp.StudentId = s.StudentId RIGHT JOIN Fees f
    4. ON fp.FeesId = f.FeesId
    5. WHERE fp.Amount IS NULL
    This is the result for your query
    Name:  01.JPG
Views: 322
Size:  55.9 KB

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    66

    Re: Need Help In Designing a query for returning students who did not pay fees

    Quote Originally Posted by CVMichael View Post
    This returns the student who did not pay any type of fee (or none). If all fees are paid, it won't be returned
    [highlight=sql]
    SELECT s.*
    , ...........
    PS. You did not mention for what database you need this, and the query might be different depending on the database type

    If you need the query to be dinamic, let me know, as it is much more complicated, and it depends on the database you are using
    Yes, It is sql server 2000 and I need feesids and names to be dynamic. I have also added an amount column. If we can get the total, We can use it for each students entry in feespayments table total. I have attached a image in the other reply.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width