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 |
bhavtosh
Starting Member
4 Posts |
Posted - 2012-03-02 : 06:35:33
|
Hi,i have below tables with below fields:TableWOCreatedBY UpdatedBYuser1 user2user1 user1user2 user3TableDataUpdatedBy DataType[if =1 means Event else Data]user1 1user3 2user3 2user2 1user1 1TableUserUserID FisrtnameLastnameuser1 Bill Lutheruser2 Charlie Martinuser3 Dean JohnNow i wish to have a select query that should display the below columns:UserID - from TableUserFullname - from TableUserCount of CreatedBy - from TableWOCount of UpdatedBy - from TableWOCount of EventUpdated - - from TableDataCount of DataUpdated - - from TableDataany help is really appreciatedthanks,bhavtosh |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-02 : 12:39:37
|
[code]SELECT u.UserID,u.Fisrtname + ' ' + u.Lastname AS FullName,SUM(w.createdCount) AS createdCount,SUM(w.updatedCount) AS updatedCount,e.EventCnt,e.dataCntFROM TableUser uINNER JOIN (SELECT CreatedBY AS UserID,COUNT(*) AS createdCount,CAST(0 AS int) AS updatedCount FROM TableWO UNION ALL SELECT UpdatedBy AS UserID,0 ,COUNT(*) FROM TableWO )wON w.UserID = u.UserIDINNER JOIN (SELECT UpdatedBy, COUNT(CASE WHEN DatatYpe=1 THEN 1 ELSE NULL END) AS EventCnt, COUNT(CASE WHEN DatatYpe=2 THEN 1 ELSE NULL END) AS dataCnt, FROM tableData GROUP BY UpdatedBy )eON e.UpdatedBy = u.UserIDGROUP BY u.UserID,u.Fisrtname + ' ' + u.Lastname,e.EventCnt,e.dataCnt[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
bhavtosh
Starting Member
4 Posts |
Posted - 2012-03-04 : 01:47:50
|
thanks buddy :)thanks,bhavtosh |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-04 : 14:28:01
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|