0

如您所见,使用此代码选择一周的开始和结束。我是 sql 新手,我理解这里一般发生了什么,但我无法详细解释。例如 ;

(select lookupweekid-1 from lookupday where dateoftransaction = date) t2 (lookupweekid)

这让我很困惑。(...) t2 (...), <-我不明白最后一部分的括号在说什么

select start_of_week,end_of_week from
(select dateoftransaction
 from lookupday t1,
      (select lookupweekid-1 from lookupday 
       where dateoftransaction = date) t2 (lookupweekid)
 where t1.lookupweekid=t2.lookupweekid
   and t1.weekdaynumber=1) t3 (start_of_week)
,(select dateoftransaction
  from lookupday t1,
       (select lookupweekid-1 from lookupday 
        where dateoftransaction = date) t2 (lookupweekid)
  where t1.lookupweekid=t2.lookupweekid
  and t1.weekdaynumber=7) t4 (end_of_week)
;
4

1 回答 1

3

t4 (end_of_week)是一个表别名,它也定义了一个列别名。这避免了必须在派生表中指定列别名。

所以这:

(select dateoftransaction
 from lookupday t1 ... 
 where t1.lookupweekid=t2.lookupweekid
 and t1.weekdaynumber=7) t4 (end_of_week)

相当于:

(select dateoftransaction as end_of_week
 from lookupday t1 ... 
 where t1.lookupweekid=t2.lookupweekid
 and t1.weekdaynumber=7) t4

这在Postgres 手册中有解释

于 2021-11-26T08:05:38.263 回答