ERROR: column "***" does not exist in dense rank function in postgres [duplicate] - sql

This question already has answers here:
Postgres error updating column data
(2 answers)
PostgreSQL "column "foo" does not exist" where foo is the value
(1 answer)
postgres column "X" does not exist
(1 answer)
Simple Postgresql Statement - column name does not exists
(2 answers)
Error: Column does not exist in postgresql for update [duplicate]
(1 answer)
Closed 5 months ago.
I would like to add columns by following window function.
select goods_code,event_code ,DENSE_RANK() Over (
Order By case when goods_code = "aaaa" then 1 end,
case when event_code = "a" then 1 end)
from atai.pricing_pattern
but it returned following error.
ERROR: column "aaaa" does not exist
LINE 1: ...ENSE_RANK() Over (Order By case when goods_code = "aaaa" the...
^
What is the root cause of this? if someone has opinion,will you please let me know
Thanks

Related

SQL Server using IIF resulted AS column in another IIF [duplicate]

This question already has answers here:
Possible to store value of one select column and use it for the next one?
(4 answers)
Using alias in query and using it
(6 answers)
Why can't I use column aliases in the next SELECT expression?
(4 answers)
Closed 9 months ago.
on SQL Server 2012+ I am attempting to use IIF resulted column into another IIF as shown in SQL query below but it is gives me error invalid column. Count table has columns CountId and CountedQty,
SELECT CountId, IIF(CountId<5, 3,2) as MyGroup, IIF([MyGroup]<5, 3,2) as GName FROM Count
Error shown for this query execution is Invalid column name 'MyGroup' if there any way to use IIF resulted column into another IIF in same query
similarly for following query it shows invalid column names errors
SELECT
Count.StockCountId,
Count.CountedQty,
IIf(Count.CountedQty > 0, 1, IIf(Count.CountedQty = 0, 3, 2)) AS MyGroup,
IIf([MyGroup] = 1, "Additional products counted", IIf([MyGroup] =2, "Insufficient products counted", "Matched Count")) AS GName
FROM Count
it returns following errors
Invalid column name 'MyGroup'.
Invalid column name 'Additional products counted'.
Invalid column name 'MyGroup'.
Invalid column name 'Insufficient products counted'.
Invalid column name 'Matched Count'.
what changes are needed to use IIF resulted columns into above query to resolve errors and get intended output
You cannot reference a column alias like that, at the time query execution reaches select it does not yet exist.
You can use a derived table or CTE and then reference the column in an outer query, you can repeat the expression, or in SQL Server you could use apply.
Your logic seems a little odd however as you'll always return 3 since MyGroup is always < 5 in this example.
select CountId, Iif(MyGroup < 5, 3, 2) as GName
from [Count]
cross apply(values(Iif(CountId < 5, 3, 2)))c(MyGroup);

SQL Case statement - Null + Not Null - then return a column [duplicate]

This question already has answers here:
SQL is null and = null [duplicate]
(4 answers)
Closed 11 months ago.
I use SQL inside a 3rd party system (So don't know the type it is)
I am trying to make a CASE work on a column using data from 2 more.
I want to display a column call Channel that is calculated using the following logic:
If column O.Test is blank and column o.subsource is not blank, display 'RESEND', otherwise display the value of column o.Source.
This is part of the SQL showing the CASE I wrote to do this:
select
-- other columns
(CASE
WHEN o.Test = NULL AND o.Subsource IS NOT NULL THEN 'RESEND'
ElSE o.Source
END) o.Source AS 'Channel',
-- other columns
The SQL runs with no errors but the output always shows what is in o.Source.
X = NULL is not equal IS NULL X
Check this question,
SQL is null and = null
change this statement "o.Test = NULL" instead of "o.Test IS NULL"

Getting column does not exist error in postgresql sql table [duplicate]

This question already has answers here:
Error: Column does not exist in postgresql for update [duplicate]
(1 answer)
postgres column "X" does not exist
(1 answer)
Closed 2 years ago.
I have an sql table that looks like this in postgresql called test.
date | data | source
------------+---------+------------------
2015-09-23 | 128 | aaamt
2015-09-24 | 0 | aaamtx2
.....
I type SELECT * FROM test where source="aaamt" but I get the following error,
ERROR: column "aaamt" does not exist
LINE 1: SELECT * FROM test where source = "aaamt";
Why am I getting this error and how to I fix it?
You need to use single quote instead of double quote
SELECT * FROM test where source = 'aaamt'
Double quotes indicate to Postgres that you are trying to specify an identifier such as a column or table name. Use single quotes for string literals, and your query should work:
SELECT *
FROM test
WHERE source = 'aaamt';
To be clear here, you current query is basically being interpreted as this:
SELECT *
FROM test
WHERE source = aaamt;
Here aaamt is being treated as a column name or maybe some other database identifier, but not as a string literal.

SQL Select statement how to return emoji [duplicate]

This question already has answers here:
SQL Query Where Column = '' returning Emoji characters 🎃 and 🍰
(4 answers)
Closed 3 years ago.
I currently have a query where I want to select cake:
SELECT '🍰'
But upon executing query, it gives me an output of '??'
How to output the real cake?
set as unicode using the N'' notation
SELECT N'🍰'

Error on dropping pandas dataframe row using boolean condition indexing [duplicate]

This question already has answers here:
How to filter Pandas dataframe using 'in' and 'not in' like in SQL
(11 answers)
Closed 3 years ago.
I'm trying to remove rows from a dataframe if a particular column value does not appear in a previously defined dictionary
dff= dff[dff['network'] in net_dic]
Each value of 'network' is a string. and net_dic looks like this:
{ 'abc' : 1
'def' : 2
.
.
.}
It errors:
TypeError: 'Series' objects are mutable, thus they cannot be hashed
Ah this works:
dff= dff[dff['network'].isin(net_dic.keys())]