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 |
|
viallos
Starting Member
3 Posts |
Posted - 2011-10-19 : 19:05:03
|
Folks, I'm looking for help with Count queryI have 2 tablesTable1 - 3 columns (Staff)SID | FName | SName11 | Joe1 | Blog122 | Joe2 | Blog233 | Joe3 | Blog3Table2 - 4 columns (Transactions)TID | Tool | SHire | SReturn1 | Tool1 | 11 | 332 | Tool2 | 22 | 113 | Tool2 | 11 | 224 | Tool1 | 33 | 22Table1 contains customer service staff who either rent or take back tools that are for rent and transaction is recorded in Table2.Table2 contain transactions - what was rented and who from staff rented as well who took it back.I would like to count number of contacts made by each staff with customer (rent or take back) within single query.Expected result for above would be SID | Contacts11 | 3 22 | 333 | 2Next step would be to add FName and SName into results.So far I have following select SHire , count(SHire)from Table2group by SHire union allselect THire , count(THire)from Table2group by SHire order by 1 which gives me results separeted for each staff by action SID | Contacts11 | 2 11 | 1 22 | 122 | 233 | 133 | 1Any ideas?Thanks |
|
|
sqlmaster555112
Starting Member
13 Posts |
Posted - 2011-10-19 : 23:35:53
|
| Try to JOIN the two instances Table1 to Table2, joining on SHire SReturn, then group all the columns in table Table1 and run a count()@sqlmasterhttp://www.intelcs.com/SQL_Server_Consultant.aspx |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-20 : 00:21:49
|
| [code]select Val,COUNT(*) AS Contactsfrom Table2UNPIVOT(Val FOR Category IN ([SHire],[SReturn]))uGROUP BY Val,Category[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|