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


Result set to be modified


Result set to be modified

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

Group: General Forum Members
Points: 51 Visits: 15
I have query as follows:
select right(convert(varchar, CCD.startDateTime , 106), 8) as startmonth,CCD.contactDisposition, count(*) as TotalCalls
from ContactCallDetail CCD,
ContactQueueDetail CQD
where
CCD.sessionID=CQD.sessionID
and CCD.sessionSeqNum=CQD.sessionSeqNum
and CCD.profileID=CQD.profileID
and CCD.nodeID=CQD.nodeID
and 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 <='2011-12-31 00:59:59.000'
group by right(convert(varchar, startDateTime , 106), 8),contactDisposition
order by 1

The result is as follows when the above query is executed:
Start month Contactdisposition Total calls
Jan-2011 1 500
Jan -2011 2 4000
Feb-2011 1 400
Feb-2011 2 5000

I would like to have the result set only as follows:
Start Month 1 2
Jan-2011 500 4000
Feb -2011 400 5000

Can I get help please to display the data as above.
Mark Cowne
Mark Cowne
SSCertifiable
SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)

Group: General Forum Members
Points: 6357 Visits: 25530
Without DDL, sample data and expected results this is just a guess....


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,
ContactQueueDetail CQD
where
CCD.sessionID=CQD.sessionID
and CCD.sessionSeqNum=CQD.sessionSeqNum
and CCD.profileID=CQD.profileID
and CCD.nodeID=CQD.nodeID
and 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 <='2011-12-31 00:59:59.000'
group by right(convert(varchar, startDateTime , 106), 8)
order by 1



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




Kingston Dhasian
Kingston Dhasian
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 5974 Visits: 5280
You can use CROSS TABS to achieve the results

SELECT   RIGHT(CONVERT(VARCHAR, CCD.startDateTime , 106), 8) AS startmonth,
SUM( CASE WHEN CCD.contactDisposition = 1 THEN 1 ELSE 0 END ) AS 1
SUM( CASE WHEN CCD.contactDisposition = 2 THEN 1 ELSE 0 END ) AS 2
FROM ContactCallDetail CCD,
ContactQueueDetail CQD
where
CCD.sessionID=CQD.sessionID
and CCD.sessionSeqNum=CQD.sessionSeqNum
and CCD.profileID=CQD.profileID
and CCD.nodeID=CQD.nodeID
and 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 <='2011-12-31 00:59:59.000'
GROUP BY RIGHT(CONVERT(VARCHAR, startDateTime , 106), 8)
ORDER BY 1



The below mentioned articles will give you more idea on CROSS TABS and PIVOTS
http://www.sqlservercentral.com/articles/T-SQL/63681/
http://www.sqlservercentral.com/articles/Crosstab/65048/


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Sean Lange
Sean Lange
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61807 Visits: 17954
Mark's excellent query looks like it should return the data you are looking for.

However if I could make a couple of suggestions. You are using the old style joins. You should consider changing this to the ANSI-92 style joins. Also you should avoid using ordinal positions for ordering. It is more difficult to read and if your query changes you run the risk of messing up your order by. You can use the column alias in the order by. The last change I would recommend is in your date checks. You have the possibility of letting some rows from the last few milliseconds. I would instead check for less than the next day.


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



_______________________________________________________________

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 (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)

Group: General Forum Members
Points: 51 Visits: 15
Thank you all very much for instant response. It works fine
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