29

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
1
  • 3
    Single quotes are the correct way to express a constant string value in SQL (and they work in any database). I consider this a simple typographic error. Commented Feb 25, 2016 at 2:18

2 Answers 2

42

Change your double quotes to single quotes.

So this:

Select 
     "constant" as constantvalue,
     orders.name
from 
     orders

Should be this:

Select 
     'constant' as constantvalue,
     orders.name
from 
     orders
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that worked. I thought I tried it but apparently not :)
It's always a pain moving from one DB to another. I went from MSSQL to MySQL and I'm googling for answers all the time!
You should be careful when "Omitting the AS Key Word" as noted in the manual postgresql.org/docs/current/sql-select.html some of the keywords postgresql.org/docs/current/sql-keywords-appendix.html could well interfere with the chosen column name.
3

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.

2 Comments

What part was confusing @RCRalph? The lack of as? I've added it to the answer.
@AdrianLynch yes that was it, thanks for editing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.