SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Add rows in same row
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Pirre0001
Starting Member

19 Posts

Posted - 02/27/2014 :  11:09:07  Show Profile  Reply with Quote
The rows look like this in the table:

Rownr---Batch---Quant
1-------E34-----3
2-------E36-----2
3-------E44-----6
4-------E34-----4
5-------E37-----12
6-------E36-----1
7-------E36-----5

I want it show in same row like this:

All_Batch
E34: 7
E36: 8
E44: 6
E37: 12

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/27/2014 :  12:15:09  Show Profile  Reply with Quote

SELECT Batch,SUM(Quant) AS Total
FROM table
GROUP BY Batch


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Pirre0001
Starting Member

19 Posts

Posted - 02/27/2014 :  15:16:51  Show Profile  Reply with Quote
This will not show it on same row. This will be on 4 rows.
My problem is that I want it on one row.

quote:
Originally posted by visakh16


SELECT Batch,SUM(Quant) AS Total
FROM table
GROUP BY Batch


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs



Edited by - Pirre0001 on 02/27/2014 15:17:31
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 02/27/2014 :  15:23:59  Show Profile  Visit SwePeso's Homepage  Reply with Quote
DECLARE	@Sample TABLE
	(
		RowNr INT,
		Batch CHAR(3),
		Quant INT
	);

INSERT	@Sample
	(
		RowNr,
		Batch,
		Quant
	)
VALUES	(1, 'E34',  3),
	(2, 'E36',  2),
	(3, 'E44',  6),
	(4, 'E34',  4),
	(5, 'E37', 12),
	(6, 'E36',  1),
	(7, 'E36',  5);

DECLARE	@Result VARCHAR(MAX);

SELECT	@Result = CAST(Data AS VARCHAR(MAX))
FROM	(
		SELECT		Batch + ': ' + CAST(SUM(Quant) AS VARCHAR(12)) + CHAR(10)
		FROM		@Sample
		GROUP BY	Batch
		FOR XML		PATH('')
	) AS d(Data);

PRINT	'---------------'
PRINT	@Result;



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000