Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hello :)

Can I group items in sql server 2008, without aggregate functions ?

some thing like this:

1 Adam USA 77854358
2 Jhone UK 85558546
3 Michal UK 85578966
4 Sara UK 81112452
5 Kone Can 55866149
6 Noura USA 79558681
7 Adam USA 77896669

to view the grid like this:

USA:
1 Adam USA 77854358
6 Noura USA 79558681
7 Adam USA 77896669

UK:
2 Jhone UK 85558546
3 Michal UK 85578966
4 Sara UK 81112452

Can:
5 Kone Can 55866149


thanks in advance :)
Posted
Comments
Herman<T>.Instance 17-Jul-12 6:19am    
order by country?
deepakaitr12345 17-Jul-12 6:48am    
Hi @Msaya

for the grouping as you mention above.
you can use with rollup and with cube option in sql server.
apart from this you can also use the grouping set functions.
Hope this points will help you

Thanks
MAU787 17-Jul-12 7:40am    
Use Order By
StianSandberg 17-Jul-12 8:37am    
Let sql do the data part of the job and do format/gui in your code. Just order by Country and iterate it in your code and check if it's a new country for each row.

1 solution

you can use ORDER BY cause like this...

SQL
DECLARE @TEMP AS TABLE  (
    ID INT IDENTITY(1,1)
    ,NAME VARCHAR(10)
    ,COUNTRY VARCHAR(10)
    ,CODE VARCHAR(15)
)

INSERT INTO @TEMP (NAME,COUNTRY,CODE) VALUES('Adam' ,'USA' ,'77854358')
INSERT INTO @TEMP (NAME,COUNTRY,CODE) VALUES('Jhone','UK','85558546')
INSERT INTO @TEMP (NAME,COUNTRY,CODE) VALUES('Michal','UK','85578966')
INSERT INTO @TEMP (NAME,COUNTRY,CODE) VALUES('Sara','UK','81112452')
INSERT INTO @TEMP (NAME,COUNTRY,CODE) VALUES('Kone','Can','55866149')
INSERT INTO @TEMP (NAME,COUNTRY,CODE) VALUES('Noura','USA','79558681')
INSERT INTO @TEMP (NAME,COUNTRY,CODE) VALUES('Adam','USA','77896669')


SELECT * FROM @TEMP ORDER BY COUNTRY DESC


and out put of this query is like this...


SQL
ID          NAME       COUNTRY    CODE
----------- ---------- ---------- ---------------
1           Adam       USA        77854358
6           Noura      USA        79558681
7           Adam       USA        77896669
2           Jhone      UK         85558546
3           Michal     UK         85578966
4           Sara       UK         81112452
5           Kone       Can        55866149
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900