Author |
Topic |
cognos79
Posting Yak Master
241 Posts |
Posted - 2008-03-25 : 10:43:51
|
I have two tables:Students:SSN Objective123 Complete 121234 Complete 123AttendanceSSN ATTENDDATE123 03/21/2007123 03/22/2007234 03/21/2007234 03/22/20007I have to join by SSN and in my final query I want to select SSN, objective, Attendate and should return only 4 rows and not 8 rows.Below is the output I am looking forOutput:SSN Objective AttendDate123 Complete121 03/21/2007123 Null 03/22/2007234 Complete123 03/21/2007234 Null 03/22/2007.I want in this way for reporting purposes.Thanks |
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-03-25 : 10:47:03
|
What have you tried?"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
cognos79
Posting Yak Master
241 Posts |
Posted - 2008-03-25 : 10:48:32
|
select S.ssna.attendates.objectivefrom students sjoin attendance aon s.ssn = a.ssn |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-25 : 10:53:47
|
The only way for you to get 8 rows is by using old style join (non-ansi) and forget about join-bindings.Try thisDECLARE @Students TABLE (SSN VARCHAR(3), Objective VARCHAR(20))INSERT @StudentsSELECT '123', 'Complete 121' UNION ALLSELECT '234', 'Complete 123'DECLARE @Attendance TABLE (SSN VARCHAR(3), ATTENDDATE DATETIME)SET DATEFORMAT MDYINSERT @AttendanceSELECT '123', '03/21/2007' UNION ALLSELECT '123', '03/22/2007' UNION ALLSELECT '234', '03/21/2007' UNION ALLSELECT '234', '03/22/2007'SELECT s.SSN, CASE WHEN y.SSN IS NULL THEN NULL ELSE s.Objective END AS Objective, a.AttendDateFROM @Students AS sINNER JOIN @Attendance AS a ON a.SSN = s.SSNLEFT JOIN ( SELECT SSN, MIN(AttendDate) AS minDate FROM @Attendance GROUP BY SSN ) AS y ON y.SSN = a.SSN AND y.minDate = a.AttendDateORDER BY s.SSN, a.AttendDate E 12°55'05.25"N 56°04'39.16" |
 |
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-03-25 : 10:55:20
|
[code]declare @Students table (SSN int, Objective varchar(100))insert into @studentsselect 123, 'Complete 121'union all select 234, 'Complete 123'declare @Attendance table (SSN int, ATTENDDATE datetime)insert into @Attendanceselect 123, '20070321'union all select 123, '20070322'union all select 234, '20070321'union all select 234, '20070322'select * from @students s join @attendance a on s.ssn = a.ssnSSN Objective SSN ATTENDDATE----------- ---------------------------------------------------------------------------------------------------- ----------- -----------------------123 Complete 121 123 2007-03-21 00:00:00.000123 Complete 121 123 2007-03-22 00:00:00.000234 Complete 123 234 2007-03-21 00:00:00.000234 Complete 123 234 2007-03-22 00:00:00.000[/code]don't understand. it would 4 rows, not 8. and your expected output...SSN Objective AttendDate123 Complete121 03/21/2007123 Null 03/22/2007 why would this be null?234 Complete123 03/21/2007234 Null 03/22/2007 why would this be null?Em |
 |
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-03-25 : 10:56:44
|
Did you even run yours?I get 4 rows.DECLARE @Students TABLE (SSN VARCHAR(3), Objective VARCHAR(20))INSERT @StudentsSELECT '123', 'Complete 121' UNION ALLSELECT '234', 'Complete 123'DECLARE @Attendance TABLE (SSN VARCHAR(3), ATTENDDATE DATETIME)SET DATEFORMAT MDYINSERT @AttendanceSELECT '123', '03/21/2007' UNION ALLSELECT '123', '03/22/2007' UNION ALLSELECT '234', '03/21/2007' UNION ALLSELECT '234', '03/22/2007'select S.ssn,a.ATTENDDATE,s.objectivefrom @students sjoin @attendance aon s.ssn = a.ssn"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
cognos79
Posting Yak Master
241 Posts |
Posted - 2008-03-25 : 11:03:21
|
sorry I gave incorrect number of records...below is the rite now.declare @students table( ssn varchar(9) ,objective varchar(20))declare @attendance table( ssn varchar(9) ,attend datetime)insert into @studentsselect '123','complete 121' union allselect '123','complete 123' union allselect '234','complete 123' union allselect '234','complete 121' insert into @attendanceselect '123', '03/21/2007' union allselect '123', '03/22/2007' union allselect '123', '03/23/2007' union allselect '234', '03/21/2007' union allselect '234', '03/22/2007' union allselect '234', '03/23/2007' select s.ssn ,a.attend ,s.objectivefrom @students sjoin @attendance aon s.ssn = a.ssnThis returns 12 rows. I need only 6 rows based on the attend date and add the objective to that result set.I think the solution that Peso gave will work. Thanks Peso |
 |
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-03-25 : 11:06:24
|
Which Objective?? The objective is at the student level and you have multiple objectives for the same student."God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
cognos79
Posting Yak Master
241 Posts |
Posted - 2008-03-25 : 11:08:28
|
execute the query that peso has replied...that is how I want |
 |
|
cognos79
Posting Yak Master
241 Posts |
Posted - 2008-03-25 : 11:26:15
|
Peso...If I change the data in the table...I think your query is not working.DECLARE @Students TABLE (SSN VARCHAR(3), Objective VARCHAR(20))INSERT @Studentsselect '123','complete 121' union allselect '123','complete 123' union allselect '234','complete 123' union allselect '234','complete 121' DECLARE @Attendance TABLE (SSN VARCHAR(3), ATTENDDATE DATETIME)SET DATEFORMAT MDYINSERT @Attendanceselect '123', '03/21/2007' union allselect '123', '03/22/2007' union allselect '123', '03/23/2007' union allselect '234', '03/21/2007' union allselect '234', '03/22/2007' union allselect '234', '03/23/2007' SELECT s.SSN, CASE WHEN y.SSN IS NULL THEN NULL ELSE s.Objective END AS Objective, --,y.SSN a.ATTENDDATEFROM @Students AS sINNER JOIN @Attendance AS a ON a.SSN = s.SSNLEFT JOIN ( SELECT SSN, MIN(at.ATTENDDATE) AS minDate FROM @Attendance at GROUP BY SSN ) AS y ON y.SSN = a.SSN AND y.minDate = a.ATTENDDATEORDER BY s.SSN, a.ATTENDDATEThe query should only return 6 rows as there are only 6 records in attendance table |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-25 : 11:37:24
|
HOW?What is the business rule to decide which @Student record to fetch?The attendance table only tells us which date any given student were present.Information about which objective for any given date is not stated. E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-25 : 11:39:45
|
With the information you have given us this far, this is your best optionDECLARE @Students TABLE (SSN VARCHAR(3), Objective VARCHAR(20))INSERT @StudentsSELECT '123', 'Complete 121' UNION ALLSELECT '123', 'Complete 123' UNION ALLSELECT '234', 'Complete 123' UNION ALLSELECT '234', 'Complete 121'DECLARE @Attendance TABLE (SSN VARCHAR(3), ATTENDDATE DATETIME)SET DATEFORMAT MDYINSERT @AttendanceSELECT '123', '03/21/2007' UNION ALLSELECT '123', '03/22/2007' UNION ALLSELECT '123', '03/23/2007' UNION ALLSELECT '234', '03/21/2007' UNION ALLSELECT '234', '03/22/2007' UNION ALLSELECT '234', '03/23/2007' SELECT DISTINCT s.SSN, CASE WHEN y.SSN IS NULL THEN NULL ELSE s.Objective END AS Objective, a.ATTENDDATEFROM @Students AS sINNER JOIN @Attendance AS a ON a.SSN = s.SSNLEFT JOIN ( SELECT SSN, MIN(ATTENDDATE) AS minDate FROM @Attendance GROUP BY SSN ) AS y ON y.SSN = a.SSN AND y.minDate = a.ATTENDDATEORDER BY s.SSN, a.ATTENDDATE E 12°55'05.25"N 56°04'39.16" |
 |
|
cognos79
Posting Yak Master
241 Posts |
Posted - 2008-03-25 : 12:07:02
|
The objectives are not tied to any date. I am trying to get result set such that it has ssn and attendance dates and kinda of like add new column with objectives to that result set. If there are 5 dates attended and only 2 objectives then result set should have 5 records and for 3 records the objective is null.So is it not possible to write query without getting duplicates and without tying up the objective to a date??? |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-25 : 15:42:45
|
You do know how a JOIN works, right? E 12°55'05.25"N 56°04'39.16" |
 |
|
cognos79
Posting Yak Master
241 Posts |
Posted - 2008-03-25 : 22:31:03
|
I know...the client came up with this crazy idea...I just wanted to make confirm with you guys and make sure this is not feasible to show in the report...Thanks all for your time. |
 |
|
|