Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
nice123ej
Starting Member
48 Posts |
Posted - 2011-01-02 : 06:48:58
|
| HiI have table that has list of students and homeworks per weekThis is my tableHomeWorks (StNo INT,WeekName Varchar(50),HomeworksNo INT)when i do this select SELECT * FROM HomeWorksI get this result2009001 Monday 22009001 Tuesay 12009001 Thursday 32010029 Thursday 22010029 Friday 22010157 Monday 42010157 Wednesday 1what i want is to get all week days for each student with 0 in thier homework nosomething like this2009001 Monday 22009001 Tuesay 12009001 Wednesday 02009001 Thursday 32009001 Friday 02010029 Monday 02010029 Tuesday 02010029 Wednesday 02010029 Thursday 22010029 Friday 22010157 Monday 42010157 Tuesay 02010157 Wednesday 12010157 Thursday 02010157 Friday 0what i tried to do is to create a Temp Table @WeekDays (WeekName varchar(50))and Inserted all Week Days in it (Monday, Tuesday, Wednesday, Thursday, Friday)and outer join it with HomeWorks table but did not work with meI want the optimum solution for this scenariocan someone help me here?!Thanks |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-01-02 : 08:25:16
|
| ;with dw as(select dw = 'Monday'union all select 'Tuesday'union all select 'Wednesday'union all select 'Thursday'union all select 'Friday'),StNo as(select distinct StNo from HomeWorks))select d.StNo, dw.dw, coalesce(h.HomeworksNo, 0)from dates dwcross join days dleft join HomeWorks hon h.StNo = d.StNoand h.WeekName = dw.dw==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|