我有一个表格结果如下
<messageProcessor xmlns="http://ws.apache.org/ns/synapse" class="org.apache.synapse.message.custom.processor.impl.CustomSamplingProcessor" name="CustomSamplingProcessor" messageStore="JMSMS">
<parameter name="reg">/custom-message-processor/customprocessor.properties</parameter>
</messageProcessor>
我想要实现的是合并所有行的计数(其中列计数被分组)属于IN(1,4)。但是,在我的所有研究中,我发现的所有方法都是基于每行的共同值(相同的id等)合并行的方法。
有没有办法根据IN标准合并行,所以我知道是否应该进一步研究它?
答案 0 :(得分:2)
工会怎么样?
select
1 as Code,
sum(Counts1) as Counts1,
sum(Counts2) as Counts2,
sum(TotalCount) as TotalCounts
from
YourTable
where
code in (1,4)
union
select *
from
YourTable
where
code not in(1,4)
答案 1 :(得分:2)
假设您将有许多分组(请参阅@Groupings映射表)
您可以通过LEFT JOIN
示例强>
Declare @YourTable Table ([Code] varchar(50),[Counts1] int,[Counts2] int,[TotalCounts] int)
Insert Into @YourTable Values
(1,10,20,30)
,(4,15,18,33)
,(5,5,14,19)
Declare @Groupings Table (Code varchar(50),Grp int)
Insert Into @Groupings values
(1,1)
,(4,1)
select code = Isnull(B.NewCode,A.Code)
,Counts1 = sum(Counts1)
,Counts2 = sum(Counts2)
,TotalCounts = sum(TotalCounts)
From @YourTable A
Left Join (
Select *
,NewCode = (Select Stuff((Select ',' + Code From @Groupings Where Grp=B1.Grp For XML Path ('')),1,1,'') )
From @Groupings B1
) B on (A.Code=B.Code)
Group By Isnull(B.NewCode,A.Code)
<强>返回强>
code Counts1 Counts2 TotalCounts
1,4 25 38 63
5 5 14 19
如果它有助于可视化,则子查询生成
Code Grp NewCode
1 1 1,4
4 1 1,4
答案 2 :(得分:1)
对计数求和,从select语句中删除代码。使用case语句将新列添加到组1和组4,然后命名此组N.然后在SQL组中由groupN。
你是对的,分组必须基于共同的价值。因此,通过创建新列,您就可以实现这一目标。