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.
| Author |
Topic |
|
mrsaif
Starting Member
43 Posts |
Posted - 2008-04-09 : 06:38:00
|
| I have a table hits with following fields with sample DataCode: CreatedOn:4 2008-04-01 14:48:15.3274 2008-04-01 13:53:41.9234 2008-04-02 15:16:25.3275 2008-04-02 16:31:07.3835 2008-04-02 16:25:07.3835 2008-04-02 16:28:07.3835 2008-04-03 16:50:34.2174 2008-04-03 13:23:30.7673 2008-04-04 13:41:41.6275 2008-04-04 13:55:00.497__________________________________________________________i want to create a query to get count of the code per day.__________________________________________________________Code: Date: Count:3 2008-04-04 14 2008-04-01 24 2008-04-02 14 2008-04-03 15 2008-04-02 35 2008-04-03 1------------------------------------------------------------How can i do this to count code per day in a single query?I am using sql server 2005.==============================================================Muhammad Saifullah |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2008-04-09 : 06:44:48
|
| [code]Select Dateadd(day, Datediff(day, 0, CreatedOn), 0) as CreatedOn, Count(Code) as [Count]from tablegroup by Dateadd(day, Datediff(day, 0, CreatedOn), 0)order by 1[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-09 : 06:47:14
|
[code]DECLARE @Sample TABLE (Code TINYINT, CreatedOn DATETIME)INSERT @SampleSELECT 4, '2008-04-01 14:48:15.327' UNION ALLSELECT 4, '2008-04-01 13:53:41.923' UNION ALLSELECT 4, '2008-04-02 15:16:25.327' UNION ALLSELECT 5, '2008-04-02 16:31:07.383' UNION ALLSELECT 5, '2008-04-02 16:25:07.383' UNION ALLSELECT 5, '2008-04-02 16:28:07.383' UNION ALLSELECT 5, '2008-04-03 16:50:34.217' UNION ALLSELECT 4, '2008-04-03 13:23:30.767' UNION ALLSELECT 3, '2008-04-04 13:41:41.627' UNION ALLSELECT 5, '2008-04-04 13:55:00.497'SELECT Code, DATEADD(DAY, DATEDIFF(DAY, '19000101', CreatedOn), '19000101') AS Date, COUNT(*) AS [Count]FROM @SampleGROUP BY Code, DATEDIFF(DAY, '19000101', CreatedOn)ORDER BY Code, DATEDIFF(DAY, '19000101', CreatedOn[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
PeterNeo
Constraint Violating Yak Guru
357 Posts |
Posted - 2008-04-09 : 06:48:44
|
| TRY THIS SELECT Code, DATEADD(DAY, DATEDIFF(DAY, 0, CreatedeOn), 0), COUNT(DATEADD(DAY, DATEDIFF(DAY, 0, CreatedeOn), 0)) 'Cnt'FROM (SELECT 4 Code, '2008-04-01 14:48:15.327' CreatedeOn UNION ALLSELECT 4, '2008-04-01 13:53:41.923' UNION ALLSELECT 4, '2008-04-02 15:16:25.327' UNION ALLSELECT 5, '2008-04-02 16:31:07.383' UNION ALLSELECT 5, '2008-04-02 16:25:07.383' UNION ALLSELECT 5, '2008-04-02 16:28:07.383' UNION ALLSELECT 5, '2008-04-03 16:50:34.217' UNION ALLSELECT 4, '2008-04-03 13:23:30.767' UNION ALLSELECT 3, '2008-04-04 13:41:41.627' UNION ALLSELECT 5, '2008-04-04 13:55:00.497') AGROUP BY Code, DATEADD(DAY, DATEDIFF(DAY, 0, CreatedeOn), 0) |
 |
|
|
mrsaif
Starting Member
43 Posts |
Posted - 2008-04-09 : 07:29:53
|
| Thank you Harsh Athalye,your reply solved my problem :)Muhammad Saifullah |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-09 : 07:35:33
|
Really?Where is the Code column? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
somalia
Starting Member
10 Posts |
Posted - 2009-09-29 : 06:49:54
|
| i have a similar problem.there is table where datas are getting saved as agentname saledate appno===============================ph0003 09/28/2009 A0010ph0003 09/28/2009 A0011ph0004 09/28/2009 A0012ph0004 09/28/2009 A0013ph0005 09/28/2009 A0014ph0005 09/28/2009 A0015ph0003 09/27/2009 A0002ph0003 09/27/2009 A0003ph0004 09/27/2009 A0004ph0004 09/27/2009 A0005ph0005 09/27/2009 A0006ph0005 09/27/2009 A0007the output should beAgentname 09/27/2009 09/28/2009 Total============================================ph0003 2 2 4ph0004 2 2 4Can anybody tell me how to do this?????somalia |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-09-29 : 06:54:27
|
quote: Originally posted by somalia i have a similar problem.there is table where datas are getting saved as agentname saledate appno===============================ph0003 09/28/2009 A0010ph0003 09/28/2009 A0011ph0004 09/28/2009 A0012ph0004 09/28/2009 A0013ph0005 09/28/2009 A0014ph0005 09/28/2009 A0015ph0003 09/27/2009 A0002ph0003 09/27/2009 A0003ph0004 09/27/2009 A0004ph0004 09/27/2009 A0005ph0005 09/27/2009 A0006ph0005 09/27/2009 A0007the output should beAgentname 09/27/2009 09/28/2009 Total============================================ph0003 2 2 4ph0004 2 2 4Can anybody tell me how to do this?????somalia
Answered herehttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=133596MadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-29 : 06:58:40
|
dont hijack threads. post as a new question to get quick solutions in future. you can do like this to get o/pSELECT Agentname,COUNT(CASE WHEN saledate='09/27/2009' THEN appno ELSE NULL END) AS [09/27/2009],COUNT(CASE WHEN saledate='09/28/2009' THEN appno ELSE NULL END) AS [09/28/2009],COUNT(appno) AS Total FROM tableGROUP BY Agentname |
 |
|
|
|
|
|
|
|