使用不同行的SQL Server分组在One Rows中合并

时间:2015-01-15 10:55:20

标签: sql-server

某些行值如此

enter image description here

现在我想对此进行分组想要单行中的单个结果,并且每当有相同周的两个值时,例如:Week4 Merge" Yes"和"不" in"是"

enter image description here

1 个答案:

答案 0 :(得分:0)

你的桌子设计不是很好。你应该更好地改变它。阅读规范化

但如果您无法更改,请使用

select studentcode, modulecode, eventtype, semester,
       max(week1) as week1,
       max(week2) as week2,
       max(week3) as week3,
       max(week4) as week4,
       max(week5) as week5,
       max(week6) as week6,
       max(week7) as week7,
       max(week8) as week8,
       max(week9) as week9,
       max(week10) as week10,
       max(week11) as week11,
       max(week12) as week12,
from your_table
group by studentcode, modulecode, eventtype, semester