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


Order by date not by alphabatical order but by usual jan - Feb etc required


Order by date not by alphabatical order but by usual jan - Feb etc required

Author
Message
keshava.murthy
keshava.murthy
Valued Member
Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)

Group: General Forum Members
Points: 55 Visits: 15
In the query below:
select right(convert(varchar, CCD.startDateTime , 106), 8) as startmonth,
sum(case when CCD.contactDisposition = 1 then 1 else 0 end) as TotalCalls1,
sum(case when CCD.contactDisposition = 2 then 1 else 0 end) as TotalCalls2
from ContactCallDetail CCD
inner join ContactQueueDetail CQD on CCD.sessionID = CQD.sessionID
and CCD.sessionSeqNum = CQD.sessionSeqNum
and CCD.profileID = CQD.profileID
and CCD.nodeID = CQD.nodeID
where
CCD.applicationName = 'FLVS'
and CCD.contactType = 1
and contactDisposition in (1, 2)
and CCD.startDateTime >= '2011-01-01 00:00:00.000'
and CCD.startDateTime < '2012-01-01 00:00:00.000'
group by right(convert(varchar, startDateTime , 106), 8)
order by startmonth


The order by clause is doing its job alphabatical ordering. However, I would like to go in order of months such as Jan, Feb etc.

Can I please get help on this.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65230 Visits: 17979
keshava.murthy (5/28/2013)
In the query below:
select right(convert(varchar, CCD.startDateTime , 106), 8) as startmonth,
sum(case when CCD.contactDisposition = 1 then 1 else 0 end) as TotalCalls1,
sum(case when CCD.contactDisposition = 2 then 1 else 0 end) as TotalCalls2
from ContactCallDetail CCD
inner join ContactQueueDetail CQD on CCD.sessionID = CQD.sessionID
and CCD.sessionSeqNum = CQD.sessionSeqNum
and CCD.profileID = CQD.profileID
and CCD.nodeID = CQD.nodeID
where
CCD.applicationName = 'FLVS'
and CCD.contactType = 1
and contactDisposition in (1, 2)
and CCD.startDateTime >= '2011-01-01 00:00:00.000'
and CCD.startDateTime < '2012-01-01 00:00:00.000'
group by right(convert(varchar, startDateTime , 106), 8)
order by startmonth


The order by clause is doing its job alphabatical ordering. However, I would like to go in order of months such as Jan, Feb etc.

Can I please get help on this.


Just need to add an extra column in your group by so you can order it.


select right(convert(varchar, CCD.startDateTime , 106), 8) as startmonth,
sum(case when CCD.contactDisposition = 1 then 1 else 0 end) as TotalCalls1,
sum(case when CCD.contactDisposition = 2 then 1 else 0 end) as TotalCalls2
from ContactCallDetail CCD
inner join ContactQueueDetail CQD on CCD.sessionID = CQD.sessionID
and CCD.sessionSeqNum = CQD.sessionSeqNum
and CCD.profileID = CQD.profileID
and CCD.nodeID = CQD.nodeID
where
CCD.applicationName = 'FLVS'
and CCD.contactType = 1
and contactDisposition in (1, 2)
and CCD.startDateTime >= '2011-01-01 00:00:00.000'
and CCD.startDateTime < '2012-01-01 00:00:00.000'
group by right(convert(varchar, startDateTime , 106), 8), month(startDateTime)
order by month(startDateTime)



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
keshava.murthy
keshava.murthy
Valued Member
Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)

Group: General Forum Members
Points: 55 Visits: 15
If I use like above suggestion then I will not get the sum which is grouped by the month
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65230 Visits: 17979
keshava.murthy (5/28/2013)
If I use like above suggestion then I will not get the sum which is grouped by the month


Did you actually try it? It is grouped by MONTH(starttime). Not sure how that won't be grouped by the month.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)

Group: General Forum Members
Points: 98702 Visits: 38996
keshava.murthy (5/28/2013)
If I use like above suggestion then I will not get the sum which is grouped by the month


Actually, it will. Give it a try.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
keshava.murthy
keshava.murthy
Valued Member
Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)

Group: General Forum Members
Points: 55 Visits: 15
If I try order by month( startdatetime) then it sorts the data by month.
The problem here is when the data spans to multiple years then it will sort as follows:
Jan 2011 450 4000
Jan 2012 200 3651
Feb 2011 300 6354
Feb 2012 400 2654

I want result set instead as follows:
Jan 2011 450 4000
Feb 2011 300 6354

till Dec 2011 and then
Jan 2012 200 3651
Feb 2012 400 2654

and so on
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65230 Visits: 17979
keshava.murthy (5/28/2013)
If I try order by month( startdatetime) then it sorts the data by month.
The problem here is when the data spans to multiple years then it will sort as follows:
Jan 2011 450 4000
Jan 2012 200 3651
Feb 2011 300 6354
Feb 2012 400 2654

I want result set instead as follows:
Jan 2011 450 4000
Feb 2011 300 6354

till Dec 2011 and then
Jan 2012 200 3651
Feb 2012 400 2654

and so on


Your query had a where clause that would prevent data from any year other than 2011.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)

Group: General Forum Members
Points: 98702 Visits: 38996
Perhaps the following will give you another idea:



create table dbo.TestSort(
tsid int identity(1,1),
datecol datetime
);
go

/*
Template: Dynamic Tally CTE - 2008
Author: Lynn A. Pettis
Date: 2013-03-12
Site: ISS, Inc -- Colorado Springs, CO

This template is the start of a dynamic Tally table for SQL Server 2008 and later

*/
with
e1(n) as (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)), -- 10 rows
e2(n) as (select 1 from e1 a cross join e1 b), -- 100 rows
eTally(n) as (select row_number() over (order by (select null)) from e2 a cross join e2 b) -- 10,000 rows
insert into dbo.TestSort(datecol)
select
dateadd(day, n-1, getdate())
from
eTally;
go

select
right(convert(varchar, datecol, 106), 8) as startmonth,
count(*)
from
dbo.TestSort
group by
right(convert(varchar, datecol, 106), 8), year(datecol), month(datecol)
order by
year(datecol), month(datecol);
go

drop table dbo.TestSort;
go




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
keshava.murthy
keshava.murthy
Valued Member
Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)

Group: General Forum Members
Points: 55 Visits: 15
I will try this way and see.
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43444 Visits: 19859
Instead of
MONTH( startdatetime)

use
convert(char(6), startdatetime, 112)




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
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