了解何时在公式内工作

时间:2016-04-11 19:27:51

标签: sql postgresql

我在sql中有一个有效的自定义列:

,case
    when(extract(day from now() - 2)) < 3
      then '1'
    else extract(day from now() - 2)
  end as MTD

然后我想要另一个列可以解决该查询,但是当我意识到我无法在不保存/命名的情况下对此进行查询等等时我试图在公式中考虑它的情况会以同样的方式工作

我想接下来的原始查询应该是:

,(combo.autotradercom_vdp + combo.carscom_vdp) / min(MTD, combo.age) as VDP/MTD

所以不是我构建了这个,我的逻辑应该工作,但不是:

,(combo.autotradercom_vdp + combo.carscom_vdp) / min(case when (extract(day from now () - 2)) < 3 then '1' else extract (day from now() - 2) end, combo.age) as VDP/MTD

我在#34;#&#34;或附近收到错误:语法错误我知道问题出在查询的min()部分,但似乎无法理解问题所在。

感谢任何帮助!

编辑:

添加了整个查询

--work in brogress, will automate inventory review excel doc
select
  case
    when combo.total_repairs < 1
      then 'None'
    when combo.total_repairs < (combo.guaranteed_price * 4) / 100
      then 'Lo'
    when combo.total_repairs between(combo.guaranteed_price * 4) / 100
    and (
      combo.guaranteed_price * 8
    )
    / 100
      then 'Med'
    else 'Hi'
  end as Repair_Tier
  , case
    when combo.list_price < combo.guaranteed_price
      then '0'
    else combo.seller_upside_percentage
  end as Seller_Upside
  , combo.days_to_expiration as Days_remaining_on_contract
  , case
    when(combo.deposit + combo.needs_repairs + combo.going_to_auction + combo.in_transit + combo.hold_for_trade_in + combo.hold_for_financing) > 0
    or left(combo.paperwork_missing, 3) = 'Yes'
      then concat('Reserved - ', case when combo.deposit > 0 then 'Deposit' when combo.needs_repairs > 0 then 'Needs Repairs' when combo.going_to_auction > 0 then 'Going to Auction' when combo.in_transit > 0 then 'In Transit' when combo.hold_for_trade_in > 0 then 'Hold for Trade-In' when combo.hold_for_financing > 0 then 'Hold for Financing' else concat('Paperwork Missing - ', combo.paperwork_missing_reason) end)
    else ''
  end as Reserved
  , case
    when combo.future_test_drives > 0
      then combo.future_test_drives
    else '0'
  end as Future_Test_Drives
  , case
    when combo.recent_test_drives > 0
      then combo.recent_test_drives
    else '0'
  end as Recent_Test_Drivescase
  , case
    when combo.recent_buyer_leads > 0
      then combo.recent_buyer_leads
    else '0'
  end as Recent_Buyer_Leads
  --MTD-2
  , case
    when(extract(day from now() - 2)) < 3
      then 1
    else extract(day from now() - 2)
  end as MTD 
  , (combo.autotradercom_vdp + combo.carscom_vdp) / min (case
    when(extract(day from now() - 2)) < 3
      then '1'
    else extract(day from now() - 2)
  end, combo.age) as VDP/MTD
from
  [combo]

2 个答案:

答案 0 :(得分:0)

有两个问题:

, (combo.autotradercom_vdp + combo.carscom_vdp) / min (case
    when(extract(day from now() - 2)) < 3
      then '1' -- why a string instead of an INT?
    else extract(day from now() - 2)
  end, combo.age) as VDP/MTD
                  #2:^^^^^^^ this alias will result in an error, too, must be quoted
     ^^^^^^^^^^^ #1: where is this coming from?

这应该有效:

, (combo.autotradercom_vdp + combo.carscom_vdp) / min (case
    when(extract(day from now() - 2)) < 3
      then 1
    else extract(day from now() - 2)
  end) as "VDP/MTD"

答案 1 :(得分:0)

我通过使用数学而不是sql公式解决了这个问题,因为min只能引用列

解决方案:

a