Guest User

Untitled

a guest
Oct 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. -- Get the active ship data
  2. UPDATE
  3. #ship_stats
  4. SET
  5. actives = COUNT(*),
  6. duras = SUM(fc.durability)
  7. FROM
  8. crawler_flsCombatant fc
  9. WHERE
  10. cluster_id = fc.clusterID AND
  11. ship_name = fc.deedName
  12.  
  13. -- First of all, you have aggregates in an update statement which doesn't quite make sense
  14. -- Also, I try not to write sql without aliasing the tables
  15. -- So the intermediate (still invalid) form would apparently be:
  16. -- ps yo' style is uggs
  17.  
  18. UPDATE ss
  19.  
  20. SET ss.actives = COUNT(*),
  21. ss.duras = SUM(fc.durability)
  22.  
  23. FROM #ship_stats ss
  24. inner join crawler_flsCombatant fc on fc.clusterID = ss.cluster_id
  25. and fc.deedName = ss.ship_name
  26.  
  27.  
  28. -- but of course, aggregates in the update still
  29. -- a couple ways to approach this, the simplest is just subqueries
  30. -- without actually an instance of sql server to test this, I think it's correct
  31.  
  32. update ss set ss.actives = (select count(*)
  33. from crawler_flsCombatant fc
  34. where fc.cluster_id = ss.cluster_id
  35. and fc.deedName = ss.ship_name),
  36. ss.duras = (select sum(fc.durability)
  37. from crawler_flsCombatant fc
  38. where fc.cluster_id = ss.cluster_id
  39. and fc.deedName = ss.ship_name)
  40. from #ship_stats ss
Add Comment
Please, Sign In to add comment