在同一行中,我有枯燥的数据。
我有列aCust, bCust, aPart, bPart, aSM, bSM, aSales, bSales.
我想将Cust、parts和SM合并在一起,同时保持Sales分离。一些行在a和b中都有数据,有些a为空,有些b为空。我该怎么把这个结合起来?如果a和b中都有数据,则数据总是相同的(销售除外)。
发布于 2015-02-02 23:59:04
你可以用coalesce()来做这件事
select coalesce(aCust, bCust) as Cust,
coalesce(aPart, bPart) as Part,
coalesce(aSM, bSM) as SM,
aSales, bSales
from table;这将选择每个字段返回的第一个非空值。
发布于 2015-02-02 23:19:23
尝试这个查询。看上去很奇怪,但能胜任这份工作。您没有指定所使用的关系数据库管理系统(Oracle、MySQL、Server等)。这就是为什么我没有使用像ISNULL这样的东西。
select
case when aCust is null then bCust else bCust end as Cust,
case when aPart is null then bPart else bPart end as Part,
case when aSM is null then bSM else bSM end as SM,
aSales, bSales
from
tblhttps://stackoverflow.com/questions/28288093
复制相似问题