Solved

How to group by date?

Posted on 2011-09-21
6
166 Views
Last Modified: 2012-06-27
In the following, how do I get a count for each unique day entry on a ticketid for an employee?

In the example below, empid=1 and ticketid=7 would have a count of 3, since it was worked on three different days.  However, empid=1 and ticketid=9 is just one day, since both entries were on 9/17. empid=3 and ticketid=9 is also one day since both entries were on 9/17 as well.
Id   empid   ticketid   datecomplete              ticketsession
4      1         9      2011-09-17 9:12:24.000        3
4      1         9      2011-09-17 9:12:24.000        3
10     1         7      2011-09-15 9:12:24.000        7
19     1         7      2011-09-16 9:12:24.000        7
20     1         7      2011-09-17 11:17:24.000        7
21     3         9      2011-09-17 9:12:24.000        3
21     3         9      2011-09-17 10:12:24.000        3

Open in new window

0
Comment
Question by:brettr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 36576102
try
select empid, CONVERT(varchar(10), datecomplete, 101) datecomplete
from table1
group by empid, CONVERT(varchar(10), datecomplete, 101)

Open in new window

0
 

Author Comment

by:brettr
ID: 36576173
Sorry - accepted too soon.  What I want is the count of 3 for empid=1 and ticketid=7.  The above query keep them on separate rows and doesn't provide a count.
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 36576201
Add the counts to your select query


select empid, count(empid) [Employees], count(tickectid) [tickectid], CONVERT(varchar(10), datecomplete, 101) datecomplete
from table1
group by empid, tickectid, CONVERT(varchar(10), datecomplete, 101)

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:brettr
ID: 36576254
That just counts each single row.  So instead of getting 3 for 9/15, 9/16 and 9/17, which were all on the same ticket, you can 1 for each.
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 36576440
Right, you only need one count but group by the two fields

select empid, count(tickectid) [Tickect Count], CONVERT(varchar(10), datecomplete, 101) datecomplete
from table1
group by empid, tickectid, CONVERT(varchar(10), datecomplete, 101)

Open in new window

0
 

Author Comment

by:brettr
ID: 36576457
ok, thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to use TOP 1 in a T-SQL sub-query? 14 49
SQL Error - Query 6 50
How to resolve SQL Server DB deadlock which makes my application hangs ? 6 63
SQL- GROUP BY 4 26
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question