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 |
|
Mits
Starting Member
48 Posts |
Posted - 2011-06-21 : 12:11:51
|
| HelloHere is the structure on my tables:TablesUser:--UserId int--UserName varcharNotes: --NoteId int--UserId int--CreatedDate datetimeI would like to display the results in following formatDate-----------User1----------User2-------User3------Total01/06/2011------10-------------20----------30---------6002/06/2011------20-------------20----------20---------60Follwoing query will show Note Count for each user on current date(Today) in following formatUser1 10User2 20User3 30SELECT Users.UserName, COUNT(NoteId) as [Note Count]FROM Notes INNER JOIN Users ON Notes.SalesPersonId = Users.UserIdWHERE (DATEADD(dd, 0, DATEDIFF(dd, 0, Notes.CreatedDate)) = DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())))GROUP BY Users.UserNameFollowing shows the Total count of notes on dates in following format:Day---------------Total Count01/06/2011--------6002/06/2011--------80select -- First day of month [Day] = Convert(varchar,dateadd(dd,datediff(dd,0,Createddate ),0),106), [Note Count] = count(*)from Notes where CreatedDate > = '2011-06-01 00:00:00:000'group by -- First day of month dateadd(dd,datediff(dd,0,Createddate),0)order by -- First day of month dateadd(dd,datediff(dd,0,Createddate),0)I need help to merege above 2 SQL in a stored procedure where I will pass a date and from the query should show me the results as mentioned above.ThanksMits |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-06-21 : 12:16:11
|
| select dateadd(dd,datediff(dd,0,Createddate),0) ,user1 = sum(case when u.name = 'user1' then 1 else 0 end) ,user2 = sum(case when u.name = 'user2' then 1 else 0 end) ,... fromUser ujoinNotes non u.userid = n.useridwhere CreatedDate > = '2011-06-01 00:00:00:000'group by-- First day of monthdateadd(dd,datediff(dd,0,Createddate),0)order by-- First day of monthdateadd(dd,datediff(dd,0,Createddate),0)==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Mits
Starting Member
48 Posts |
Posted - 2011-07-08 : 09:25:57
|
| Thanks Nigel, your solution works. |
 |
|
|
|
|
|
|
|