Using SQL Server, I am trying to request some kind of averaged score from a table that I did not design, where basically I want a list grouped by one column to have the number of different values โโof another column that matches this criterion, as well as the number of rows, corresponding to another criterion (which I will use to create an averaged account or something else). It may not be difficult, but I have a bad day in theory theory, and any pointers would be greatly appreciated.
Here is a simplified and generalized scenario (diagram and sample data below). Say we have three columns:
objid (has a clustered index)userid (no index, I could add it)actiontype (no index, I can add it)
None of them are unique, and none of them can be null . We want to completely ignore any lines where actiontype is none . We want to know, for userid , how many actiontype = 'flag' lines are on average per object with which the user interacted.
So, if we have "ahmed", "joe" and "maria", and joe interacts with 3 objects and raises 5 flags, the number 5 / 3 = 1.6666 continuous; if "ahmed" interacted with 3 objects and did not raise flags, its number would be 0 ; if โmariaโ interacted with 5 objects and raised 4 flags, its number would be 4 / 5 = 0.8 :
+ -------- + ------------------ +
| userid | flags_per_object |
+ -------- + ------------------ +
| ahmed | 0 |
| joe | 1.66666667 |
| maria | 0.8 |
+ -------- + ------------------ +
I will not be remotely surprised if it is closed as a duplicate, I simply cannot find it.
Here's a simplified table setup and data retrieval:
create table tmp ( objid varchar(254) not null, userid varchar(254) not null, actiontype varchar(254) not null ) create clustered index tmp_objid on tmp(objid) insert into tmp (objid, userid, actiontype) values ('alpha', 'joe', 'none') insert into tmp (objid, userid, actiontype) values ('alpha', 'joe', 'none') insert into tmp (objid, userid, actiontype) values ('alpha', 'joe', 'update') insert into tmp (objid, userid, actiontype) values ('alpha', 'joe', 'close') insert into tmp (objid, userid, actiontype) values ('alpha', 'joe', 'flag') insert into tmp (objid, userid, actiontype) values ('alpha', 'joe', 'flag') insert into tmp (objid, userid, actiontype) values ('alpha', 'joe', 'flag') insert into tmp (objid, userid, actiontype) values ('alpha', 'joe', 'flag') insert into tmp (objid, userid, actiontype) values ('beta', 'joe', 'none') insert into tmp (objid, userid, actiontype) values ('beta', 'joe', 'none') insert into tmp (objid, userid, actiontype) values ('beta', 'joe', 'close') insert into tmp (objid, userid, actiontype) values ('beta', 'joe', 'flag') insert into tmp (objid, userid, actiontype) values ('gamma', 'joe', 'none') insert into tmp (objid, userid, actiontype) values ('delta', 'joe', 'update') insert into tmp (objid, userid, actiontype) values ('alpha', 'maria', 'update') insert into tmp (objid, userid, actiontype) values ('beta', 'maria', 'flag') insert into tmp (objid, userid, actiontype) values ('beta', 'maria', 'flag') insert into tmp (objid, userid, actiontype) values ('gamma', 'maria', 'flag') insert into tmp (objid, userid, actiontype) values ('gamma', 'maria', 'flag') insert into tmp (objid, userid, actiontype) values ('gamma', 'maria', 'update') insert into tmp (objid, userid, actiontype) values ('gamma', 'maria', 'close') insert into tmp (objid, userid, actiontype) values ('delta', 'maria', 'update') insert into tmp (objid, userid, actiontype) values ('epsilon', 'maria', 'update') insert into tmp (objid, userid, actiontype) values ('alpha', 'ahmed', 'none') insert into tmp (objid, userid, actiontype) values ('beta', 'ahmed', 'none') insert into tmp (objid, userid, actiontype) values ('gamma', 'ahmed', 'none') insert into tmp (objid, userid, actiontype) values ('gamma', 'ahmed', 'update') insert into tmp (objid, userid, actiontype) values ('delta', 'ahmed', 'update') insert into tmp (objid, userid, actiontype) values ('delta', 'ahmed', 'close') insert into tmp (objid, userid, actiontype) values ('epsilon', 'ahmed', 'update') insert into tmp (objid, userid, actiontype) values ('epsilon', 'ahmed', 'close')