Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to sum to rows value in query on condition basis
for example

id count
1 125
2 3
3 111
32 169


i want to sum where id=2 and id=32 these two rows value can sum
like
id count
1 125
2 171 (3+169)
3 111

kindly suggest me how can i do this

What I have tried:

SQL
select s.ShowCauseStatusId, s.CountA, case  when s.ShowCauseStatusId =2 or s.ShowCauseStatusId =32 then SUM(CountA)  else  ' '  end as d from ( select

                ipgc.ShowCauseStatusId
               ,count(*) as CountA

                from dbo.MemoIntellectualPropertyGoodsClass mipgc

                left join dbo.IntellectualPropertyGoodsClass ipgc on mipgc.IntellectualPropertyGoodsClassId = ipgc.Id and ipgc.DataEntryStatusId=1

                left join dbo.MemoIntellectualProperty mip on mipgc.MemoProductId = mip.Id and mip.DataEntryStatusId=1

                left join dbo.IntellectualProperty ip on ipgc.IntellectualPropertyId = ip.Id and ip.DataEntryStatusId=1

                left join dbo.Memo m on mip.MemoId = m.Id and m.DataEntryStatusId=1

                left join dbo.GoodsClass gc on ipgc.GoodsClassId = gc.Id and gc.DataEntryStatusId=1

                left join dbo.Corporate c on ip.ProductCorporateId = c.Id and c.DataEntryStatusId=1

                left join dbo.ContactPerson cp on c.AccountManagerId = cp.Id and cp.DataEntryStatusId=1

                left join dbo.StatusCorrespondence sc on ipgc.StatusId = sc.Id and sc.DataEntryStatusId=1

                left join dbo.TrademarkLetterRemark ltr on ipgc.LetterRemarkId = ltr.Id

                where mipgc.DataEntryStatusId = 1

                and ip.IntellectualPropertyTypeId = 4

                and m.IntellectualPropertyCaseTypeId = 1

               
                and (ipgc.StatusId = 9)
					and c.CorporateStatusId=1
			and (ipgc.ShowCauseStatusId in (1,2,3) 
			or ipgc.StatusId=32)
					and c.CorporateStatusId=1
and ipgc.ApplicationNumber is not null
			 group by ipgc.ShowCauseStatusId 
             

				union all
				select

                ipgc.StatusId 
                ,count(*) as Count


                from dbo.MemoIntellectualPropertyGoodsClass mipgc

                left join dbo.IntellectualPropertyGoodsClass ipgc on mipgc.IntellectualPropertyGoodsClassId = ipgc.Id and ipgc.DataEntryStatusId=1

                left join dbo.MemoIntellectualProperty mip on mipgc.MemoProductId = mip.Id and mip.DataEntryStatusId=1

                left join dbo.IntellectualProperty ip on ipgc.IntellectualPropertyId = ip.Id and ip.DataEntryStatusId=1

                left join dbo.Memo m on mip.MemoId = m.Id and m.DataEntryStatusId=1

                left join dbo.GoodsClass gc on ipgc.GoodsClassId = gc.Id and gc.DataEntryStatusId=1

                left join dbo.Corporate c on ip.ProductCorporateId = c.Id and c.DataEntryStatusId=1

                left join dbo.ContactPerson cp on c.AccountManagerId = cp.Id and cp.DataEntryStatusId=1

                left join dbo.StatusCorrespondence sc on ipgc.StatusId = sc.Id and sc.DataEntryStatusId=1

                left join dbo.TrademarkLetterRemark ltr on ipgc.LetterRemarkId = ltr.Id

                where mipgc.DataEntryStatusId = 1

                and ip.IntellectualPropertyTypeId = 4

                and m.IntellectualPropertyCaseTypeId = 1

               
              --  and (ipgc.StatusId = 9)
					and c.CorporateStatusId=1
			and  ipgc.StatusId=32
					and c.CorporateStatusId=1
and ipgc.ApplicationNumber is not null
			
                group by ipgc.StatusId

				 ) as s
Posted
Updated 13-Dec-18 22:58pm
v2
Comments
Mohibur Rashid 13-Dec-18 22:58pm    
Why are you picking 2 and 32? why are you showing 2 as id?
Noman Suleman 13-Dec-18 23:50pm    
yes i have replaced 32 with 2 then it works.

Not 100% sure what you are asking for.
But if you are trying to get sum of count for id where the right most number is the same, then here is a way of doing it in tsql.
SQL
with CntAll as (
	select 1 id, 125 cnt
	union select 2, 3
	union select 3, 111
	union select 32, 169
)
select 
	right(id, 1) rightid,
	sum(cnt) SumCnt
from CntAll
group by
	right(id, 1)
;
--what you will get
--rightid	SumCnt
--1	125
--2	172
--3	111

--Also would like to mention 3+169 = 172 and not 171, I assume a typo


--similar to above but not grouping by right(id, 1) but getting it as a sub type query and grouping the rightid instead - maybe faster execution time
with CntAll as (
	select 1 id, 125 cnt
	union select 2, 3
	union select 3, 111
	union select 32, 169
), CntWithRightId as (
	select 
		right(id, 1) rightId,
		id,
		cnt
	from CntAll
)
select 
	rightid,
	sum(cnt) SumCnt
from CntWithRightId
group by
	rightid
;

Hope that helps you out.
 
Share this answer
 
v2
Comments
Noman Suleman 13-Dec-18 23:33pm    
thanks buddy for guidance
jaket-cp 15-Dec-18 3:03am    
glad to help :)
Try this
SELECT [id], SUM([count]) AS [count]
FROM (
		SELECT (CASE [id] WHEN 32 THEN 2 ELSE [id] END) AS [id], [count]
		FROM [YourTable]) AS result
GROUP BY [id]
 
Share this answer
 
Comments
Noman Suleman 13-Dec-18 23:33pm    
thanks for answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900