SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Group by without soring


Group by without soring

Author
Message
masoudk1990
masoudk1990
Right there with Babe
Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)

Group: General Forum Members
Points: 718 Visits: 299
Hi, this is initializations:


CREATE TABLE #table1(col1 nvarchar(1), col2 int);

INSERT INTO #table1 VALUES('B','100');
INSERT INTO #table1 VALUES('A','200');
INSERT INTO #table1 VALUES('B','300');
INSERT INTO #table1 VALUES('C','400');



Here is my query:

WITH CTE (col1,col2) as (
SELECT col1, SUM(col2)
FROM #table1
GROUP BY col1
)

SELECT * FROM cte



Here is output:

col1,col2
----------------
A,200
B,400
C,400

Why GROUP BY statement sort my table automatically?
I want to display my table as it is.
Is it possible?

Thank you very much for help.

___________________________________
Computer Enterprise Masoud Keshavarz
I don't care about hell.
If I go there I've played enough Diablo to know how to fight my way out.
ManicStar
ManicStar
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7390 Visits: 4782

CREATE TABLE #table1(col1 nvarchar(1), col2 int, OrderMe int);

INSERT INTO #table1 VALUES('B','100',0);
INSERT INTO #table1 VALUES('A','200',1);
INSERT INTO #table1 VALUES('B','300',0);
INSERT INTO #table1 VALUES('C','400',1);


WITH CTE (col1,col2, OrderMe) as (
SELECT col1, SUM(col2), OrderMe
FROM #table1
GROUP BY col1, OrderMe
)

SELECT col1,Col2 FROM cte
ORDER BY OrderMe



Group by isnt' an ordering function, and if you want a specific order on your table, its best to add an ordering key of some kind.
masoudk1990
masoudk1990
Right there with Babe
Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)

Group: General Forum Members
Points: 718 Visits: 299
Thank you for reply.
My table have key, problem is if I sort my table with key I have to use it in "GROUP BY" and it generate incorrect output.

See this:

CREATE TABLE #table1(ID INT ,col1 nvarchar(1), col2 int);

INSERT INTO #table1 VALUES(1,'B','100');
INSERT INTO #table1 VALUES(2,'A','200');
INSERT INTO #table1 VALUES(3,'B','300');
INSERT INTO #table1 VALUES(4,'C','400');


WITH CTE (ID,col1,col2) as (
SELECT ID ,col1, SUM(col2)
FROM #table1
GROUP BY col1, ID
)

SELECT col1,Col2 FROM cte
ORDER BY ID



Col1,Col2
---------------
B,100
A,200
B,300
C,400

As you see KEY didn’t allowed me to group my data by Col1

___________________________________
Computer Enterprise Masoud Keshavarz
I don't care about hell.
If I go there I've played enough Diablo to know how to fight my way out.
siggemannen
siggemannen
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1277 Visits: 383
Try MIN(ID) in the inner select and it will work i think?
masoudk1990
masoudk1990
Right there with Babe
Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)

Group: General Forum Members
Points: 718 Visits: 299
siggemannen (9/14/2013)
Try MIN(ID) in the inner select and it will work i think?


haha yes :-D
Thanks it worked, much appreciated

___________________________________
Computer Enterprise Masoud Keshavarz
I don't care about hell.
If I go there I've played enough Diablo to know how to fight my way out.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search