postgresql - how to get a handle on fields with ':' character in them - postgresql

can't seem to get access to the addr:housenumber field in osm data using psql.
Here is the command I'm trying and I'm getting a syntax error:
select planet_osm_polygon.addr:housenumber from planet_osm_polygon, planet_osm_line where planet_osm_line.name ilike '%washington street%' limit 3;
for simplicity, this won't even work:
select addr:housenumber from planet_osm_polygon limit 3;

What about
SELECT "addr:housenumber" FROM planet_osm_polygon LIMIT 3;
?

try with "
select planet_osm_polygon."addr:housenumber" from planet_osm_polygon, planet_osm_line where planet_osm_line.name ilike '%washington street%' limit 3;

Related

How to read / list security labels on columns in postgreSQL

I've set up PostgreSQL Anonymizer on my database with security labels and everything works fine.
I'm trying to regularly ceck if there is missing security labels on the columns of my database to telle the developers to add them in the next release but I can't fin a way to read the security labels.
Can anyone know how to do this ?
EDIT on 10/11/2022
Thanks to #Shiva, I've end up doing this query :
select cl."oid", col.ordinal_position, col.table_schema, col.table_name, col.column_name
FROM information_schema.columns col
join pg_catalog.pg_class cl on cl.relname = col.table_name
WHERE col.table_schema = 'XXXX'
and not exists (select objoid FROM pg_seclabel where provider = 'anon' and objsubid = col.ordinal_position and objoid = cl."oid");
You have to query pg_seclabel catalog to get list of security labels.
SELECT objsubid, provider, label FROM pg_seclabel WHERE objoid::regclass = 'mytable'::regclass
objsubid is the column number whose corresponding column name can be found by querying information_schema.columns catalog.
SELECT column_name FROM information_schema.columns WHERE table_name = 'mytable' AND ordinal_position = <column_number>
You can combine the above two queries to find columns that do not have the required security labels.

How can I write this query in PostgreSQL? Original from Oracle

SELECT ACTOR_NAME
FROM MOVIES
WHERE MOVIE_NAME = 'Isle of Dogs'
ORDER BY SUBSTR(ACTOR_NAME, INSTR(ACTOR_NAME, ' ', -1)+1) ASC;
I think what you're looking for is the POSITION command in place of INSTR

Symfony DQL Postgresql group by isnt working

I have just switched from mysql to postgresql and my working dql queries stopped working.
Here is my dql
SELECT p
FROM AppBundle:Photo p
JOIN AppBundle:Like l WITH p = l.photo
WHERE p.isModerated = :isModerated
AND p.isActive = :isActive
AND p.category IN(:categories)
AND p.creationDate <= :begin
AND p.creationDate >= :end
GROUP BY p.id
ORDER BY l.creationDate DESC
Getting the next error
SQLSTATE[42803]: Grouping error: 7 ERROR: column "l1_.creation_date" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ... p0_.creation_date >= $4 GROUP BY p0_.id ORDER BY l1_.creati...
As i can understand, it says that group by column should be in SELECT. I dont it to be there. I need to select just p (AppBundle:Photo) and nothing more. What should be edited in dql to get it working properly?
Thank you.
I just replaced ORDER BY l.creationDate DESC to ORDER BY p.creationDate DESC
It will work for me. As i understood, ORDER BY column should be SELECTed

How to Convert Rows into Columns using SQL Select Query?

Master Table
Code UserName
1 UserOne
2 UserTwo
3 UserThree
Details Table
Code UserCode ParamName ParamValue
1 1 NameOne ValueOne
1 1 NameTwo ValueTwo
1 1 NameThree ValueThree
and so on
The above is my Master and Details table. I wanna write a query which will convert the rows of details table into columns. The desired output is given below:
Code UserCode NameOne NameTwo NameThree and so on
1 1 ValueOne ValueTwo ValueThree and so on
How can I achieve this? Any suggestion will be great in advance.
This is a common problem, without know anyting about BDMS to use, i suggest two low-level solution:
adding column by JOIN
adding column by subselect
Adding column by subselect consist in adding a subselect to take each dato do you need to traspose into column.
Adding column by JOIN consist into add a left join to data (whit right data cut), ad expose field you need into columns.
These solution are each static and valid only if you have a fixed number of column to traspsose. A way to let that dinamy could be to intruduce a store procedure.
I hope the below query will help you achieve what you want
SELECT DISTINCT
(SELECT ParamValue FROM tblDetails WHERE ParamName ='TestOne' AND UserCode = tb.UserCode) AS TestOne,
(SELECT ParamValue FROM tblDetails WHERE ParamName ='TestTwo' AND UserCode = tb.UserCode) AS TestTwo,
(SELECT ParamValue FROM tblDetails WHERE ParamName ='TestThree' AND UserCode = tb.UserCode) AS TestThree
FROM tblDetails tb
or you can use PIVOT if the ParamName can have many values which cannot be guaranteed in advance.
A few general / common strategies...
You can use a PIVOT query...
You can use a CASE (TSQL) or DECODE (PLSQL) type of statement...
SELECT
...
CASE Parmname WHEN 'NameOne' THEN [ValueOne] ELSE '' END as NameOne,
CASE Parmname WHEN 'NameTwo' THEN [ValueTwo] ELSE '' END as NameTwo
...
You can use DERIVED TABLES...
SELECT
...
N1.Parmname,
N2.Parmname,
...
FROM
...
LEFT JOIN (SELECT * FROM tbl_Detail WHERE Parmname = 'NameOne') N1
ON...
LEFT JOIN (SELECT * FROM tbl_Detail WHERE Parmname = 'NameTwo') N2
...
...etcetera

PGSQL Error Code 42703 column does not exist

I have a database in postgreSQL. I want to read some data from there, but I get an error (column anganridref does not exist) when I execute my command.
Here is my NpgsqlCommand:
cmd.CommandText = "select * from angebot,angebotstatus,anrede where anrid=anganridref and anstaid=anganstaidref";
and my 3 tables
the names of my columns are rights. So I don't understand why that error comes. Someone can explain me why it does crash? Its not the problem of large and lowercase.
You are not prefixing your column names in the where clause:
select *
from angebot,
angebotstatus,
anrede
where anrid = anganridref <-- missing tablenames for the columns
and anstaid = anganstaidre
It's also recommended to use an explicit JOIN instead of the old SQL 89 implicit join syntax:
select *
from angebot
join angebotstatus on angebot.aaaa = angebotstatus.bbbb
join anrede on angebot.aaaa = anrede.bbbb