sql将两个不同的行条目组合在一起

时间:2017-09-18 14:09:20

标签: sql sql-server

我的数据库中有两种不同的运动类型,这意味着同样的事情

冲刺赛车1, 冲刺赛车2

我需要专门为Dash Racing做一个客户计数,我希望sql能够结合赛车1&的客户数量。 2而不是我必须在之后手动添加它们。

有没有办法做到这一点?

代码如下:

select  Sport ,
        count(distinct customerID ) 'CustCount' ,
        sum(Stake) 'TotalStake' ,
        sum(Return) 'TotalReturn'
from    table1
where   sport in ('dash horse racing' , 'dash horse racing 2')
group by Sport

返回一个类似于此的表:

Sport   -----------------------   CustCount-------- TotalStake  --------    TotalReturn
dash Horse Racing 2
dash Horse Racing 1

然而我想结合VS赛车1& 2,创造一种新的联合运动VS. 赛马',得到像这样的表:

*Sport ----------- CustCount---------TotalStake----------TotalReturn*

*dash Horse Racing*

任何帮助将不胜感激。 感谢。

1 个答案:

答案 0 :(得分:0)

只需选择您想要的文字,然后通过

离开小组
select  'dash horse racing' as Sport ,
        count(distinct customerID ) 'CustCount' ,
        sum(Stake) 'TotalStake' ,
        sum(Return) 'TotalReturn'
from    table1
where   sport in ('dash horse racing' , 'dash horse racing 2')