2013-02-25 67 views
0
| fk | red | brown | green | 
|1337| 1 | 0 | 0 | 
|1337| 0 | 1 | 0 | 
|1337| 0 | 0 | 1 | 

在tSql中,我將如何將這些返回爲一行,以表示1337在某些點已經紅色,棕色和綠色?將SQL連接成一行

| fk | red | brown | green | 
|1337| 1 | 1 | 1 | 

回答

3

使用Max功能Grouping by fk

select fk, max(red) red, max(brown) brown, max(green) green 
from yourTable 
group by fk 

如果redbrown & ​​bit type區,然後嘗試如下

select fk, max(red+0) red, max(brown+0) brown, max(green+0) green 
from yourTable 
group by fk 
2

通過分組:

Select fk , 
     Max(red) as red, 
     Max(brown) as brown, 
     Max(green) as green 
FROM myTableName 
GROUP BY fk 
1

像這樣:

SELECT fk 
     ,MAX(red) 
     ,MAX(brown) 
     ,MAX(green) 
FROM yourtable 
GROUP BY fk