-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLQuery6.sql
More file actions
28 lines (28 loc) · 808 Bytes
/
SQLQuery6.sql
File metadata and controls
28 lines (28 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
WITH SuccessfulDefenders AS (
SELECT DISTINCT UserID_Defender AS UserID
FROM Attacks
WHERE Castle_Destroyed < 30.0 AND Stars_achieved <= 1
),
WallLevels AS (
SELECT
ub.UserID,
b.Level AS WallLevel
FROM UserBuildings ub
JOIN Buildings b ON ub.BuildingID = b.BuildingID
WHERE b.Name LIKE '%Wall%'
),
WallAvg AS (
SELECT AVG(CAST(WallLevel AS FLOAT)) AS AvgWallLevel
FROM WallLevels
WHERE UserID IN (SELECT UserID FROM SuccessfulDefenders)
),
TownHallAvg AS (
SELECT AVG(CAST(TownHallLevel AS FLOAT)) AS AvgTownHallLevel
FROM Users
WHERE UserID IN (SELECT UserID FROM SuccessfulDefenders)
)
SELECT
w.AvgWallLevel,
t.AvgTownHallLevel,
ROUND(w.AvgWallLevel - t.AvgTownHallLevel, 2) AS LevelDifference
FROM WallAvg w, TownHallAvg t;