Ewald Bos
Ewald Bos

Reputation: 1770

T-SQL (Azure) count the content of a 'cell'

I have a table that is the following (it counts the seconds of a user that has opened and closed a div):

id | User | Seconds | DivId
1  | 1000 | 2       | 2
2  | 1000 | 125     | 2
3  | 1500 | 568     | 2
4  | 2000 | 3       | 2

I would like to count the total of seconds for each user in seconds, so the result should be

User | Seconds | DivId
1000 | 127     | 2
1500 | 568     | 2
2000 | 3       | 2

how would i count the content of the cell? This counts the number of entries;

SELECT        
    User, COUNT (*) AS Seconds, DivId
FROM            
    dbo.AnalyticsDivTime
GROUP BY 
    User, Seconds, DivId

Upvotes: 1

Views: 25

Answers (2)

Lukasz Szozda
Lukasz Szozda

Reputation: 175994

You could use:

SELECT [User], Sum(Seconds) as Seconds, DivId  -- SUM instead of COUNT
FROM dbo.AnalyticsDivTime
GROUP BY [User], DivId;  -- removed Seconds

Upvotes: 1

apomene
apomene

Reputation: 14389

Use SUM instead of count:

SELECT        
User, SUM(Seconds) as Seconds, DivId
FROM            
dbo.AnalyticsDivTime
GROUP BY User,  DivId

Upvotes: 1

Related Questions