Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Add Total to the query Expand / Collapse
Author
Message
Posted Wednesday, December 18, 2013 5:38 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 9:03 AM
Points: 282, Visits: 1,015
Fi,

I have a table with the Group name and Total Count by group . I need to add a 'Total' and summation of all counts at the end .

CREATE TABLE #TempTable
(GroupName VARCHAR(10),NumberOfCases INT )


INSERT INTO #Temp Table (GroupName,NumberOfCases)
VALUES ('Grp A',10)
INSERT INTO #Temp Table (GroupName,NumberOfCases)
VALUES ('Grp B',20)

SELECT * from #TempTable

I want the result to be like the below :

Grp A 10
Grp B 20
Total 30

Thanks,
PSB
Post #1524063
Posted Wednesday, December 18, 2013 5:41 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Wednesday, April 09, 2014 2:57 AM
Points: 704, Visits: 3,270
Not sure if I'm over-simplifying your problem but how about the below?!

INSERT INTO #TempTable (GroupName, NumberOfCases)
SELECT 'Total', SUM(NumberOfCases)
FROM #TempTable



---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
Post #1524065
Posted Wednesday, December 18, 2013 5:48 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 8:19 AM
Points: 2,078, Visits: 2,411
If the solution from Abu Dina is oversimplified, below is a more practical solution.
SELECT GroupName, SUM(NumberOfCases) as NumberOfCases
FROM #TempTable
GROUP BY GroupName

UNION ALL

SELECT 'Grand Total' as GroupName, SUM(NumberOfCases)
FROM #TempTable



** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Post #1524070
Posted Wednesday, December 18, 2013 6:24 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 2:45 AM
Points: 123, Visits: 330
PSB (12/18/2013)
Fi,

I have a table with the Group name and Total Count by group . I need to add a 'Total' and summation of all counts at the end .

CREATE TABLE #TempTable
(GroupName VARCHAR(10),NumberOfCases INT )


INSERT INTO #Temp Table (GroupName,NumberOfCases)
VALUES ('Grp A',10)
INSERT INTO #Temp Table (GroupName,NumberOfCases)
VALUES ('Grp B',20)

SELECT * from #TempTable

I want the result to be like the below :

Grp A 10
Grp B 20
Total 30

Thanks,
PSB



Hope following will give you the better solution...


with cte as (
select groupname ,SUM(numberofcases)numberofcases from #TempTable group by groupname with rollup
)

select isnull (GROUPname , 'Total' )GROUPname ,numberofcases from cte

Post #1524093
Posted Wednesday, December 18, 2013 6:45 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:06 AM
Points: 6,817, Visits: 6,316
SELECT COALESCE(GroupName,'Total') AS [GroupName],SUM(NumberOfCases) AS [NumberOfCases] 
FROM #TempTable
GROUP BY GroupName WITH ROLLUP
ORDER BY GROUPING(GroupName) ASC




Far away is close at hand in the images of elsewhere.

Anon.

Post #1524106
Posted Wednesday, December 18, 2013 9:41 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 10:45 AM
Points: 99, Visits: 501
You can also use the sub-clause GROUPING SETS to specifiy the groups. Use function GROUPING to sort the data as desired (Total at the end).

USE tempdb;
GO
SELECT
GroupName, NumberOfCases
INTO #Temp
FROM
(
VALUES
('Grp A',10),
('Grp B',20)
) AS T(GroupName,NumberOfCases);

SELECT
CASE WHEN GROUPING(GroupName) = 1 THEN 'Total' ELSE GroupName END as GroupName,
SUM(NumberOfCases) AS sum_NumberOfCases
FROM
#Temp
GROUP BY
GROUPING SETS (
(GroupName),
()
)
ORDER BY
GROUPING(GroupName),
GroupName;

DROP TABLE #Temp;




Post #1524211
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse