如何在第一个和第二个记录中设置NULL?

时间:2012-11-02 12:45:10

标签: sql sql-server stored-procedures

我有以下查询:

Declare @t Table(Id int identity,Groep int,[Date] Datetime,MondayMorning      
Int,MondayEvening Int,TuesdayMorning Int,TuesdayEvening Int ,WednesdayMorning    
Int,WednesdayEvening Int)
Insert Into @t Select 2,'2012-01-01',2,0,2,2,2,2
union all
Select 2,'2012-01-02', 2,2,2,3,2,2
union all
Select 3,'2012-01-03', 2,2,2,2,2,2


select Id, Groep ,[Date], MondayMorning, MondayEvening, TuesdayMorning,TuesdayEvening, WednesdayMorning, WednesdayEvening
from @t t
union all
select Id, Groep, [Date], MondayMorning, MondayEvening, TuesdayMorning,TuesdayEvening, WednesdayMorning, WednesdayEvening
From @t tt

WHERE tt.MondayMorning <> tt.MondayEvening
AND (tt.MondayMorning != 0 and tt.MondayEvening != 0)
OR tt.TuesdayMorning <> tt.TuesdayEvening
AND (TuesdayEvening != 0 and TuesdayMorning != 0)
OR tt.WednesdayMorning <> tt.WednesdayEvening
AND (tt.WednesdayEvening != 0 and tt.WednesdayMorning != 0)

此查询为我提供了一个实际上正确的结果。我只需要在星期二设置在Id = 2的第一条记录中的Null值。在Id = 2的第二个记录中设置TuesdayMorning = null

要了解其工作原理,请运行查询。

任何想法?

1 个答案:

答案 0 :(得分:1)

您错过了周一0值的条件:

WHERE tt.MondayMorning <> tt.MondayEvening
AND (tt.MondayMorning != 0 and tt.MondayEvening != 0)
OR tt.TuesdayMorning <> tt.TuesdayEvening
AND (TuesdayEvening != 0 and TuesdayMorning != 0)