Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Results in order

Author  Topic 

confuzed04
Starting Member

39 Posts

Posted - 2007-06-13 : 11:39:33
I have the following query and I am wanting to get the results to be in order. Right now, it shows me the results by date, but the dates are out of order. How can I get it to give me the results by date in date order???

SELECT DISTINCT MDN, dateadd(day, datediff(day, 0, CallDate), 0) as CallDate,sum(PaidBalCost), sum(BonusBalCost), sum(ceiling((Cast(DurationSeconds as Decimal)/60))) as Minutes
FROM VoiceCallDetailRecord
WHERE CallDate >= '02/19/2007' and calldate < '03/19/2007'
and NOT (Left(Endpoint,3) IN ('011')
or (Left(Endpoint,4) IN ('1340','1876','1868','1809',
'1246','1242','1780','1403',
'1250','1604','1807','1519',
'1204','1506','1709','1867',
'1902','1705','1613','1416',
'1905','1902','1514','1450',
'1418','1819','1306','1867')))
AND (((CONVERT(varchar, CallDate, 108) Between '07:00:00' AND '20:59:59'))
AND DATEPART(weekday, CallDate) in (2,3,4,5,6))
Group By MDN, dateadd(day, datediff(day, 0, CallDate), 0)
UNION
SELECT DISTINCT MDN, dateadd(day, datediff(day, 0, CallDate), 0) as CallDate,sum(PaidBalCost), sum(BonusBalCost), sum(ceiling((Cast(DurationSeconds as Decimal)/60))) as Minutes
FROM ZeroChargeVCDRecord
WHERE CallDate >= '02/19/2007' and calldate < '03/19/2007'
and NOT (Left(Endpoint,3) IN ('011')
or (Left(Endpoint,4) IN ('1340','1876','1868','1809',
'1246','1242','1780','1403',
'1250','1604','1807','1519',
'1204','1506','1709','1867',
'1902','1705','1613','1416',
'1905','1902','1514','1450',
'1418','1819','1306','1867')))
AND (((CONVERT(varchar, CallDate, 108) Between '07:00:00' AND '20:59:59'))
AND DATEPART(weekday, CallDate) in (2,3,4,5,6))
Group By MDN, dateadd(day, datediff(day, 0, CallDate), 0)

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-06-13 : 11:42:18
add to the end of the query
ORDER BY CallDate



KH

Go to Top of Page

confuzed04
Starting Member

39 Posts

Posted - 2007-06-13 : 12:00:24
I did this and it won't let me run it with a UNION statement. I have to eliminate the UNION and leave a black line to get it to run and then my results aren't combined. Please advise
Go to Top of Page

sshelper
Posting Yak Master

216 Posts

Posted - 2007-06-13 : 12:03:21
You have to put the ORDER BY CallDate only after the second SELECT statement.

SQL Server Helper
http://www.sql-server-helper.com
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-06-13 : 12:03:36
only add the ORDER BY to the end

<your existing query>
ORDER BY CallDate



KH

Go to Top of Page

confuzed04
Starting Member

39 Posts

Posted - 2007-06-13 : 14:37:29
Oh! Thanks so much!!
Go to Top of Page
   

- Advertisement -