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
 GROUP BY in one row
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

romeck
Starting Member

16 Posts

Posted - 07/29/2013 :  01:05:30  Show Profile  Reply with Quote
I have table:

Name type nr
adam pupil 1
adam pupil 2
martha pupil 1
martha pupil 1
martha pupil 2
martha pupil 2
martha pupil 2
martha pupil 2
how can i make a query with gropuing to have :
name type nr nrcount nr nr count
adam pupil 1 1 2 1
martha pupil 1 2 2 4

So basicaly each group should be in the same row not like in group statemant that all diffrtent groups in new line. i can use functions but stil it will be probably consume more time to execute.

THQ

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 07/29/2013 :  01:59:31  Show Profile  Reply with Quote
will the values be always 1 and 2?

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

romeck
Starting Member

16 Posts

Posted - 07/29/2013 :  02:16:36  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

will the values be always 1 and 2?

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




1. yes there will be always just two values in that column (there will be number of two months that user want to check)
2. no - what if there are many unspecified values ?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 07/29/2013 :  02:38:47  Show Profile  Reply with Quote

declare @t table
(
Name varchar(50),
[type] varchar(50),
 nr int
 )
 insert @t
 values ('adam', 'pupil', 1),
('adam', 'pupil', 2),
('martha', 'pupil', 1),
('martha', 'pupil', 1),
('martha', 'pupil', 2),
('martha', 'pupil', 2),
('martha', 'pupil', 2),
('martha', 'pupil', 2)


select Name,[type],
MAX(case when nr=1 THEN nr END) AS [1],
COUNT(case when nr=1 THEN nr END) AS [1],
MAX(case when nr=2 THEN nr END) AS [2],
COUNT(case when nr=2 THEN nr END) AS [1]
from @t 
group by Name,[type]



output
----------------------------------------------
Name	type	1	1	2	1
----------------------------------------------
adam	pupil	1	1	2	1
martha	pupil	1	2	2	4



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
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.08 seconds. Powered By: Snitz Forums 2000