Here the sql query
The first query is my main select query. It will select millions of records
However at the later queries i have to use first selected UserIds. right now my only solution is duplicating this query
However i wonder would there be any more proper solution?
SQL server 2014 Sp1
SELECT
UserId
FROM tblUsersProfile
WHERE (
TotalBattleCount < 20
AND Money < 200000
AND LastMoveTime < DATEADD(DAY, -30, SYSUTCDATETIME())
AND UserId IN (SELECT
UserId
FROM tblUsersPokemons
GROUP BY UserId
HAVING COUNT(Id) < 20)
AND UserId NOT IN (SELECT
UserId
FROM tblUsersPokemons
WHERE PokemonLevel > 90
GROUP BY UserId)
AND UserId > 3
)
OR UserId IN (SELECT
UserId
FROM tblBannedUsers)
ORDER BY UserId ASC
DELETE FROM tblDailyRewardsParticipants
WHERE EventUserId IN (SELECT
UserId
FROM tblUsersProfile
WHERE (
TotalBattleCount < 20
AND Money < 200000
AND LastMoveTime < DATEADD(DAY, -30, SYSUTCDATETIME())
AND UserId IN (SELECT
UserId
FROM tblUsersPokemons
GROUP BY UserId
HAVING COUNT(Id) < 20)
AND UserId NOT IN (SELECT
UserId
FROM tblUsersPokemons
WHERE PokemonLevel > 90
GROUP BY UserId)
AND UserId > 3
)
OR UserId IN (SELECT
UserId
FROM tblBannedUsers)
ORDER BY UserId ASC)
DELETE FROM tblDailyRewardsUserParticipateCounts
WHERE UserId IN (SELECT
UserId
FROM tblUsersProfile
WHERE (
TotalBattleCount < 20
AND Money < 200000
AND LastMoveTime < DATEADD(DAY, -30, SYSUTCDATETIME())
AND UserId IN (SELECT
UserId
FROM tblUsersPokemons
GROUP BY UserId
HAVING COUNT(Id) < 20)
AND UserId NOT IN (SELECT
UserId
FROM tblUsersPokemons
WHERE PokemonLevel > 90
GROUP BY UserId)
AND UserId > 3
)
OR UserId IN (SELECT
UserId
FROM tblBannedUsers)
ORDER BY UserId ASC)