如果任何值存在于重复单元号的三列中的任何一列中,我想要求和。如果我使用以下查询,则会出错。任何帮助赞赏。谢谢
select RANK() OVER(PARTITION BY unitNumber order by tableA.id) as unitnumber, ColA, ColC, ColB
from tableA join
tableB on tableA.id = TableB.id
where unitNumber > 1
and tableB .adm_Date between 01-01-2013 and 12-31-2013
Data:
unitNumber colA colB ColB
235 402 88 null
245 null 501 522
255 110 550 null
245 85 null 101
215 90 null 20
275 852 225 null
215 70 null 20
225 null null null
输出应为
unitNumber colA colB ColB
235 402 88 null
245 85 501 623------added the values in colA, ColB
275 852 225 null
215 160 null 40 ------added the calues colA and ColB
答案 0 :(得分:0)
你不需要排名功能。你需要的只是按UnitNumber分组。我基于您的源和目标数据来查询。
SELECT UnitNumber, SUM(ISNULL(ColA, 0)) ColA, SUM(ISNULL(ColB, 0)) ColB, SUM(ISNULL(ColC, 0)) ColC
from tableA join
tableB on tableA.id = TableB.id
where unitNumber > 1
and tableB.adm_Date between 01-01-2013 and 12-31-2013
GROUP BY UnitNumber
假设所有数据都在同一个表中
SELECT UnitNumber, SUM(ISNULL(ColA, 0)) ColA, SUM(ISNULL(ColB, 0)) ColB, SUM(ISNULL(ColC, 0)) ColC
from tableA
where unitNumber > 1
and tableA.adm_Date between 01-01-2013 and 12-31-2013
GROUP BY UnitNumber