Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Order by date not by alphabatical order but by usual jan - Feb etc required Expand / Collapse
Author
Message
Posted Tuesday, May 28, 2013 9:08 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, May 28, 2013 4:52 PM
Points: 9, 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.
Post #1457379
Posted Tuesday, May 28, 2013 9:14 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:16 AM
Points: 13,110, Visits: 11,939
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 Moden's 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)
Post #1457386
Posted Tuesday, May 28, 2013 9:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, May 28, 2013 4:52 PM
Points: 9, Visits: 15
If I use like above suggestion then I will not get the sum which is grouped by the month
Post #1457391
Posted Tuesday, May 28, 2013 9:36 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:16 AM
Points: 13,110, Visits: 11,939
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 Moden's 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)
Post #1457408
Posted Tuesday, May 28, 2013 9:44 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 8:53 AM
Points: 23,033, Visits: 31,556
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.



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)
Post #1457411
Posted Tuesday, May 28, 2013 9:51 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, May 28, 2013 4:52 PM
Points: 9, 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
Post #1457416
Posted Tuesday, May 28, 2013 9:53 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:16 AM
Points: 13,110, Visits: 11,939
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 Moden's 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)
Post #1457417
Posted Tuesday, May 28, 2013 10:13 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 8:53 AM
Points: 23,033, Visits: 31,556
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





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)
Post #1457430
Posted Tuesday, May 28, 2013 10:20 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, May 28, 2013 4:52 PM
Points: 9, Visits: 15
I will try this way and see.
Post #1457434
Posted Tuesday, May 28, 2013 11:53 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 9:05 AM
Points: 3,357, Visits: 7,258
Instead of
MONTH( startdatetime)

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




Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1457466
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse