我在MSSQL 2012r2表中具有以下数据:
id data1 amount
------------------
10 abc 95.00
10 NULL 312.00
20 def 16.00
30 gqi 32.00
预期的查询结果:
id data1 amount
------------------
10 abc 407.00
20 def 16.00
30 gqi 32.00
仅使用sql可能吗?
答案 0 :(得分:2)
使用Window window = yourDialog.getWindow();
if (window != null) {
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setAttributes(lp);
}
,max()
聚合函数和sum()
group by
答案 1 :(得分:2)
case when
也可以帮助您
select id, case when data1 is null then 'abc' else data1 end
as data1, sum(amount) from table_name
group by id,case when data1 is null then 'abc' else data1 end
答案 2 :(得分:1)
如果您希望NULL
看起来像'abc'
,请使用coalesce()
:
select id, coalesce(data1, 'abc') as data1,
sum(amount) as amount
from t
group by id, coalesce(data1, 'abc');
这将创建一个任意的'abc'
行(如果不存在)。如果只希望将其与 any 的现有值合并:
select t.id, coalesce(t.data1, t2.data1) as data1,
sum(t.amount) as amount
from t outer apply
(select top (1) t2.data1
from t t2
where t2.id = t.id and t2.data1 is not null
) t2
group by t.id, coalesce(t.data1, t2.data1);
答案 3 :(得分:0)
请尝试这个。
select t1.id, coalesce(t1.data1,t2.data1)
from Table t1 join (select id, sum(amount) from Table group by id) t2
on t1.id = t2.id
在这里,主要的问题只是如何在多列之间获取非空值。因此我们可以通过 coalesce函数来实现。
什么是合并函数?
COALESCE()函数返回列表中的第一个非空值。
示例:
SELECT COALESCE(NULL,NULL,NULL,'W3Schools.com',NULL,'Example.com');
执行上述sql查询后,它会返回。
W3Schools.com
第一个不是空值。
答案 4 :(得分:0)
选择id,max(data1)data1,sum(amount)数量 来自表名 按ID分组
答案 5 :(得分:-1)
select id, COALESCE(data1 ,'anyvalue') as data1, sum(amount) as amount
from <table>
group by id , COALESCE(data1,'anyvalue')