I have a SQL Server database with the following structure
CREATE TABLE users (
userid INT IDENTITY PRIMARY KEY CLUSTERED,
age INT
)
GO
CREATE TABLE userstatus(
userstatusID INT IDENTITY PRIMARY KEY CLUSTERED,
statusid INT,
userid INT FOREIGN KEY REFERENCES dbo.users(userid)
)
GO
Some sample data:
INSERT INTO dbo.users
( age )
VALUES ( 15 -- age - int
),
( 20 ),
( 25 );
INSERT INTO dbo.userstatus
( statusid, userid )
VALUES ( 1, 1 ),
( 1, 2 ),
( 1, 3 ),
( 2, 1 ),
( 3, 1 ),
( 2, 2 );
Now I want to get the sum of user ages, and the sum of user ages that has status=2.
Something like:
Age with status = 2: 35 Total Ages: 60