Assume Table A with an XML field. Table A has two rows, with the following XML data in the table.
Row 1
<fullname>
<firstName>John</firstName>
<lastName>Smith</lastName>
</fullname>
and Row 2
<fullname>
<firstName>Jane</firstName>
</fullname>
This query:
SELECT * FROM A
XMLTABLE(('/fullname'::text) PASSING (a.xml)
COLUMNS firstName text PATH ('firstName'::text), lastName text PATH ('lastName'::text)) a
will only return data on John and not Jane. Is there a work around for this?
https://www.postgresql.org/docs/13/functions-xml.html
"default X" resolved the issue.
Related
I've a postgres table with data like
Name
Attendance
Jackie
2
Jade
5
Xi
10
Chan
15
In my query I want all present by name, and if name doesn't exist return "null" instead of no row for that particular name
Eg
query where Name in ('Jackie', 'Jade', 'Cha', 'Xi')
Should return
Name
Attendance
Jackie
2
Jade
5
NULL
NULL
Xi
10
To produce the desired rows, you need to join with a table or set of rows which has all those names.
You can do this by inserting the names into a temp table and joining on that, but in Postgres you can turn an array of names into a set of rows using unnest. Then left join with the table to return a row for every value in the array.
select attendances.*
from
unnest(ARRAY['Jackie','Jade','Cha','Xi']) as names(name)
left join attendances on names.name = attendances.name;
On my PostgreSQL DB I have a table that looks like below. The description column can store a string of any size.
What I'm looking for, is a way to select just the first X chars from the content of the description column, or the whole string if X > description.length
CREATE TABLE descriptions (
id uuid NOT NULL,
description text NULL,
);
E.g.: If X = 100 chars and if in the description column I store a string containing 150+ chars, when I run select <some method on description> from descriptions, I just want to get back the first 100 chars from the description column.
Bonus if the approach proposed is extremely fast!
Use a type cast or use substring():
SELECT CAST(description AS varchar(100)), substring(description for 100)
FROM descriptions;
Or if you want to do it the "PostgreSQL way":
SELECT description::varchar(100), substr(description, 1, 100)
FROM descriptions;
I am trying to import specified data using query importrange but at the same time I want to reduce need for additional calculation columns and by using concat or something similar to add 2 columns together with a space in between ie. first name 'bob' last name 'smith' returns 'bob smith' in 1 column
=QUERY({IMPORTRANGE("https://docs.google.com/spreadsheets/d/1oaZP3-p1cI4d1QyLQ2qM5sMwnVGz8S0bhe29W4QqH6g/edit#gid=1908577977","Sheet7!A2:c"),"select Col1&" "&Col2,Col3",0})
I've tried the above but it returns formula parse error
https://docs.google.com/spreadsheets/d/1oaZP3-p1cI4d1QyLQ2qM5sMwnVGz8S0bhe29W4QqH6g/edit?usp=sharing
in post-IMPORTRANGE you can join two columns only like this:
=FLATTEN(QUERY(TRANSPOSE(QUERY(
IMPORTRANGE("13Ptmj3sejlOADvwhgfBPxRy_H-RGCxLX4r2jecbceIE", "Sheet7!A2:C"),
"select Col1,Col2", )),,9^9))
so for 3 columns:
={FLATTEN(QUERY(TRANSPOSE(QUERY(
IMPORTRANGE("13Ptmj3sejlOADvwhgfBPxRy_H-RGCxLX4r2jecbceIE", "Sheet7!A2:C"),
"select Col1,Col2", )),,9^9)),
IMPORTRANGE("13Ptmj3sejlOADvwhgfBPxRy_H-RGCxLX4r2jecbceIE", "Sheet7!C2:C")}
I have a parquet file with the following format
id = 2a1ed0848022
raw_value:
[{"state":"MO","city":"O Fallon","location_name":"Jackson Hewitt Tax Service","top_category":"Accounting, Tax Preparation, Bookkeeping, and Payroll Services"},
{"state":"IL","city":"Collinsville","location_name":"L E Smith Jewelry","top_category":"Jewelry, Luggage, and Leather Goods Stores"},
{"state":"MO","city":"O Fallon","location_name":"Bagwasi Family Eyecare","top_category":"Health and Personal Care Stores"},
{"state":"MO","city":"O Fallon","location_name":"Rally's Drive-In Restaurants","top_category":"Restaurants and Other Eating Places"},
{"state":"IL","city":"Collinsville","location_name":"BP","top_category":"Gasoline Stations"}
I would like to create a table in Athena on this parquet file and run a query like this
select maid from test12 where state="MD" and city="Baltimore".
How can I search state and city from the second column which has nested JSON.
The key to this is to use UNNEST. I'm assuming that raw_value is typed as array<struct<state:string,city:string,location_name:string,top_category:string>>.
SELECT id
FROM the_table CROSS JOIN UNNEST(raw_value) rv (location)
WHERE location.state = 'MD' AND city = 'Baltimore'
Using UNNEST like this expands each row in the table to one row per element in the array.
If raw_value is a string column, you need to parse it first. You can find an example of this in this answer: https://stackoverflow.com/a/56176204/1109
I have a table with columns that contain arrays that I want converted into strings so I can split them by the delimiter into multiple columns.
I'm having trouble doing this with arrays of dates with timezones.
create materialized view matview1 as select
(location) as location,
(nullif(split_part(string_agg(distinct name,'; '),'; ',1),'')) as name1,
(nullif(split_part(string_agg(distinct name,'; '),'; ',2),'')) as name2,
(nullif(split_part(string_agg(distinct name,'; '),'; ',3),'')) as name3,
(array_agg(distinct(event_date_with_timestamp))) as event_dates
from table2 b
group by location;
In the code above I'm creating a materialized view of table to consolidate all table entries related to certain locations into single rows.
How can I create additional columns for each event_date entry like I did with the names (e.g Name1, Name2 and Name3 from the 'name' array)?
I tried changing the array to a string format with:
(nullif(split_part(array_to_string(array_agg(distinct(event_date_with_timestamp))),'; ',1),'')) as event_date1
But this throws the error:
"function array_to_string(timestamp with time zone[]) does not exist"
And casting to different datatypes always produces errors saying I can't cast from type timestampz into anything else.
I found a way to accomplish this by casting from timestampz to text and then back again like this:
(nullif(split_part(string_agg(distinct event_date::text,'; '),'; ',1),'')::date) as date1,