Given a Postgres database with some extensions such as address_standardizer, how to run the below statement with Query Builder:
SELECT pprint_addy(normalize_address('202 East Fremont Street, Las Vegas, Nevada 89101'));
Which yields:
202 E Fremont St, Las Vegas, NV 89101
You can run raw select statements with the DB::select() method:
$data = DB::select('SELECT pprint_addy(normalize_address(?)) as address', ['202 East Fremont Street, Las Vegas, Nevada 89101']);
print_r($data);
Related
I have a table that has columns with title and content.
CREATE TABLE t_items (
item_id varchar PRIMARY KEY,
title varchar,
content varchar);
insert into t_items values ('111', 'Madrid title n1', 'The Madrid papers Open revelled in Reals win, with AS describing Chelsea as shipwrecked having been completely unsettled by Mendys error for');
insert into t_items values ('222', 'test title 22', 'Así fue el Mutua Madrid Open 2019, un torneo inolvidable. En la última edición, Novak Djokovic y Kiki Bertens se proclamaron campeones, David Ferrer se');
insert into t_items values ('333', 'Madrid title 33', 'Real Madrid play at Stamford Bridge for just the second time ever, and the first time with fans in the stands. Its going to be epic');
insert into t_items values ('444', 'test title 44', 'You can change the date of your tickets because of Covid-19 without any problems. A little bit of action · Zoo Aquarium de Madrid | Disfruta de tus animales');
insert into t_items values ('555', 'test title 554', 'In a former electrical substation, Kunsthalle Praha is sparking new energy in the Czech capital with inaugural show 100 year');
insert into t_items values ('661', 'test title 4554', 'Bistro Praha is the place to go if you want a warm atmosphere, authentic goulash, cabbage soup, wiener schnitzel or a range of European style dishes.');
I need to make a query that will return a prepared response for later full text search. But all the results of the found text should be combined into one line. Also, all found document IDs should be combined into one array.
SELECT to_tsvector('english', t_items.content || t_items.title),
array_agg(t_items.item_id) as id_array
FROM t_items
WHERE
to_tsvector(title) || to_tsvector(content)
## plainto_tsquery('Madrid & Open')
GROUP BY t_items.content, t_items.title;
My query incorrectly returns scattered rows.
-----------+----------
'2019':7 '22':27 'así':1 | {222}
'chelsea':12 'complet':17 'describ':11 | {111}
(2 rows)
I need the result of a union that includes all the results in one line:
-----------+----------
'2019':7 '22':27 'así':1 'chelsea':12 'complet':17 'describ':11 | {222, 111}
(1 rows)
Here is a working example:
https://onecompiler.com/postgresql/3xyf3pfq5
I am stuck at this point, I am trying to clean the row name of a table using postgresql. The table is for districts in wales and England.
What I am trying is to delete the strings 'District', '(B)', 'London Boro' and everything before the hyphen in the dual English - Welsh name, for example (Swansea - Abertawe) I would like to eliminate everything before the hyphen (I would like to know how to do it in both direction anyway) and get (Abertawe). I manage with the first 3 strings but with the part of hyphen I cannot find the solution to the issue.
I have tried using replace and trim but I don't obtain the result desired.
This is code as I have tried the last time:
select name,
trim(replace(replace(replace(replace(name, 'District', ''), '(B)', ''), 'London Boro', ''),'% - %', ''), ' - ')
from district;
This is a sample of the table used
name
Swansea - Abertawe
Barnsley District (B)
Bath and North East Somerset
Bedford (B)
Birmingham District (B)
Blackburn with Darwen (B)
Blackpool (B)
Blaenau Gwent - Blaenau Gwent
Bolton District (B)
Bournemouth (B)
Bracknell Forest (B)
Bradford District (B)
The Vale of Glamorgan - Bro Morgannwg
Aylesbury Vale District
Chiltern District
South Bucks District
Wycombe District
Bury District (B)
Cardiff - Caerdydd
Caerphilly - Caerffili
Calderdale District (B)
Cambridge District (B)
East Cambridgeshire District
City of Westminster London Boro
Croydon London Boro
Ealing London Boro
Enfield London Boro
Castell-nedd Port Talbot - Neath Port Talbot
And this is what I want to obtain:
name
Abertawe
Barnsley
Bath and North East Somerset
Bedford
Birmingham
Blackburn with Darwen
Blackpool
Blaenau Gwent
Bolton
Bournemouth
Bracknell Forest
Bradford
Bro Morgannwg
Aylesbury Vale
Chiltern
South Bucks
Wycombe
Bury
Caerdydd
Caerffili
Calderdale
Cambridge
East Cambridgeshire
City of Westminster
Croydon
Ealing
Enfield
Neath Port Talbot
Thanks,
Use Case statement to distinguish both of the conditions and use Position function to check the - exist or not.
Try This
select
name,
case when position(' - ' in name)>0 then
trim(replace(replace(replace(substr(name,position(' - ' in name)+3,length(name)), 'District', ''), '(B)', ''), 'London Boro', ''))
else
trim(replace(replace(replace(name, 'District', ''), '(B)', ''), 'London Boro', ''))
end
from district
DEMO
Working with the professional mobility of insee on more than 1m of entities, i seek to add up a field called ipondi only on the journeys from commune of residence to commune of work, and not of commune of work to residential commune.
Let us assume a simple example, with the column of commune of residence named "departure", and commune of work named "arrival", and the field which i wish to make the sum named "ipondi":
start; end; ipondi
La Ciotat; Marseille; 84
La Ciotat; Marseille; 15
Aubagne; Ceyreste; 12
Marseille; La Ciotat; 73
So I get the following result:
select start, end, sum(ipondi)
from trajets
group by start, end
So I get the following result:
La Ciotat; Marseille; 99
Aubagne; Ceyreste; 12
Marseille; La Ciotat; 73
Which is normal. However, I would like to "delete" the Marseille; La Ciotat because it is the return journey of the first two lines.
This being so to arrive at this result:
start; end; ipondi
La Ciotat; Marseille; 99
Aubagne; Ceyreste; 12
My link to my database : https://drive.google.com/file/d/1TOB1MTAt8UNCjt0up6qcgnR593yMXkqt/view?usp=sharing
How to do this on PostgreSQL?
Thank you.
I am using the musicbrainz.org postgresql database, which I have installed locally and have accessed via python.
The database is a list of music artists and associated criteria. Here is the schema :
How can I create a SQL query that outputs all tracks for a release group? I am able to get the proper artist info and releases associated with a specific band but the tracks for the release are wrong using the below query's:
strsql_band = "SELECT artist.id, artist.gid, artist.name, artist.comment FROM artist WHERE artist.name=%s AND type>1 ORDER BY type, artist.last_updated DESC"
strsql_memberid = "SELECT entity0, link.begin_date_year, link.end_date_year FROM l_artist_artist l JOIN link ON l.link=link.id WHERE entity1=%s AND link_type=103"
strsql_release = "SELECT id, release_group.name FROM release_group WHERE artist_credit=%s"
# This does not return the correct tracks for the release
strsql_track = "SELECT id, position, name, length FROM track WHERE artist_credit=%s LIMIT 15"
Any help would be greatly appreciated, I have been banging my head on this for hours now.
medium and track table will give you this information
Here is the query that worked for my case:
SELECT
array_agg(track.name) as track_list,
release.gid as release_gid,
release.release_group as release_group_id
FROM track
INNER JOIN medium
ON track.medium=medium.id
INNER JOIN release
ON medium.release=release.id
GROUP BY release.gid, release.release_group limit 5;
Sample Results
release_gid release_group track_list
00000363-74c9-4c3b-bef7-a433eb9687e3 950559 {"Small Screen",Alice,Dauntless,Lullaby,"Grinding the Mill","Four Colors","2000 Gods","This Life","More Than Meets the Eye","The World"}
00000ba0-d5e2-41d7-9c5d-48e8eb04226a 388941 {"Ai, Vanna","Dod māmiņa, kam dodama","Mēs sūdzēsimies Strazdbūrā","Ar Eiropu plecos","Mīļais mans (Oi ļuļi, oi ļuļi)","Tu tik lūr, monamūr","Kocēni, Dikļi un Tožas","Sev augstu laimi vēlēju","Happy mērsrags","Tur es dzēru, tur man tika","Pritonā pie eglītes","Kur ir manas dāvaniņas","Iespraud svecīti","Ints un Ģirts","Mazs putniņš kājām... (Vai Rīga gatava?)","Melngalvju blūzs",Siergriezējs,"Zeva dziesma","Kentaura skaistumkopšanas salonā","Es sakūru uguntiņu (Gaisā skrēja)","Tu nesit... (Nestandarta žoklis)"}
00000ff3-8918-400f-89d5-ec80f52465bf 530779 {"SING IT","Precious Days",トキメキ}
0000172d-6e43-4d7d-8647-da718593a97a 2382060 {"J'ai besoin d'un doliprane","Cœur brisé","Mon proprio","J'me demande","Extra extra terrestre","Bienvenue chez moi (dans ma coloc)","L'amour c'est du pipeau","La caissière du casino",Jean-Marc,"Toutes des salopes"}
Is there anyway to sort the string with number field by its number only
I have a value like this
subject_code
DE 312
DE 313
DE 315
Eng 311
COMP 314
can it be sort like this
subject_code
Eng 311
DE 312
DE 313
COMP 314
DE 315
I tried
order by SOUNDEX(subject_code),LENGTH(subject_code),subject_code
but it does not work as expected.
Thank you for your any help and suggestions.
One workaround to your situation uses string operations to obtain the numerical subject code and use it for sorting.
SELECT
subject_code
FROM yourTable
ORDER BY
CAST(SUBSTR(subject_code,
INSTR(subject_code, ' ') + 1) AS UNSIGNED)
However, you should really be storing the text and numerical code in separate columns.
Output:
Demo here:
Rextester