Results 1 to 4 of 4

Thread: Query Problem

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Question Query Problem

    Hi all
    Please look the next problem
    SELECT Student_Name,Attedance,HalfID FROM Table1
    above query providing me following result


    Code:
    Name1,P,1
    Name1,P,2
    Name1,A,1
    Name1,P,2
    Here half Id 1 mean first half and 2 mean second half
    But I want result in followig format


    Code:
    StudentName  FirstHalfAttedance  SecondHalfAttedance
    Name1            P                             P
    Name1            A                             P
    Last edited by shakti5385; Feb 9th, 2008 at 12:34 AM.

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Query Problem

    As long as a student can only have one 1 row and only one 2 row - you can fake this with a MAX() function

    Code:
    Select Student_Name
        ,Max(Case When HalfId=1 Then Attendance Else '' End)
        ,Max(Case When HalfId=2 Then Attendance Else '' End)
         From Table1
         Group by Student_Name
    You can also create VIEWS of 1 and another VIEW of 2 attendance. And then JOIN the whole mess back together. But basically it's the same thing.

    Personally I consider this taking normalization too far - if I understand what you are trying to acheive with this FirstHalf and SecondHalf...

    FirstHalfAttendance is not the same "entity" as SecondHalfAttendance.

    I would have designed the table to be:

    StudentName
    FirstHalfAttendance
    SecondHalfAttendance

    To avoid this very issue.

    We have tables that store the marks a student gets in a class. First Marking Period Mark is stored right in the same row as 2nd MP Mark and 3rd MP Mark and 4th MP Mark and the Final Mark as well. I've seen other systems where each mark is a separate row - making the "re-association" of all a classes marks extremely difficult.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    Addicted Member
    Join Date
    Feb 2008
    Location
    Hamburg
    Posts
    138

    Re: Query Problem

    The data does not seem consistent to me. Student name1 seems to get an A and a P for same semester shouldnt it be:


    Name1,P,1
    Name1,P,2
    Name2,A,1
    Name2,P,2


    You can denormalize this, to get the desired result, using the TRANSFORM statement in Access (other DBs need a more complicated approach) like so:

    Code:
    TRANSFORM first(attendance) as val
    SELECT student_Name
    FROM Tabelle1
    GROUP BY student_name
    Pivot IIF(HalfID=1,"FirstHalf_Attendance","SecondHalf_Attendance") IN ("FirstHalf_Attendance","SecondHalf_Attendance")

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Query Problem

    I assumed it was Name1 and Name2 - never noticed that was all the same name...

    We need more info from the OP for this one.

    Also - what is the backend DB being used.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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