2017-09-19 4 views
-1

1人のレコードにつき複数のアラートレコード(1人につき最大10個)を表示するにはどうすればよいですか?行番号を使用してレコードに番号を付けて出力に配置しようとしていますが、クロス・ステートメントの「where」パラメータに誤った構文エラーがあるため、書いたコードは機能しません。私は必要なものを達成するために複数の方法を試しましたが、エラーを乗り越えることはできません。助けてください。行番号を使用して複数のレコードを結合する方法

select 
sd.[student_number], 
a.[health_alert], 
a.[comments], 
b.[health_alert], 
b.[comments] 

from student_enrollmentcube as se, 
    student_demographicscube as sd 

cross apply (select a.[health_alert], a.[comments], (select row_number() 
over (partition by a.[student_id] order by a.[student_id]) as r1 from 
    ar_student_health_alerts) a) where a.[student_id] = sd.[student_id] and 
    r1 = 1) 
cross apply (select b.[health_alert], b.[comments], (select row_number() 
over (partition by b.[student_id] order by b.[student_id]) as r2 from 
    ar_student_health_alerts) b) where b.[student_id] = sd.[student_id] and 
    r2 = 2) 

where se.student_id = sd.student_id and 
    se.enrollment_status= 'active' and 
    se.[current_academic_year] = 'y' 
+0

これはSQLに関連していますか?その場合は、適切にタグを付けてください。 – genpfault

+0

正確なエラーメッセージも含めてください –

答えて

0

あなたは他の不完全なselect文やリテラル値を持つテーブルの別名のさえ比較です内に埋め込まれ、不完全なSELECT文を含め、構文を使用して複数の問題を、持っています。

これを分割することをおすすめします。クロス・ステートメントは、外部表への参照を除いて、単独で実行する必要があります。言い換えれば

、この作業を取得:あなたが適用クロスでそれを使用しようとする前に

(select a.[health_alert], a.[comments], (select row_number() 
over (partition by a.[student_id] order by a.[student_id]) as r1 from 
    ar_student_health_alerts) a) where a.[student_id] = sd.[student_id] and 
    r1 = 1) 

を。

関連する問題