Sql server 如何使用sql server附加行数据?
测试数据Sql server 如何使用sql server附加行数据?,sql-server,sql-server-2008,Sql Server,Sql Server 2008,测试数据 Name count percentage location ABC 2 100.00% india ABC 1 90.00% us ABC 4 50.00% null ABC 3
Name count percentage location
ABC 2 100.00% india
ABC 1 90.00% us
ABC 4 50.00% null
ABC 3 100.00% uk
我想用india location追加location空记录的计数,需要删除空记录吗?
表示位置为null的第三行,然后需要使用india count i追加计数4。e2+4=6。
请向任何人建议如何使用sql实现此结果?下面的解决方案占最大百分比
( select name,count,percentage,location
from testdate
where location is not null and location<>'india'
)
union all
( select name,sum(count),max(percentage),max(location)
from testdate
where location='india' or location is null
group by name
)
试试这样的
select name,sum(count),max(percentage),coalesce(location,'India')
from Yourtable
group by name,coalesce(location,'India')
目前,它将从NULL或India中获取最大百分比只需使用一个案例将NULL更改为India,然后使用GROUP BY添加计数
输出
编辑:
第二种情况可以替换为
COALESCE("location", 'india') as "location"
你能把你的欲望输出也列为表格吗?这会改变百分比吗?这是一个很好的起点。我认为根据上述结果生成的查询可以更改。你能发布它也会改变总体百分比吗…@Sravanti如何影响百分比?请告诉我们逻辑和结果
| Name | sum | max | location |
|------|-----|---------|----------|
| ABC | 1 | 90.00% | us |
| ABC | 6 | 100.00% | india |
| ABC | 3 | 100.00% | uk |
COALESCE("location", 'india') as "location"