How could I update the St. to Street on this column? I am getting a hard time on how to figure this out
Address
125 Center St, New York City, NY 10001
68 Hickory St, Seattle, WA 98101
I am trying to update one word on a column which is the St. to Street
I would use an expression like
regexp_replace(col, '\mSt\M\.?', 'Street', 'g')
Related
I have a table in Postgres that has records like
ID
Address
1
862 N Longbranch Road Voorhees, NJ 08043
2
7300 Overlook, Ave Moncks Corner, SC 29461
3
76 SW Green Lake, Street Sterling, VA 20164
4
597 Wintergreen St Erlanger, KY 41018
So for searching a specific address my query is simple
select * from profile where address ilike '7300 Overlook, Ave Moncks Corner, SC%'
This is returning record 2
What I want is
select * from profile where address ilike '7300 Overlook Ave Moncks Corner SC%'
(Please note that commas are missing in second query)
Even if the string inside ilike doesn't contain comma , result 2 should be returned.
I have a table with a column containing an address.
I want to remove everything after the , in the string.
How do I go about doing that in PostgreSQL?
I've tried using REPLACE, but that only works on specific strings, which is a problem because each row in the column would have a different address.
SELECT *
FROM address_book
r_name r_address
xxx 123 XYZ st., City, Zipcode
yyy 333 abc road, City, Zipcode
zzz 222 qwe blvd, City, Zipcode
I'm need column r_address to only return:
123 XYZ st.
333 abc road
222 qwe blvs
Use the split_part function, like so:
SELECT r_name, split_part(r_address, ',', 1) AS street
FROM address_book
Docs: https://www.postgresql.org/docs/current/functions-string.html
Fiddle: http://sqlfiddle.com/#!17/51afe/1
I have a table which holds data in the following format, however I would like to be able to create a query that checks whether the reference number is duplicated and only return the entry with the latest date_issued.
ref_no name gender place date_issued
xgb/358632/p John Smith M London 02.08.2016
Xgb/358632/p John Smith M London 14.06.2017
Rtu/638932/k Jane Doe F Birmingham 04.09.2017
The result from the query should be;
ref_no name gender place date_issued
Xgb/358632/p John Smith M London 14.06.2017
Rtu/638932/k Jane Doe F Birmingham 04.09.2017
Is there a fairly straightforward solution for this?
assuming the date column is type date or timestamp
select distinct on(ref_no) * from tablename order by refno,date desc;
this works beacuse distinct on supresses rows with duplicates of the expression in parenthese.
After the second space, I need to fetch the values till the particular position in the string.
Source:
"8 115 MACKIE STREET VICTORIA PARK WA 6100 AU"
"6A CAMBOON ROAD MORLEY WA 6062 AU"
output:
"MACKIE STREET VICTORIA PARK"
"CAMBOON ROAD MORLEY"
I'm trying to split the street name and suburb from the unit #,street# present in the beginning and the state, postcode, country present in the end.
t=# with s(v) as (values('6A CAMBOON ROAD MORLEY WA 6062 AU'),('8 115 MACKIE STREET VICTORIA PARK WA 6100 A'))
, split as (select *,count(1) over (partition by v) from s, regexp_matches(v,'( [A-Z]+)','g') with ordinality t(m,o))
select distinct v,string_agg(m[1],'') over (partition by v) from split where o <= count-(3-1);
v | string_agg
---------------------------------------------+------------------------------
8 115 MACKIE STREET VICTORIA PARK WA 6100 A | MACKIE STREET VICTORIA PARK
6A CAMBOON ROAD MORLEY WA 6062 AU | CAMBOON ROAD MORLEY
(2 rows)
I excluded index (or any not fitting mask [A-Z]+) thus cutting not three positions from the end, but two (3-1) where 1 is ahead known index.
Also I start not from the second space as it would be against your desired result...
Let's say I have some data along the lines of:
Department Location | Product Sale ID
New York ID-1
New York ID-1
New York ID-2
New York ID-2
California ID-1
California ID-1
California ID-3
Florida ID-3
Florida ID-4
Florida ID-5
I want to create a new view so that it counts the distinct number of times there is an overlap such the results are:
Department Location | Distinct ID Overlap Count
New York 1
California 2
Florida 1
In this case, New York has an ID overlap with California on ID-1. California has an overlap with New York on ID-1 and Florida on ID-3. Florida, conversely, only has the ID overlap with California on ID-3.
I've looked into doing a LOD calculation along the lines of:
{fixed [Department Location]:countd[Product Sale ID]}
But I'm not sure how to really to extract the results I want from here. I'm having trouble thinking of how to approach this logically and am wondering if it is possible or can only be done on the data source side?
You were on the right track with FIXED. But what we really care about is which Product Sale IDs have multiple locations. You can calculate that with a very straightforward LOD expression:
{ FIXED [Product Sale ID] : COUNTD([Department Location]) } > 1
With that information now available to us, you just need to count, for each Product Location, the number of distinct Product Sale IDs that occur in multiple locations. Here's one way to do that:
COUNTD(
IIF(
{ FIXED [Product Sale ID] : COUNTD([Department Location]) } > 1,
[Product Sale ID],
NULL
)
)