1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
CREATE FUNCTION dbo.fnScheduleView(@date datetime) RETURNS TABLE AS RETURN WITH DP AS (SELECT DP=DATEPART(weekday, @date )) SELECT countryCode, cityCode, itemCode, storeType, vendorCode, gridCodeX, gridCodeY, facings, brand, rc, storeCode, storeName,ARABIC, ItemName, upcCode, TREF, DSC1,DSC2,DSC3,DSC4 FROM ( SELECT *, CASE DP.DP WHEN 1 THEN [SunDCS1] WHEN 2 THEN [MonDCS1] WHEN 3 THEN [TueDCS1] WHEN 4 THEN [WedDCS1] WHEN 5 THEN [ThuDCS1] WHEN 6 THEN [FriDCS1] WHEN 7 THEN [SatDCS1] END DSC1, CASE DP.DP WHEN 1 THEN [SunDCS2] WHEN 2 THEN [MonDCS2] WHEN 3 THEN [TueDCS2] WHEN 4 THEN [WedDCS2] WHEN 5 THEN [ThuDCS2] WHEN 6 THEN [FriDCS2] WHEN 7 THEN [SatDCS2] END DSC2, CASE DP.DP WHEN 1 THEN [SunDCS3] WHEN 2 THEN [MonDCS3] WHEN 3 THEN [TueDCS3] WHEN 4 THEN [WedDCS3] WHEN 5 THEN [ThuDCS3] WHEN 6 THEN [FriDCS3] WHEN 7 THEN [SatDCS3] END DSC3, CASE DP.DP WHEN 1 THEN [SunDCS4] WHEN 2 THEN [MonDCS4] WHEN 3 THEN [TueDCS4] WHEN 4 THEN [WedDCS4] WHEN 5 THEN [ThuDCS4] WHEN 6 THEN [FriDCS4] WHEN 7 THEN [SatDCS4] END DSC4 FROM dbo.ScheduleView CROSS JOIN DP ) SQ WHERE DSC1=1 OR DSC2=1 OR DSC3=1 OR DSC4=1 GO