Using expression names in subsequent calculation
От | Tony Theodore |
---|---|
Тема | Using expression names in subsequent calculation |
Дата | |
Msg-id | CAJFv53qhOROQ8ED=QZEGq6cn_Xm86bfZGrUe+SkHssp+QF9=Yg@mail.gmail.com обсуждение исходный текст |
Ответы |
Re: Using expression names in subsequent calculation
|
Список | pgsql-novice |
Hello, I'm migrating an application from Access, and having great fun so far, but I'm running into problems with some queries. What I'm trying to do boils down to this: SELECT 10 AS qty, 0.5 AS price, 0.1 AS tax_rate, qty * price AS val, val * tax_rate AS tax_amount, val + tax_amount as total but get a "column qty does not exist" error. In a real query - qty, price and tax_rate would come from actual columns, but then the error is on the "val" column. I'd like to use the name of the expression rather than repeat the calculation. Near the bottom of the SELECT docs, there's a note about namespaces that explains why this won't work, but I'm wondering how to go about this type of query. So far I've come up with: WITH order_details AS (SELECT 10 AS qty, 0.5 AS price, 0.1 AS tax_rate), order_values AS (SELECT qty * price AS val from order_details), order_taxes AS (SELECT val * tax_rate AS tax_amount FROM order_details, order_values) SELECT qty, price, tax_rate, val, tax_amount, val + tax_amount as total FROM order_details, order_values, order_taxes However, that looks at lot more complicated than simply repeating the calculations ;) What's the best way go about calculations like this that build upon previous results? Thanks, Tony
В списке pgsql-novice по дате отправления: