2010-03-30 116 views
2

如何進行旋轉功能旋轉功能2008

我有DATAS

Day  Period subject fromtime totime 

---------------------------------------------- 
Monday 1st English 9:30 10:15 

Monday 1st English 9:30 10:15 

Monday 5th English 1:30 2:20 

Monday 8th English 3:40 4:30 

一個表,但我需要的格式

day  period(1st)  2nd 3rd...... 5th...   8th 

-------------------------------------------------------------------------- 
Monday 1st   nill nill  5th    8th 
     english      english  english 

Tuesday ....... 

這樣。

如何執行pivot函數以獲得此格式。

請幫我........

我用我的查詢

SELECT DAY, [1st],[2nd],[3rd],[4th], [5th],[6th],[7th], [8th] 
FROM 
    (
     SELECT Day, Period, Subject,FromTime,ToTime 
     FROM StaffTimeTableEntry 
    ) up 
PIVOT (Min(Subject) FOR Period IN ([1st],[2nd],[3rd],[4th], [5th],[6th],[7th], [8th])) AS pvt 
order by Day 

,結果出來的

day  1st  2nd  3rd  4th 5th  6th  7th 8th 

---------------------------------------------------------------------------------- 
Friday NULL NULL NULL NULL NULL NULL English NULL 

Monday NULL NULL NULL NULL English NULL NULL NULL 

Monday NULL NULL NULL NULL NULL NULL NULL English 

Monday English NULL NULL NULL NULL NULL NULL NULL 

Saturday NULL NULL NULL NULL NULL English NULL 

Thursday NULL NULL NULL English NULL NULL NULL 

Tuesday NULL English NULL NULL NULL NULL NULL NULL 

Tuesday NULL NULL NULL NULL NULL English NULL NULL 

Wednesday NULL NULL NULL NULL NULL English NULL  

但這裏也有我3日星期一的記錄,而不是作爲一個單一的星期一記錄...

如何獲得一個記錄每一天...?

回答

2

試試這個:

SELECT DAY, [1st],[2nd],[3rd],[4th], [5th],[6th],[7th], [8th] 
FROM 
    (
     SELECT Day, Period, Subject --,FromTime,ToTime 
     FROM StaffTimeTableEntry 
    ) up 
PIVOT (Min(Subject) FOR Period IN ([1st],[2nd],[3rd],[4th], [5th],[6th],[7th], [8th])) AS pvt 
order by Day 

沒有「消費」由PIVOT操作者將保持在最終結果集,增加了其基數的任何列。

+0

雅它的作品了,我..感謝了很多 – Innova 2010-03-30 06:07:09

+0

但這裏有一天,這種格式 週五 週一 週六 週四 週二週三 但我需要天才能.. – Innova 2010-03-30 06:42:41

+0

'秩序在'星期一'的情況下,然後是'星期二',然後是2',等等。或者找到一些方法將您的日期轉換爲日期,然後按順序排列。 – 2010-03-30 06:48:43