Get the non existing data from database

  • Hi ,

    I need to write a query for teachers who dont have classes on a particular day.

    like ex: name, day, courseid

    Mary, MON, Science

    Surya, TUE ,Maths

    Mary, TUE ,Biology

    The teacher teach a particular course on the particular day. Ex: Mary is not teaching on Wed, Thur, Fri.

    I need to get the data to find when the teacher is free(Wed, Thur, Fri) so the students can meet him/her for a free session/doubts.

    Please help.

  • sindhupavani123 (10/18/2016)


    Hi ,

    I need to write a query for teachers who dont have classes on a particular day.

    like ex: name, day, courseid

    Mary, MON, Science

    Surya, TUE ,Maths

    Mary, TUE ,Biology

    The teacher teach a particular course on the particular day. Ex: Mary is not teaching on Wed, Thur, Fri.

    I need to get the data to find when the teacher is free(Wed, Thur, Fri) so the students can meet him/her for a free session/doubts.

    Please help.

    Can you provide some sample DDL, INSERT statements and desired results, as described in the link in my signature, please. Do this, and you should get a working solution in return.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Pretty sparse on details here. I notice you are very new around here so I decided to help you out with some ddl and sample data so you can see how you should do this in the future.

    I think you are looking for something like this.

    create table #something

    (

    name varchar(10)

    , day char(3)

    , courseid varchar(10)

    )

    insert #something

    select 'Mary', 'MON', 'Science' union all

    select 'Surya', 'TUE', 'Maths' union all

    select 'Mary', 'TUE', 'Biology'

    create table #Days

    (

    NameOfDay char(3)

    )

    insert #Days (NameOfDay)

    values ('MON'),('TUE'),('WED'),('THU'),('FRI')

    select *

    from #Days d

    left join #something s on s.day = d.NameOfDay

    and s.name = 'Mary'

    where s.day is null

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Thank you.

    Its quite close to what i expect

    In your query 'left join #something s on s.day = d.NameOfDay

    and s.name = 'Mary'' you are trying to retrieve data for only one teacher .

    But my requirement is to get all the teachers with their nameas well as respective free days:

    like,

    name, day, courseid

    mary , wed ,NULL

    THU ,NULL

    fri , NULL

    Surya Mon Null

    WEd Null

    Thu Null

  • sindhupavani123 (10/18/2016)


    Thank you.

    Its quite close to what i expect

    In your query 'left join #something s on s.day = d.NameOfDay

    and s.name = 'Mary'' you are trying to retrieve data for only one teacher .

    But my requirement is to get all the teachers with their nameas well as respective free days:

    like,

    name, day, courseid

    mary , wed ,NULL

    THU ,NULL

    fri , NULL

    Surya Mon Null

    WEd Null

    Thu Null

    If what I attempted is not what you are after then you need to provide more details about what you expect. I gave you a pretty good example of how to post sample data. Can you hard code a select statement that would be representative of what you want for output from your sample data?

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Still kind of guessing here. I assume you have a table with the teachers in it that you could use for this. Since it is unclear I created a cte to simulate this table.

    create table #something

    (

    name varchar(10)

    , day char(3)

    , courseid varchar(10)

    )

    insert #something

    select 'Mary', 'MON', 'Science' union all

    select 'Surya', 'TUE', 'Maths' union all

    select 'Mary', 'TUE', 'Biology'

    create table #Days

    (

    NameOfDay char(3)

    )

    insert #Days (NameOfDay)

    values ('MON'),('TUE'),('WED'),('THU'),('FRI');

    with Teachers as --hopefully you have a table with names, I am deriving one here

    (

    select Name

    from #something

    group by name

    )

    select *

    from #Days d

    cross join Teachers t

    left join #something s on s.name = t.name and s.day = d.NameOfDay

    where s.name is null

    drop table #something

    drop table #Days

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • this is what i need..thanks so much..

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply