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

在 tSql 中,我如何将这些作为一行返回,以说明 1337 在某些时候是红色、棕色和绿色?

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

3 回答 3

3

使用Max功能Grouping by fk

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

如果red, brown&greenbit type字段然后尝试如下

select fk, max(red+0) red, max(brown+0) brown, max(green+0) green
from yourTable
group by fk
于 2013-02-25T11:45:24.783 回答
2

通过分组:

Select  fk , 
        Max(red) as red,
        Max(brown) as brown, 
        Max(green) as green 
FROM    myTableName
GROUP   BY fk
于 2013-02-25T11:45:31.573 回答
1

像这样:

SELECT  fk
       ,MAX(red)
       ,MAX(brown)
       ,MAX(green)
FROM   yourtable
GROUP  BY fk
于 2013-02-25T11:45:40.080 回答