Using a loop in SQL to populate the table (SQL Server) -


I'm particularly new with SQL and loops and need some help with the following problem.

I have a table like this:

  SpotID eventID maximap 123 1 45 236 1 109 69 1 18 123 2 216 236 2 29 69 2 84 123 3 91 236 3 457 69 3 280  

I would like to generate a new table with the following output:

  SpotID Over30 Over70 Over100 123 3 2 1 236 2 2 2 69 So what I am doing next is to count how many times the temperatures exceed 30, 70 and 100 per spotID limit for separate eventide.  

Is there any way to do this with a loop? My data set is obviously bigger and I'm curious if I can use some skillful.

Thank you very much.

Mike

You need conditional aggregation:

 As maximal amount of 70 (when maxtemp> 100 again 1 more) (code maxtemp> 30 then 1 and 0 end), sum as over_30 (case maxtemp> 70 then 1 and 0 in the end); 0 end) as the plus_100 by likethis group by spottif;  

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -