2014-01-06 75 views
0

我在SQL Server 2008中的兩個表:查詢來獲取不同行的數據到一個

會議:

MeetingID(bigint) MeetingStartTime(datetime) MeetingEndTime(datetime) 


100     2014-01-05 17:10:13.000 2014-01-05 17:10:13.000 

----------------------------------------------------------------------- 

反饋:

MeetingID(bigint) QuestionID(int) Response(varchar 210) 

    100       1    Very Good 

    100       2    Average 

------------------------------------------------------------- 

我想要的結果是今日12.00AM-11.59PM:

MeetingID MeetingStartTime MeetingEndTime QuestionID Response QuestionID Response 

100  2014-01-05 17:10 2014-01-05 17:10  1  Very Good 2  Average 

我寫了一個查詢爲:

select m.MeetingID, m.MeetingStartTime, mMeetingEndTime, r.QuestionID, r.Response 
from MEETING minner joinFEEDBACK ronm.MeetingID=r.MeetingID 
where m.MeetingID<GETDATE() and m.MeetingID>DATEADD(hour,-12,GETDATE()) 

但結果我得到的是這樣的:

MeetingID MeetingStartTime MeetingEndTime QuestionID Response 

    100  2014-01-05 17:10 2014-01-05 17:10  1   Very Good 

    100  2014-01-05 17:10 2014-01-05 17:10  2   Average 

請幫助。

回答

0

請使用下面的代碼爲您的要求:

如果u有那麼多響應ID在下面的腳本中添加這些多列。

;with cte as 
    (
    select a.MeetingId,convert(date,a.MeetingStartTime) MeetingStartTime, 
    convert(date,a.MeetingEndTime) MeetingEndTime, 
    b.QestionID,b.Response 
    from MEETING a 
    join FEEDBACK b on a.MeetingId = b.MeetingId 
    where convert(date,a.MeetingStartTime) = convert(date,getdate()) and convert(date,a.MeetingEndTime) = convert(date,getdate()) 
    ) 

     select a.MeetingId,a.MeetingStartTime,a.MeetingEndTime, 
     QuestionID = (select QestionID from FEEDBACK b where a.MeetingId = b.MeetingId and b.QestionID = min(a.QestionID)), 
     Response = (select Response from FEEDBACK b where a.MeetingId = b.MeetingId and b.QestionID = min(a.QestionID) ), 
     QuestionID = (select QestionID from FEEDBACK b where a.MeetingId = b.MeetingId and b.QestionID = min(a.QestionID) + 1), 
     Response = (select Response from FEEDBACK b where a.MeetingId = b.MeetingId and b.QestionID = min(a.QestionID) +1) 
     from 
     cte a 
     group by 
     a.MeetingId,a.MeetingStartTime,a.MeetingEndTime 
+0

問題ID可能是隨機的,不一定是1,2等等...... – user2681748

相關問題