Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 sum a count.

Author  Topic 

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2014-12-09 : 10:55:06
[code]
Create Table #PL (TransT_lgnNumber int,MonthF varchar(10))
--
--jan
insert into #PL(TransT_lgnNumber,MonthF)
select count(*),'Jan 2013' from tblTrans_Ticket
where TransT_strParentTType_strCode = '0177'
and TransT_dtmRealTransTime between '20130101' and '20130111'
group by TransT_lgnNumber

-- select top 100 * from #PL
drop table #PL
[/code]
This will give me multiple count columns. How can i get the sum in one row so i should have -- "sum_column,'Jan 2013'"

Thanks.

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-12-09 : 12:03:13
you mean like this?

[code[
select top 100 cast(TransT_lgnNumber as varchar(5)) + ',' + MonthF from #PL
[/code]
Go to Top of Page

djj55
Constraint Violating Yak Guru

352 Posts

Posted - 2014-12-09 : 15:49:19
Will this work?

Create Table #PL (TransT_lgnNumber int,MonthF varchar(10))
--
--jan
insert into #PL(TransT_lgnNumber,MonthF)
select count(*),'Jan 2013'
from tblTrans_Ticket
where TransT_strParentTType_strCode = '0177'
and TransT_dtmRealTransTime between '20130101' and '20130111';

-- select top 100 * from #PL
drop table #PL


djj
Go to Top of Page

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2014-12-09 : 17:33:50
I think i did something wrong and i was not getting the expected result. Will take a look tomorrow as i don't have server access right now.
I think i also took the group by out but this led to x2 count as the TransT_lgnNumber has x2 id's in every transaction and i was trying to group it to one.
Do you know why group by fails? If that was the case.
Thanks both.
Go to Top of Page
   

- Advertisement -