Questions tagged [column-alias]

155 questions
0
votes
2 answers

SQL: Unknown column 'new_id' in 'where clause'

Sample on sqlfiddle - Where I run a query like below - select CONCAT(id, rev) as new_id from DOCS where new_id>1 i am getting below error - Unknown column 'new_id' in 'where clause' Why does not sql allow to use the column alias name in the where…
samshers
  • 1
  • 6
  • 37
  • 84
0
votes
2 answers

Why can't I use only one SELECT clause with RANK() and WHERE condition

I can get the rank alias with this query: SELECT *, RANK() OVER (PARTITION BY some_field ORDER BY value) AS rk FROM my_table Result: some_field value rk same 10 1 same 20 2 same 30 3 And I tried to query with: SELECT *, RANK()…
Chris Kao
  • 410
  • 4
  • 12
0
votes
1 answer

when i run below code in postgresql it give error but when i run this in mysql editor then it runs sucessully . what is reason behind it?

this is my code when i run it on mysql it runs successfully but when i run on Postgres it doesn't run and give error as total_rate column doesn't exist: create table gross ( id numeric primary key , name text , rate numeric , quantity…
0
votes
2 answers

How to use WHERE with virtual column name added with UNION?

SELECT '1000000' AS number UNION ALL SELECT '541' AS number UNION ALL SELECT '-500' AS number UNION ALL SELECT '100' AS number UNION ALL SELECT number, 'biggest' AS result WHERE number = 1000000 How to make this work? My task is to find the…
God
  • 1
0
votes
0 answers

Choose custom column name in SELECT expression depending on returning data, PostgreSQL

Let say we have such table: id | bird | color 1 | raven | black 2 | gull | white 3 | broadbill | green 4 | goldfinch | yellow 5 | tit | yellow Is it possible in PostgreSQL to write such SELECT expression, which can make…
Volodymyr Nabok
  • 415
  • 1
  • 4
  • 11
0
votes
1 answer

how to call an alias in SQL postgres

I currently have: sum(CASE when co.date between '1/1/2022' and now() then co.amount else 0 End) as RevYTD, sum(CASE WHEN co.date between '1/1/2021' and '12/31/2021' THEN co.amount END) AS…
Kris
  • 1
0
votes
1 answer

Is it possbile to follow DRY principal in PostgreSQL inside select statement

SELECT C.category_name, P.product_name, SUM(P.unit_price) as unit_price, ROUND( AVG(P.unit_price) OVER(partition by C.category_name) :: numeric, 2 ) AS avg_unit_price, ROUND( ( ( …
Jimmy
  • 27
  • 3
0
votes
1 answer

Why would using column aliases in a select statement fill in null values with the column name?

I am using DBT to select data with column aliases, and I am wondering why the column name would become the default value if there are no values in the column I am renaming. How would you fix this select statement so that it doesn't fill in the null…
iamyolanda
  • 25
  • 4
0
votes
1 answer

How I will use the one of the output column name in same SQL statement for sum calculation

select nodw as "node ID", riskvale as "TotalRis", (portvalue - marvalue) as "TotalMarValue" --{ Here I want to take the upper column value i.e ""TotalRis"" / "TotalMarValue" * 100 } as "total Uses"** from abc I want to use…
omi
  • 11
  • 1
0
votes
0 answers

select, postgres jsonb, cast() as alias, and where

I'm a bit stumped getting an SQL query to work, using Postgress 9.5. Here is the table schema: dbhost-> \d item Table "public.item" Column | Type | …
Aaron Maxwell
  • 31
  • 1
  • 1
  • 2
0
votes
1 answer

Can we cast and rename a jsonb field in one Postgres SQL query?

I have a field in a jsonb field in a Postgres table and want to do two things directly in the query so we don't have to do cleaning in the next steps (have most of the data cleaning in one place essentially). The two things I want to do are: Change…
Jorjani
  • 827
  • 1
  • 16
  • 31
0
votes
0 answers

how to Use created SQL variable in the same query

I have the following query: SELECT target, CASE WHEN var_10 >1.5 AND var_10 <2.5 THEN 1 END as var_10_bck, Then I want to use this new created variable "var_10_bck" in the same query that continues as : CASE WHEN…
artur
  • 13
  • 3
0
votes
0 answers

Summing a column in a query with an alias - postgres

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…
0
votes
0 answers

operation execution orders in Postgresql

I'm trying to learn Postgresql with the help of this website https://www.postgresqltutorial.com. But I'm confused in the order of operations.So I compared the order they say in this site with others, all are same to each other. In some queries the…
0
votes
1 answer

Can not safely replay call when trying to assign and use a variable in procedure

I am trying use the data from a column in one table as column aliases of another table. DECLARE var1 VARCHAR(20), var2 VARCHAR(20); BEGIN WITH TABLE1 AS (SELECT ROWNUM RN, * FROM TABLE) ,A1 AS (SELECT TO_CHAR(VALUE) INTO var1 FROM TABLE1…