0

I am running this query:

SELECT exchange_name, 
       COUNT(exchange_name) as trade_count,           
       SUM(trade_count) as total_trades 
FROM trades 
WHERE trade_time / 1000 > (extract(epoch from now()) - (86400)*1)   
GROUP BY exchange_name 
ORDER BY trade_count DESC

I am running into this error:

ERROR:  column "trade_count" does not exist

How can I sum the trade_count column and add it as an entry in every row in a separate column?

  • Unrelated, but: `extract(epoch from now()) - (86400)` can be made more readable using: `extract(epoch from (now() - interval '1 day')` - and if `trade_time` was a proper `timestamp` column (which it really should be), then this would be even more readable: `where trade_time > now() - interval '1 day'` –  Jul 15 '22 at 13:26
  • What do you mean "proper timestamp"? Like unix timestamp? – Husnain Mehmood Jul 15 '22 at 13:46
  • `timestamp` or `timestamp with time zone` is a [data type](https://www.postgresql.org/docs/current/datatype-datetime.html) especially designed to store, well, timestamps. A "unix timestamp" is not a "timestamp" - it's just a number. https://blog.sql-workbench.eu/post/epoch-mania/ –  Jul 15 '22 at 13:49

0 Answers0