SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Date grouping issue - Sql Server 2005
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dirtyeggs
Starting Member

USA
4 Posts

Posted - 12/03/2012 :  17:03:52  Show Profile  Reply with Quote
Hello all - I am fairly new to sql and I am having the following issue.

I need to group some records based on date and I am trying to ignore the milliseconds - below is my query however, I see some results with the same timestamp but they are not grouped and I do not know why. I expected those two HAL records to be grouped. Any help would be appreciated. This is on SQL SERVER 2005.

SELECT count(*),ticker,CONVERT(varchar, CONVERT(VARCHAR(MAX), last_upd_date, 120), 120) from crx_order
group by last_upd_date,ticker
order by last_upd_date desc

results example
----------------
2 MSFT 2012-12-03 16:00:05
1 HAL 2012-12-03 14:59:52
1 HAL 2012-12-03 14:59:52

robvolk
Most Valuable Yak

USA
15679 Posts

Posted - 12/03/2012 :  17:14:50  Show Profile  Visit robvolk's Homepage  Reply with Quote
Because you're grouping by the actual value, not the truncated value. Try this:
SELECT count(*),ticker,DATEADD(ms, -DATEPART(ms,last_upd_date), last_upd_date) from crx_order
group by DATEADD(ms, -DATEPART(ms,last_upd_date), last_upd_date),ticker
order by DATEADD(ms, -DATEPART(ms,last_upd_date), last_upd_date) desc
Go to Top of Page

dirtyeggs
Starting Member

USA
4 Posts

Posted - 12/04/2012 :  09:27:17  Show Profile  Reply with Quote
I thought that might be the issue .... thanks it works great.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000