此 SQL 的正确样式

Proper Style for this SQL

此 sql 代码的正确换行和缩进是什么?

sql = "select p.diff, away_score - home_score, clock from 
     (select possession,away_score,home_score, play_type, clock, 
               (lag(clock, 1) over (order by id) - clock) as diff 
               from plays where game_id in #{ids}
               and league = 1 offset 1) 
       as p 
       where possession= 0 and 
       play_type not in (150, 151, 152, 153, 154, 105, 106)
       and diff > 0 and diff < 30;"

这个问题没有正确答案。样式、格式、空格,大体上都是个人喜好。 SQL 不会太在意它是否全部是一行(请注意,这是基于我使用 MS SQL Server 的经验,其他系统可能会有点麻烦)。如果N个人查看了并且回答了这个问题,你会得到N个答案,N(N-1)个反对意见。

话虽如此,我会这样做:

sql = "
select
   p.diff
  ,away_score - home_score
  ,clock
 from (--  Comment describing the subquery
       select
          possession
         ,away_score
         ,home_score
         ,play_type
         ,clock
         ,(lag(clock, 1) over (order by id) - clock) as diff 
        from plays
        where game_id in #{ids}
         and league = 1 offset 1
      ) as p 
  where possession = 0
   and play_type not in (150, 151, 152, 153, 154, 105, 106)
   and diff > 0
   and diff < 30;
"