开发者

Help with SQL query (add 5% to users with conditions)

开发者 https://www.devze.com 2022-12-25 21:46 出处:网络
I’m having some difficulties with a query which purpose is to give users with more than one thread (called CS) in current year a 5% point “raise”.

I’m having some difficulties with a query which purpose is to give users with more than one thread (called CS) in current year a 5% point “raise”. My relational schema looks like this:

Thread = (**threadid**, threadname, threadLocation)

threadoffering = (threadid, season, year, user)

user = (**name**, points)

Then, what I need is to check:

WHERE thread.threadid = threadoffering.threadid AND where threadoffering.year AND threadoffering.season = currentDate AND where threadoffering.User > 1

then GIVE 5 % raise TO user.points

I hope it is explained thoroughly but otherwise here it is in short text:

Give a 5 % “point raise” to all users who has more tha开发者_开发知识库n 1 thread in threadLocation CS in the current year and season (always dynamic, so for example now is year = 2010 and season is = spring).

I am looking forward to your answer

Sincerely, Emil


This should work:

SELECT
    u.name,
    CASE WHEN (COUNT(*) > 1)
        THEN MIN(u.points) * 1.05
        ELSE MIN(u.points)
    END AS points
FROM
    threadoffering to inner join user u
        on to.user = u.name
WHERE
    to.year = @targetYear and to.season = @targetSeason
GROUP BY
    u.name
0

精彩评论

暂无评论...
验证码 换一张
取 消