Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql Select Constant

Tags:

sql

postgresql

In Oracle I can select a constant value that will populate down the column like this:

Select 
     "constant" constantvalue,
     orders.name
from 
     orders

and it will yield:

ConstantValue     Name
  constant       sandwich
  constant        burger

For whatever reason, when I try to do this in postgres I receive this error.

ERROR:  column "Constant" does not exist

here is my code

    select
        date_trunc('day', measurement_date + (interval '1 day' * (6 - extract(dow from measurement_date)))) week,
        "AROutstanding" colname,
        round(avg(Total_Outstanding),0) numbah
    from
                (
                select
                    measurement_date,

                    sum(cast(sum_of_dollars as numeric)) Total_Outstanding
                from
                    stock_metrics
                where
                    invoice_status not in  ('F','Write off')
                group by
                    measurement_date
                ) tt
            group by
                week
like image 297
Brett Brockway Avatar asked Feb 25 '16 02:02

Brett Brockway


People also ask

How do I declare a constant in PostgreSQL?

Defining constantsFirst, specify the name of the constant. The name should be as descriptive as possible. Second, add the constant keyword after the name and specify the data type of the constant. Third, initialize a value for the constant after the assignment operator ( := ).

How do I select a variable in PostgreSQL?

In PostgreSQL, the select into statement to select data from the database and assign it to a variable. Syntax: select select_list into variable_name from table_expression; In this syntax, one can place the variable after the into keyword.

What is Unnest in PostgreSQL?

The purpose of unnest function in PostgreSQL is to expand the array into rows. Unnest function generates a table structure of an array in PostgreSQL. Unnest array function is beneficial in PostgreSQL for expanding the array into the set of values or converting the array into the structure of the rows.

How does select query work in PostgreSQL?

The PostgreSQL SELECT statement retrieves data from a single or several tables in a database, and returns the data in a result table, called a result-set. Use the SELECT statement to return one or more rows matching the specified criteria from the database tables.


2 Answers

Change your double quotes to single quotes.

So this:

Select 
     "constant" constantvalue,
     orders.name
from 
     orders

Should be this:

Select 
     'constant' constantvalue,
     orders.name
from 
     orders
like image 104
Adrian Lynch Avatar answered Oct 19 '22 10:10

Adrian Lynch


For anyone confused by @Adrian Lynch's answer, I found out that doing

SELECT
    'constant' AS ConstantValue,
    orders.name
FROM
    orders

did the trick for me.

This could be used for numbers as follows: 1234 AS constantvalue.

like image 1
RCRalph Avatar answered Oct 19 '22 09:10

RCRalph