473,323 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

How to group by date irrespective of time

Hello,
I have a table which has a date column. The values in the date column are along with time
i.e., 2/1/2008 12:00:00 PM
2/1/2008 2:13:00 PM
2/3/2008 4:00:00 AM
3/1/2008 1:00:00 PM
3/1/2008 4:00:00 PM
3/3/2008 2:18:00 AM
3/3/2008 5:00:00 PM
3/3/2008 7:08:12 PM

I want to group the data based on dates irrespective of time. i,e., i want the results to be as follows


Date Count
2/1/2008 2
2/3/2008 1
3/1/2008 2
3/3/2008 3

How do i query the table using T-SQL to get the above reults..I tried using different date formats, but still i am not getting the expected results..
Mar 6 '08 #1
12 18874
amitpatel66
2,367 Expert 2GB
Hello,
I have a table which has a date column. The values in the date column are along with time
i.e., 2/1/2008 12:00:00 PM
2/1/2008 2:13:00 PM
2/3/2008 4:00:00 AM
3/1/2008 1:00:00 PM
3/1/2008 4:00:00 PM
3/3/2008 2:18:00 AM
3/3/2008 5:00:00 PM
3/3/2008 7:08:12 PM

I want to group the data based on dates irrespective of time. i,e., i want the results to be as follows


Date Count
2/1/2008 2
2/3/2008 1
3/1/2008 2
3/3/2008 3

How do i query the table using T-SQL to get the above reults..I tried using different date formats, but still i am not getting the expected results..
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT trunc(dat), COUNT(*) FROM table1 GROUP BY trunc(dat)
  3.  
  4.  
Mar 6 '08 #2
This query works in Oracle, but i want it in MS SQL.
Mar 6 '08 #3
ck9663
2,878 Expert 2GB
Hello,
I have a table which has a date column. The values in the date column are along with time
i.e., 2/1/2008 12:00:00 PM
2/1/2008 2:13:00 PM
2/3/2008 4:00:00 AM
3/1/2008 1:00:00 PM
3/1/2008 4:00:00 PM
3/3/2008 2:18:00 AM
3/3/2008 5:00:00 PM
3/3/2008 7:08:12 PM

I want to group the data based on dates irrespective of time. i,e., i want the results to be as follows


Date Count
2/1/2008 2
2/3/2008 1
3/1/2008 2
3/3/2008 3

How do i query the table using T-SQL to get the above reults..I tried using different date formats, but still i am not getting the expected results..

try:

Expand|Select|Wrap|Line Numbers
  1. select convert(varchar(10), YourDateColumn,101), count(*)
  2. from YourTable 
  3. group by convert(varchar(10), YourDateColumn,101)
  4.  
-- CK
Mar 6 '08 #4
This query is working fine.
I want to order the result based on dates..so i added
order by convert(varchar(10), Transaction_date_time,101)
at the end of the query.
When i query i get the results as follows which is not right in order
01/02/2008 1
01/18/2008 3
01/22/2008 1
04/07/2007 2
04/10/2007 1
04/13/2007 2
04/19/2007 2
05/04/2007 1
05/05/2007 3
05/08/2007 2
05/25/2007 1
05/31/2007 2
06/16/2007 1
06/19/2007 2
06/25/2007 1
06/26/2007 1
07/05/2007 1
07/12/2007 1
07/23/2007 1
07/24/2007 1
07/27/2007 2
07/31/2007 2
08/02/2007 1
08/09/2007 1
08/13/2007 1
08/20/2007 1
08/21/2007 1
08/23/2007 1
08/24/2007 2
08/30/2007 1
09/08/2007 2
09/10/2007 1
09/13/2007 1
09/18/2007 1
09/24/2007 2
09/27/2007 2
10/04/2007 1
10/05/2007 1
10/09/2007 1
10/12/2007 2
10/16/2007 1
10/25/2007 3
10/31/2007 2
11/02/2007 1
11/03/2007 1
11/05/2007 1
11/23/2007 1
11/26/2007 1
12/04/2007 1
12/18/2007 2
12/19/2007 2.
How do i solve it..Actually i want to get the latest 10 dates from the table. i.e.,
i want results as
1/22/2008 1
1/18/2008 3
1/2/2008 1
12/19/2007 2
12/18/2007 2
12/4/2007 1
11/26/2007 1
11/23/2007 1
11/5/2007 1
11/3/2007 1

So if i add Top 10, then i get
01/02/2008 1
01/18/2008 3
01/22/2008 1
04/07/2007 2
04/10/2007 1
04/13/2007 2
04/19/2007 2
05/04/2007 1
05/05/2007 3
05/08/2007 2 which is incorrect..Please help me in solving this..
Mar 6 '08 #5
ck9663
2,878 Expert 2GB
use ordinal position of the columns

Expand|Select|Wrap|Line Numbers
  1. ORDER BY 1 

where 1 means the first column. So if the date is not the first column, change it accordingly.

-- CK
Mar 6 '08 #6
use ordinal position of the columns

Expand|Select|Wrap|Line Numbers
  1. ORDER BY 1 

where 1 means the first column. So if the date is not the first column, change it accordingly.

-- CK
This is the query which i am using
select top 10 convert(varchar(10), date_time,101), count(*)
from My_Table
group by convert(varchar(10), date_time,101)
order by 1

But still i am getting the same results as shown in my previous message
Mar 6 '08 #7
ck9663
2,878 Expert 2GB
This is the query which i am using
select top 10 convert(varchar(10), date_time,101), count(*)
from My_Table
group by convert(varchar(10), date_time,101)
order by 1

But still i am getting the same results as shown in my previous message
Your result is sorted based on date. If you mean you want it descending (latest on top), add the DESC keyword.

-- CK
Mar 6 '08 #8
Your result is sorted based on date. If you mean you want it descending (latest on top), add the DESC keyword.

-- CK

If i add desc, it gives me result as follows
01/02/2008 1
01/18/2008 3
01/22/2008 1
04/07/2007 2
04/10/2007 1
04/13/2007 2
04/19/2007 2
05/04/2007 1
05/05/2007 3
05/08/2007 2

I think it is not ordering based on the date completely. It is ordering based on month irrespective of year..How do i solve it?
Mar 6 '08 #9
ck9663
2,878 Expert 2GB
Sorry, I missed the year part. Since you converted the date to varchar, it sort it like a string. Try a:

Expand|Select|Wrap|Line Numbers
  1. ORDER BY DATE_TIME
instead. I believe it will sort it even if it's not on your SELECT list.

-- CK
Mar 6 '08 #10
Sorry, I missed the year part. Since you converted the date to varchar, it sort it like a string. Try a:

Expand|Select|Wrap|Line Numbers
  1. ORDER BY DATE_TIME
instead. I believe it will sort it even if it's not on your SELECT list.

-- CK
When i changed order by Date_time, it gave me a error saying that

Column "MyTable.Date_Time" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
Mar 6 '08 #11
gpl
152 100+
The problem is that you have converted it to a string ... convert it back to a date and all will be well

convert(datetime, convert(varchar(10), date_time,101),101)

I have this handy function (which is region agnostic)

CREATE FUNCTION dbo.DateOnly(@ADate DateTime)
RETURNS[DateTime] AS
BEGIN
RETURN Convert(Datetime, datediff(d, 0, @ADate))
END


Graham
Mar 6 '08 #12
The problem is that you have converted it to a string ... convert it back to a date and all will be well

convert(datetime, convert(varchar(10), date_time,101),101)

I have this handy function (which is region agnostic)

CREATE FUNCTION dbo.DateOnly(@ADate DateTime)
RETURNS[DateTime] AS
BEGIN
RETURN Convert(Datetime, datediff(d, 0, @ADate))
END


Graham

Thanks a Lot Mr.Graham..The query is working as required after converting back to date time..Ur help was very useful.
Mar 6 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Gerry Abbott | last post by:
Hi all, Is there a convenient way to trap the date/time of the last change to the data (in the tables) in a database. (the date modified changes when the file is accessed, irrespective of...
13
by: priyasmita_guha | last post by:
Here is a program- /* PROGRAM: To find the difference between two dates */ #include<dos.h> #include<stdio.h> #include<conio.h> #include<process.h> void valid_date(int,int,int); int...
7
by: matteosartori | last post by:
Hi all, I've spent all morning trying to work this one out: I've got the following string: ...
9
by: insomniux | last post by:
Hi, I am having a problem with formatting the default value of a date field. It has been discussed earlier in many topics, but still I cannot solve the problem. What's happening: I have various...
2
by: kirke | last post by:
Hi, I have a datetime column named dtDateTime. its format is "Oct 27 2006 12:00:00 " I want to group by only date part of it and count my code is $sql1="SELECT ...
0
by: mykhan | last post by:
I have been using access database with asp.net pages. I just realized a problem with date saving in access table. I didnt give any specific format for the date when saved in tables, because I wanted...
13
by: Killer42 | last post by:
Hi all. Hopefully a simple one for any SQL guru. In an Access query, how can I group by a date field, without having my data broken down by time? In other words, I just want a count per day, not...
2
by: Stevienashaa | last post by:
Hello I'm using Access 2003, and I have a query (written in SQL) which has two parameters and asks the user for two dates. This has been working fine. Today I modified the query, removing the...
1
by: mcgr0199 | last post by:
How do I separate a date/time stamp in Access 2007? I need to be able to query for certain times of day over a period of 120 days (i.e. create a query that only gives me the data for 1pm,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.