我有兩個表register
和att_bottom
,我只想顯示某個建築物的遲到時間,根據今天的日期顯示逗號分隔的學生。 這是加入兩個表時,將顯示數據的方式:連接行值sql server 2008 r2
Student ID | Building | Period | Grade
12345 2 1 11
12345 2 5 11
2 1 12
我想這一點:
Student ID | <u>Building | Period | Grade
12345 2 1,5 11
2 1 12
這是我的查詢:
select r.STUDENT_ID,
r.BUILDING ,
(select ab.attendancePeriod + ','
from att_bottom ab
where ab.STUDENT_ID = r.student_id
and ab.building = '2'
and ab.attendance_c ='T'
and ab.SCHOOL_YEAR =2014
CONVERT(date,ab.attendance_date,102) = convert(date,getdate(),102)
FOR XML PATH ('')) AS PERIODS,
r.GRADE
FROM register r
where r.CURRENT_STATUS = 'A'
and r.BUILDING ='2'
我讓所有的2號樓的學生,即使他們沒有T的attedance_c;對於週期NULL值被檢索:
Student ID | Building | Period | Grade
12345 2 1 , 5 11
2 1 , 12
95687 2 NULL 09
78417 2 NULL 10
20357 2 NULL 11
我試圖and ab.attendancePeriod is Not NULL
和我仍然得到同樣的結果。
有什麼想法?
什麼'DataType'您正在使用您的'期間'欄? – prograshid
我得到三行與該查詢,而不是5(在我糾正語法錯誤後)。你能否仔細檢查你是如何得到這些結果的? (例如,95687,78417等來自哪裏 - 它們是否僅存在於註冊中,但不存在於att_bottom中?) –
這些數字是我用來說明問題的隨機ID。 – user1308665