2

我怎样才能从这个表格中转出一张桌子:

S_ID          SUBJECT          MARK
1             English          90
1             Math             40
1             Computer         30
2             English          85
2             Math             10
2             Computer         06
3             English          10
3             Math             20
3             Computer         40

到这个表格

S_ID       English        Math         Computer
1          90             40           30
2          85             10           06
3          10             20           40

使用 SQL 代码,

我正在使用 MS Access 2010,

谢谢

4

3 回答 3

4
SELECT S_ID ,
       MAX(CASE WHEN subject = 'English' THEN mark ELSE null END) AS English,
       MAX(CASE WHEN subject = 'Math' THEN mark ELSE null END) AS Math,
       MAX(CASE WHEN subject = 'Computer' THEN mark ELSE null END) AS Computer
FROM myTable
GROUP BY S_ID 
于 2011-07-04T12:32:27.927 回答
4

您需要一个交叉表查询,该查询将为每个主题生成一列;

TRANSFORM Sum(MARK) AS TotalMark
SELECT 
   S_ID
FROM marks
   GROUP BY S_ID
PIVOT SUBJECT;
于 2011-07-04T12:51:51.587 回答
0

如果这是一次性练习,您可以按顺序使用许多语句:如下所示:

insert into new_table
select distinct s_id, 0,0,0
from old_table

然后一系列更新

update new_table n
set english = (select english from old_table where s_id = n.s_id )
where s_id = n.s_id
于 2011-07-04T12:34:39.760 回答