如果字段不为null,则sql查询连接增量编号,否则自动增加它

时间:2017-01-03 12:31:08

标签: sql sql-server

我遇到了问题。

有一个表dbo.t1,这是我想要实现的结果。

实际输出

sitename    country
abc.net       NULL
abc.net       NULL
abc.net      India
abc.net      India

预期产出

sitename    country
abc.net       1
abc.net       2
abc.net      India1
abc.net      India2

所以基本上,有两个列,Sitename和Country,其值已在上面显示。空值应该用增量值代替,在这种情况下,值“印度”应该用自身+增量值替换,即'India1','India2'等等。

Pl随时可以要求澄清。

2 个答案:

答案 0 :(得分:2)

您可以使用with t as ( <your query here> ) select t.sitename, (coalesce(t.country, '') + cast(row_number() over (partition by sitename, country order by sitename) as varchar(255) ) as country from t;

 ggplot(data,aes(Var1,Var2,fill=Var1))+
  geom_point(aes(size=Var3/3),shape=21,show.legend = FALSE)+  theme(axis.title.y=element_text(vjust=0))+
  scale_size_identity()+
  theme(panel.grid.major=element_line(linetype=2,color="black",size=0.1),
        axis.text.x=element_text(angle=90,hjust=1,vjust=0))

答案 1 :(得分:0)

使用Row_numberUnion

的另一种方法
select sitename , 
isnull(country,'') + convert(varchar(100)
                    ,row_number() over (
                    partition by sitename order by sitename)
                    ) as country
from #Temp 
where country is  null

union 

select sitename , isnull(country,'') + convert(varchar(100)
                    ,row_number() over (
                    partition by sitename order by sitename)
                    ) as country
from #Temp 
where country is  not null 

<强>结果: -

enter image description here