Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Grouping problem

Author  Topic 

rauk10
Starting Member

16 Posts

Posted - 2008-08-10 : 12:43:48
I have one problem regarding grouping.

I have a table like this

ID NAME LOCATION
1 Abc TX
2 cde TX
3 Fgh TX
4 Ijk NY
5 Mno NY
6 Def NY
7 ghi CA

I want to group them by the Location as


NAME LOCATION
Abc TX
cde TX
Fgh TX


NAME LOCATION
Ijk NY
Mno NY
Def NY


NAME LOCATION
ghi CA


I would really appreciate your help.

Thank You.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-10 : 13:17:54
you want to break them to show in three resultsets?
Go to Top of Page

rauk10
Starting Member

16 Posts

Posted - 2008-08-10 : 13:21:52
quote:
Originally posted by visakh16

you want to break them to show in three resultsets?



Yes I want to break them. I want them to be group by the location. So here I want to break it in three recordsets

I would really appreciate that.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-10 : 13:28:24
You'll need to run three different queries then. You can wrap it into a stored procedure though, so you only need to run one thing.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-10 : 13:29:35
quote:
Originally posted by rauk10

quote:
Originally posted by visakh16

you want to break them to show in three resultsets?



Yes I want to break them. I want them to be group by the location. So here I want to break it in three recordsets

I would really appreciate that.


cant you make a procedure with a parameter for location and execute it with a location value to return only values belonging to location?
Go to Top of Page

rauk10
Starting Member

16 Posts

Posted - 2008-08-10 : 13:40:25
quote:
Originally posted by visakh16

quote:
Originally posted by rauk10

quote:
Originally posted by visakh16

you want to break them to show in three resultsets?



Yes I want to break them. I want them to be group by the location. So here I want to break it in three recordsets

I would really appreciate that.


cant you make a procedure with a parameter for location and execute it with a location value to return only values belonging to location?


can you tell me how can I do that
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-10 : 13:46:27
[code]CREATE PROC GetLocationData
@Location varchar(100)
AS
SELECT NAME,LOCATION
FROM YourTable
WHERE LOCATION=@Location
ORDER BY ID
GO[/code]

execute it like this

[code]EXEC GetLocationData 'TX'--1st resultset
EXEC GetLocationData 'NY'--2nd resultset
EXEC GetLocationData 'CA'--3rd resultset[/code]
Go to Top of Page

rauk10
Starting Member

16 Posts

Posted - 2008-08-10 : 13:58:15
quote:
Originally posted by visakh16

CREATE PROC GetLocationData
@Location varchar(100)
AS
SELECT NAME,LOCATION
FROM YourTable
WHERE LOCATION=@Location
ORDER BY ID
GO


execute it like this

EXEC GetLocationData 'TX'--1st resultset
EXEC GetLocationData 'NY'--2nd resultset
EXEC GetLocationData 'CA'--3rd resultset




Thank You very much. It really helped.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-10 : 14:03:03
quote:
Originally posted by rauk10

quote:
Originally posted by visakh16

CREATE PROC GetLocationData
@Location varchar(100)
AS
SELECT NAME,LOCATION
FROM YourTable
WHERE LOCATION=@Location
ORDER BY ID
GO


execute it like this

EXEC GetLocationData 'TX'--1st resultset
EXEC GetLocationData 'NY'--2nd resultset
EXEC GetLocationData 'CA'--3rd resultset




Thank You very much. It really helped.


You're welcome . glad that i could help you out. Suggest you to learn about procedures by refering to books online

http://msdn.microsoft.com/en-us/library/ms187926.aspx
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-11 : 03:27:33
quote:
Originally posted by rauk10

I have one problem regarding grouping.

I have a table like this

ID NAME LOCATION
1 Abc TX
2 cde TX
3 Fgh TX
4 Ijk NY
5 Mno NY
6 Def NY
7 ghi CA

I want to group them by the Location as


NAME LOCATION
Abc TX
cde TX
Fgh TX


NAME LOCATION
Ijk NY
Mno NY
Def NY


NAME LOCATION
ghi CA


I would really appreciate your help.

Thank You.



Where do you want to show data?
If you use Reports, group it by Location

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -