access a column aliases in the where clause in postgresql

前端 未结 2 1731
谎友^
谎友^ 2021-01-28 01:39

I understand that I may need to do grouping to accomplish this, but I can\'t quite get it. Postgresql 8.1

I am needing to limit my result set to Origination > 2017-01-01

2条回答
  •  再見小時候
    2021-01-28 02:14

    Here is how you did it - alias in the column name

    select 
      -- etc etc 
      (select innerDLI.datetime_created from distribution_line_items innerDLI where innerDLI.item_number = distribution_line_items.item_number order by innerDLI.datetime_created asc limit 1) as Origination,
      -- etc etc
    from distribution_stop_information
      -- etc etc
    

    Here is how you can put it in the where, alias in the join

    select 
      -- etc etc 
      Origination.datetime_created
      -- etc etc
    from distribution_stop_information 
    left join distribution_line_items AS Origination ON Origination.item_number = distribution_line_items.item_number
    where Origination.datetime_created > to_date(?, 'YYYY-MM-DD') - interval '180 days'  
    -- etc etc
    

    There is nothing about this that is better than the solution you posted (the sql optimizer should result in the same plan) but it is "using an alias in the where clause"

提交回复
热议问题