SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Add Total to the query


Add Total to the query

Author
Message
PSB
PSB
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2571 Visits: 1565
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
Abu Dina
Abu Dina
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3061 Visits: 3325
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
HanShi
HanShi
SSCrazy Eights
SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)

Group: General Forum Members
Points: 8772 Visits: 3718
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’! **
vignesh.ms
vignesh.ms
SSC Eights!
SSC Eights! (844 reputation)SSC Eights! (844 reputation)SSC Eights! (844 reputation)SSC Eights! (844 reputation)SSC Eights! (844 reputation)SSC Eights! (844 reputation)SSC Eights! (844 reputation)SSC Eights! (844 reputation)

Group: General Forum Members
Points: 844 Visits: 516
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


David Burrows
David Burrows
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16854 Visits: 10127

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.


hunchback
hunchback
SSChasing Mays
SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)

Group: General Forum Members
Points: 619 Visits: 639
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;





Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search