Avatar of Jagdish Devaku
Jagdish Devaku
 asked on

Select Rows including their SUM in the last

Hi,

I have requirement as below:

My table:

Name      Amount
Raj      100
Manj      200
Jag      300
John      400


Required Result:
Name      Amount
Raj      100
Manj      200
Jag      300
John      400
Total      1000


I tried using UNION and it working.

But I want to write this in one query. or any other better way without using UNION or temp tables.

Thanks in advance.
Microsoft SQL ServerMicrosoft SQL Server 2008Microsoft SQL Server 2005

Avatar of undefined
Last Comment
Jagdish Devaku

8/22/2022 - Mon
Habib Pourfard

As far as I know, it is not possible except for UNION, temp tables or table variable...
SOLUTION
Jim Horn

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
deighton

this may meet your needs, there are drawbacks such as you have to avoid a name being NULL

SELECT COALESCE(dq.name, 'TOTAL'), dq.total FROM (select name, sum(amount) AS total from YourTable
group by name with rollup) AS DQ
Aaron Tomosky

Also, without an order by you never know how the rows will line up. If this matters...
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SOLUTION
deighton

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Habib Pourfard

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Jim Horn

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Anthony Perkins

I tried using UNION and it working.

But I want to write this in one query.

Just a minor quible:  A SELECT statement with a UNION clause is still one query.
Jagdish Devaku

ASKER
Thanks all for the help.

I am checking the queries.
Jagdish Devaku

ASKER
Thanks for all your support.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.