1

私の問題はこの質問のようなものですが、別のテーブルからデータをフェッチしています:

Name   ID  Subject-name    obtained-marks
gumman  9   English              3
gumman  9   Islamic-Studies      4
gumman  9   Pak Studies          3 

次のような出力が必要です。

ID   Name   English   Islamic-Studies   Pak-studies
9   gumman   3             4                3

これどうやってするの?

4

2 に答える 2

4
select ID,
       Name,
       [English],
       [Islamic-Studies],
       [Pak Studies]
from YourTable 
pivot
  (
    min([obtained-marks]) for [Subject-name] in ([English],
                                                 [Islamic-Studies],
                                                 [Pak Studies])
  ) as P
于 2012-04-17T06:58:34.517 に答える
2

これを試して:

Select s.ID, s.Name, 
       MAX(case WHEN s.SubjectName = 'English'         THEN s.ObtainedMarks end) as 'English',
       MAX(case WHEN s.SubjectName = 'Islamic-Studies' THEN s.ObtainedMarks end) as 'Islamic-Studies',
       MAX(case WHEN s.SubjectName = 'Pak Studies'     THEN s.ObtainedMarks end) as 'Pak-studies'
From Students s
GROUP BY s.ID, s.Name

デモ

于 2012-04-17T07:06:20.323 に答える