How to store multiple circles in a single PostGIS geometry column? - sql
I need to store multiple circles in a single geometry column in PostGIS.
So far I can store only one like this:
UPDATE element SET geo = ST_Buffer(ST_MakePoint(-71.1043443253471, 42.3150676015829), 6, 'quad_segs=8') WHERE id = 1;
Is there a way to use GEOMETRYCOLLECTION here?
Thanks in advance.
Aggregate your buffers using ST_Union and then create the collection with ST_ForceCollection, e.g. to append a buffer to an existing geometry:
UPDATE element
SET geo =
ST_ForceCollection(
ST_Union(geo, ST_Buffer(ST_MakePoint(-80.10, 45.31), 3) ))
WHERE id = 1;
Demo: db<>fiddle
SELECT
ST_ForceCollection(
ST_Union(
ST_Buffer(ST_MakePoint(-71.10, 42.31), 6),
ST_Buffer(ST_MakePoint(-80.10, 45.31), 3)
));
st_astext
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
GEOMETRYCOLLECTION(POLYGON((-77.1576441587903 44.72472903395162,-77.32836140246613 44.161949702904735,-77.60559116309236 43.6432893009412,-77.97867965644035 43.188679656440364,-78.43328930094118 42.81559116309237,-78.95194970290473 42.538361402466144,-79.51472903395161 42.36764415879031,-80.1 42.31,-80.68527096604838 42.36764415879031,-81.24805029709526 42.53836140246614,-81.76671069905879 42.815591163092364,-82.22132034355963 43.18867965644036,-82.59440883690763 43.64328930094119,-82.87163859753385 44.16194970290473,-83.04235584120968 44.72472903395161,-83.1 45.309999999999995,-83.04235584120968 45.89527096604838,-82.87163859753386 46.45805029709526,-82.59440883690763 46.9767106990588,-82.22132034355964 47.431320343559634,-81.7667106990588 47.80440883690763,-81.24805029709528 48.08163859753386,-80.68527096604839 48.25235584120969,-80.10000000000001 48.31,-79.51472903395162 48.2523558412097,-78.95194970290474 48.08163859753387,-78.4332893009412 47.80440883690765,-77.97867965644036 47.431320343559655,-77.60559116309237 46.97671069905883,-77.32836140246614 46.45805029709529,-77.1576441587903 45.89527096604841,-77.1 45.31,-77.1576441587903 44.72472903395162)),POLYGON((-65.21528831758062 41.139458067903234,-65.55672280493228 40.01389940580947,-66.11118232618472 38.976578601882395,-66.8573593128807 38.06735931288072,-67.76657860188237 37.32118232618473,-68.80389940580945 36.766722804932286,-69.92945806790321 36.42528831758062,-71.09999999999998 36.31,-72.27054193209675 36.425288317580616,-73.39610059419053 36.76672280493228,-74.4334213981176 37.321182326184726,-75.34264068711927 38.06735931288071,-76.08881767381526 38.97657860188238,-76.64327719506771 40.01389940580945,-76.98471168241937 41.13945806790321,-77.1 42.30999999999998,-76.98471168241937 43.48054193209675,-76.64327719506772 44.60610059419052,-76.08881767381529 45.643421398117596,-75.3426406871193 46.55264068711927,-74.43342139811763 47.29881767381526,-73.39610059419056 47.85327719506771,-72.27054193209679 48.19471168241938,-71.10000000000002 48.31,-69.92945806790325 48.19471168241939,-68.80389940580949 47.85327719506773,-67.76657860188242 47.29881767381529,-66.85735931288073 46.55264068711931,-66.11118232618475 45.643421398117646,-65.55672280493229 44.60610059419057,-65.21528831758062 43.48054193209681,-65.1 42.31,-65.21528831758062 41.139458067903234)))
(1 row)
Or using a CTE
WITH j (geom) AS (
VALUES (ST_Buffer(ST_MakePoint(-71.10, 42.31), 6)),
(ST_Buffer(ST_MakePoint(-80.10, 45.31), 3))
)
SELECT ST_ForceCollection(ST_Union(geom)) FROM j;
st_astext
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
GEOMETRYCOLLECTION(POLYGON((-77.1576441587903 44.72472903395162,-77.32836140246613 44.161949702904735,-77.60559116309236 43.6432893009412,-77.97867965644035 43.188679656440364,-78.43328930094118 42.81559116309237,-78.95194970290473 42.538361402466144,-79.51472903395161 42.36764415879031,-80.1 42.31,-80.68527096604838 42.36764415879031,-81.24805029709526 42.53836140246614,-81.76671069905879 42.815591163092364,-82.22132034355963 43.18867965644036,-82.59440883690763 43.64328930094119,-82.87163859753385 44.16194970290473,-83.04235584120968 44.72472903395161,-83.1 45.309999999999995,-83.04235584120968 45.89527096604838,-82.87163859753386 46.45805029709526,-82.59440883690763 46.9767106990588,-82.22132034355964 47.431320343559634,-81.7667106990588 47.80440883690763,-81.24805029709528 48.08163859753386,-80.68527096604839 48.25235584120969,-80.10000000000001 48.31,-79.51472903395162 48.2523558412097,-78.95194970290474 48.08163859753387,-78.4332893009412 47.80440883690765,-77.97867965644036 47.431320343559655,-77.60559116309237 46.97671069905883,-77.32836140246614 46.45805029709529,-77.1576441587903 45.89527096604841,-77.1 45.31,-77.1576441587903 44.72472903395162)),POLYGON((-65.21528831758062 41.139458067903234,-65.55672280493228 40.01389940580947,-66.11118232618472 38.976578601882395,-66.8573593128807 38.06735931288072,-67.76657860188237 37.32118232618473,-68.80389940580945 36.766722804932286,-69.92945806790321 36.42528831758062,-71.09999999999998 36.31,-72.27054193209675 36.425288317580616,-73.39610059419053 36.76672280493228,-74.4334213981176 37.321182326184726,-75.34264068711927 38.06735931288071,-76.08881767381526 38.97657860188238,-76.64327719506771 40.01389940580945,-76.98471168241937 41.13945806790321,-77.1 42.30999999999998,-76.98471168241937 43.48054193209675,-76.64327719506772 44.60610059419052,-76.08881767381529 45.643421398117596,-75.3426406871193 46.55264068711927,-74.43342139811763 47.29881767381526,-73.39610059419056 47.85327719506771,-72.27054193209679 48.19471168241938,-71.10000000000002 48.31,-69.92945806790325 48.19471168241939,-68.80389940580949 47.85327719506773,-67.76657860188242 47.29881767381529,-66.85735931288073 46.55264068711931,-66.11118232618475 45.643421398117646,-65.55672280493229 44.60610059419057,-65.21528831758062 43.48054193209681,-65.1 42.31,-65.21528831758062 41.139458067903234)))
(1 row)
Related
BigQuery SQL JSON Returning additional rows when current row contains multiple values
I have a table that looks like this keyA | data:{"value":false}} keyB | data:{"value":3}} keyC | data:{"value":{"paid":10,"unpaid":20}}} For keyA,keyB I can easily extract a single value with JSON_EXTRACT_SCALAR, but for keyC I would like to return multiple values and change the key name, so the final output looks like this: keyA | false keyB | 3 keyC-paid | 10 keyD-unpaid | 20 I know I can use UNNEST and JSON_EXTRACT multiple values and create additional but unsure how to combine them to adjust the key column name as well?
Even more generic approach create temp function extract_keys(input string) returns array<string> language js as """ return Object.keys(JSON.parse(input)); """; create temp function extract_values(input string) returns array<string> language js as """ return Object.values(JSON.parse(input)); """; create temp function extract_all_leaves(input string) returns string language js as ''' function flattenObj(obj, parent = '', res = {}){ for(let key in obj){ let propName = parent ? parent + '.' + key : key; if(typeof obj[key] == 'object'){ flattenObj(obj[key], propName, res); } else { res[propName] = obj[key]; } } return JSON.stringify(res); } return flattenObj(JSON.parse(input)); '''; select col || replace(replace(key, 'value', ''), '.', '-') as col, value, from your_table, unnest([struct(extract_all_leaves(data) as json)]), unnest(extract_keys(json)) key with offset join unnest(extract_values(json)) value with offset using(offset) if applied to sample data in your question - output is Benefit of this approach is that it is quite generic and thus can handle any level of nesting in json For example for below data/table the output is
Try this one: WITH sample AS ( SELECT 'keyA' AS col, '{"value":false}' AS data UNION ALL SELECT 'keyB' AS col, '{"value":3}' AS data UNION ALL SELECT 'keyC' AS col, '{"value":{"paid":10,"unpaid":20}}' AS data ) SELECT col || IFNULL('-' || k, '') AS col, IFNULL(v, JSON_VALUE(data, '$.value')) AS data FROM ( SELECT col, data, `bqutil.fn.json_extract_keys`(JSON_QUERY(data, '$.value')) AS keys, `bqutil.fn.json_extract_values`(JSON_QUERY(data, '$.value')) AS vals FROM sample ) LEFT JOIN UNNEST(keys) k WITH OFFSET ki LEFT JOIN UNNEST(vals) v WITH OFFSET vi ON ki = vi;
Laravel: Sort a column ASC but 0 come last
Using Laravel, to order by a column ascending, but 0 to come last. This is how we do in normal SQL: SELECT * FROM your_table ORDER BY your_field = 0, your_field; I want to know how to do in Laravel. Many thanks
Use orderByRaw() Assuming Your_Table model Your_Table::orderByRaw('your_field = 0, your_field')->get();
You can try orderByRaw function. for example : $data = new YourModel(); $data = $data->orderByRaw('your_field = 0', 'ASC', 'your_field')->get();
I think we can use DB::raw, so the usage maybe. DB::select( DB::raw( "SELECT * FROM your_table ORDER BY your_field = 0, your_field" ) );
return $this->your_model ->orderByRaw('display_order = 0, display_order ASC') ->get();
How calculate TRIMMEAN in SQL Server 2012?
How can I calculate the TRIMMEAN in SQL Server? Executing the TRIMMEAN in Excel for the example data below and percent of 0.35 my result is: 0.10 I've tried so many ways but not success. example data CREATE TABLE [dbo].[YourTable]( [Data] [decimal](18, 8) NOT NULL ); WITH Nums AS ( SELECT number FROM master..spt_values WHERE type='P' AND number > 0 ), Vals As ( SELECT * FROM (VALUES (2,0.03555556), (1,0.05777778), (1,0.05888889), (1,0.05916667), (2,0.05944444), (2,0.06000000), (3,0.06027778), (1,0.06055556), (2,0.06083333), (2,0.06111111), (2,0.06138889), (2,0.06166667), (2,0.06194444), (2,0.06222222), (3,0.06250000), (1,0.06277778), (2,0.06305556), (3,0.06361111), (7,0.06388889), (4,0.06416667), (3,0.06444444), (8,0.06472222), (4,0.06500000), (5,0.06527778), (3,0.06555556), (9,0.06583333), (8,0.06611111), (8,0.06638889), (5,0.06666667), (10,0.06694444), (6,0.06722222), (16,0.06750000), (13,0.06777778), (10,0.06805556), (4,0.06833333), (7,0.06861111), (10,0.06888889), (8,0.06916667), (10,0.06944444), (14,0.06972222), (14,0.07000000), (15,0.07027778), (15,0.07055556), (21,0.07083333), (19,0.07111111), (20,0.07138889), (20,0.07166667), (18,0.07194444), (24,0.07222222), (19,0.07250000), (17,0.07277778), (15,0.07305556), (23,0.07333333), (25,0.07361111), (26,0.07388889), (22,0.07416667), (22,0.07444444), (24,0.07472222), (19,0.07500000), (20,0.07527778), (29,0.07555556), (33,0.07583333), (16,0.07611111), (30,0.07638889), (20,0.07666667), (25,0.07694444), (37,0.07722222), (32,0.07750000), (25,0.07777778), (25,0.07805556), (34,0.07833333), (28,0.07861111), (22,0.07888889), (25,0.07916667), (27,0.07944444), (24,0.07972222), (28,0.08000000), (31,0.08027778), (34,0.08055556), (21,0.08083333), (23,0.08111111), (23,0.08138889), (18,0.08166667), (22,0.08194444), (18,0.08222222), (30,0.08250000), (26,0.08277778), (31,0.08305556), (24,0.08333333), (20,0.08361111), (21,0.08388889), (26,0.08416667), (29,0.08444444), (39,0.08472222), (20,0.08500000), (25,0.08527778), (28,0.08555556), (31,0.08583333), (29,0.08611111), (21,0.08638889), (16,0.08666667), (27,0.08694444), (21,0.08722222), (16,0.08750000), (23,0.08777778), (21,0.08805556), (24,0.08833333), (24,0.08861111), (21,0.08888889), (23,0.08916667), (23,0.08944444), (15,0.08972222), (21,0.09000000), (18,0.09027778), (19,0.09055556), (15,0.09083333), (19,0.09111111), (17,0.09138889), (18,0.09166667), (21,0.09194444), (14,0.09222222), (19,0.09250000), (21,0.09277778), (25,0.09305556), (18,0.09333333), (17,0.09361111), (20,0.09388889), (25,0.09416667), (18,0.09444444), (16,0.09472222), (21,0.09500000), (9,0.09527778), (16,0.09555556), (16,0.09583333), (16,0.09611111), (14,0.09638889), (15,0.09666667), (18,0.09694444), (15,0.09722222), (13,0.09750000), (20,0.09777778), (18,0.09805556), (19,0.09833333), (24,0.09861111), (17,0.09888889), (10,0.09916667), (10,0.09944444), (17,0.09972222), (16,0.10000000), (17,0.10027778), (15,0.10055556), (10,0.10083333), (17,0.10111111), (19,0.10138889), (14,0.10166667), (9,0.10194444), (15,0.10222222), (14,0.10250000), (15,0.10277778), (14,0.10305556), (7,0.10333333), (13,0.10361111), (19,0.10388889), (10,0.10416667), (9,0.10444444), (9,0.10472222), (16,0.10500000), (16,0.10527778), (8,0.10555556), (10,0.10583333), (15,0.10611111), (12,0.10638889), (10,0.10666667), (9,0.10694444), (12,0.10722222), (9,0.10750000), (17,0.10777778), (8,0.10805556), (14,0.10833333), (9,0.10861111), (9,0.10888889), (8,0.10916667), (13,0.10944444), (16,0.10972222), (10,0.11000000), (12,0.11027778), (10,0.11055556), (11,0.11083333), (9,0.11111111), (12,0.11138889), (10,0.11166667), (8,0.11194444), (15,0.11222222), (7,0.11250000), (9,0.11277778), (10,0.11305556), (7,0.11333333), (12,0.11361111), (8,0.11388889), (5,0.11416667), (13,0.11444444), (8,0.11472222), (9,0.11500000), (5,0.11527778), (6,0.11555556), (6,0.11583333), (9,0.11611111), (12,0.11638889), (4,0.11666667), (8,0.11694444), (7,0.11722222), (12,0.11750000), (9,0.11777778), (8,0.11805556), (8,0.11833333), (5,0.11861111), (7,0.11888889), (4,0.11916667), (5,0.11944444), (5,0.11972222), (11,0.12000000), (1,0.12027778), (8,0.12055556), (7,0.12083333), (8,0.12111111), (9,0.12138889), (7,0.12166667), (3,0.12194444), (11,0.12222222), (9,0.12250000), (8,0.12277778), (9,0.12305556), (4,0.12333333), (5,0.12361111), (3,0.12388889), (3,0.12416667), (13,0.12444444), (7,0.12472222), (8,0.12500000), (8,0.12527778), (5,0.12555556), (6,0.12583333), (6,0.12611111), (3,0.12638889), (6,0.12666667), (5,0.12694444), (9,0.12722222), (4,0.12750000), (6,0.12777778), (7,0.12805556), (8,0.12833333), (7,0.12861111), (8,0.12888889), (9,0.12916667), (8,0.12944444), (4,0.12972222), (2,0.13000000), (5,0.13027778), (3,0.13055556), (8,0.13083333), (4,0.13111111), (1,0.13138889), (3,0.13166667), (9,0.13194444), (12,0.13222222), (8,0.13250000), (4,0.13277778), (5,0.13305556), (4,0.13333333), (11,0.13361111), (7,0.13388889), (8,0.13416667), (2,0.13444444), (10,0.13472222), (5,0.13500000), (5,0.13527778), (5,0.13583333), (10,0.13611111), (2,0.13638889), (8,0.13666667), (3,0.13694444), (8,0.13722222), (5,0.13750000), (4,0.13777778), (5,0.13805556), (8,0.13833333), (2,0.13861111), (2,0.13888889), (7,0.13916667), (5,0.13972222), (10,0.14000000), (4,0.14027778), (4,0.14055556), (6,0.14083333), (5,0.14111111), (3,0.14138889), (5,0.14166667), (4,0.14194444), (7,0.14222222), (5,0.14250000), (6,0.14277778), (5,0.14305556), (7,0.14333333), (6,0.14361111), (9,0.14388889), (7,0.14416667), (3,0.14444444), (4,0.14472222), (5,0.14500000), (4,0.14527778), (7,0.14555556), (5,0.14583333), (4,0.14611111), (5,0.14638889), (4,0.14666667), (2,0.14694444), (8,0.14722222), (5,0.14750000), (3,0.14777778), (5,0.14805556), (6,0.14833333), (3,0.14861111), (3,0.14888889), (8,0.14916667), (4,0.14944444), (6,0.14972222), (5,0.15000000), (4,0.15027778), (3,0.15055556), (1,0.15083333), (1,0.15111111), (3,0.15138889), (6,0.15166667), (2,0.15194444), (3,0.15222222), (2,0.15250000), (2,0.15277778), (1,0.15305556), (2,0.15333333), (2,0.15361111), (2,0.15388889), (1,0.15416667), (8,0.15444444), (5,0.15472222), (2,0.15500000), (1,0.15527778), (4,0.15555556), (2,0.15583333), (3,0.15611111), (1,0.15666667), (1,0.15722222), (2,0.15750000), (3,0.15777778), (2,0.15805556), (3,0.15833333), (2,0.15861111), (4,0.15888889), (3,0.15916667), (2,0.15944444), (3,0.15972222), (4,0.16000000), (1,0.16055556), (2,0.16083333), (4,0.16111111), (4,0.16138889), (4,0.16166667), (2,0.16194444), (5,0.16222222), (1,0.16250000), (3,0.16333333), (1,0.16388889), (2,0.16416667), (1,0.16444444), (1,0.16472222), (1,0.16500000), (2,0.16527778), (1,0.16555556), (1,0.16611111), (1,0.16638889), (4,0.16666667), (3,0.16694444), (3,0.16722222), (1,0.16750000), (1,0.16777778), (2,0.16833333), (4,0.16861111), (2,0.16888889), (1,0.16916667), (1,0.16972222), (2,0.17000000), (2,0.17027778), (3,0.17055556), (2,0.17083333), (2,0.17111111), (3,0.17138889), (2,0.17166667), (1,0.17194444), (2,0.17222222), (2,0.17250000), (5,0.17277778), (5,0.17305556), (3,0.17333333), (2,0.17361111), (3,0.17388889), (3,0.17416667), (2,0.17444444), (2,0.17472222), (1,0.17527778), (3,0.17555556), (1,0.17583333), (2,0.17611111), (1,0.17666667), (3,0.17722222), (3,0.17750000), (1,0.17777778), (1,0.17805556), (3,0.17833333), (5,0.17861111), (4,0.17888889), (2,0.17916667), (1,0.17944444), (2,0.18000000), (1,0.18027778), (2,0.18055556), (2,0.18083333), (1,0.18111111), (2,0.18138889), (4,0.18166667), (2,0.18194444), (1,0.18222222), (1,0.18250000), (1,0.18277778), (2,0.18361111), (1,0.18388889), (1,0.18416667), (3,0.18444444), (2,0.18472222), (4,0.18500000), (2,0.18527778), (2,0.18583333), (3,0.18611111), (1,0.18638889), (1,0.18666667), (1,0.18694444), (2,0.18722222), (1,0.18861111), (4,0.18888889), (1,0.18916667), (1,0.18944444), (1,0.18972222), (2,0.19000000), (1,0.19027778), (1,0.19083333), (1,0.19111111), (1,0.19194444), (2,0.19222222), (1,0.19250000), (1,0.19361111), (1,0.19388889), (1,0.19416667), (1,0.19444444), (1,0.19472222), (1,0.19500000), (1,0.19555556), (3,0.19583333), (2,0.19638889), (3,0.19666667), (2,0.19722222), (1,0.19777778), (2,0.19805556), (1,0.19833333), (2,0.19861111), (2,0.19888889), (1,0.19916667), (1,0.19944444), (2,0.20027778), (1,0.20055556), (2,0.20083333), (2,0.20138889), (1,0.20194444), (2,0.20222222), (1,0.20250000), (1,0.20277778), (1,0.20333333), (1,0.20361111), (1,0.20388889), (1,0.20444444), (1,0.20500000), (2,0.20527778), (3,0.20555556), (1,0.20583333), (1,0.20611111), (3,0.20638889), (2,0.20666667), (2,0.20694444), (1,0.20750000), (1,0.20805556), (3,0.20833333), (1,0.20888889), (1,0.20916667), (1,0.20944444), (2,0.20972222), (1,0.21000000), (1,0.21027778), (1,0.21055556), (2,0.21083333), (1,0.21111111), (2,0.21138889), (2,0.21166667), (1,0.21194444), (3,0.21250000), (2,0.21277778), (1,0.21305556), (2,0.21333333), (2,0.21361111), (2,0.21388889), (2,0.21416667), (1,0.21472222), (1,0.21500000), (1,0.21694444), (1,0.21750000), (4,0.21777778), (2,0.21861111), (1,0.21888889), (3,0.21916667), (1,0.21944444), (2,0.21972222), (1,0.22000000), (1,0.22027778), (1,0.22055556), (1,0.22083333), (1,0.22111111), (1,0.22138889), (1,0.22166667), (1,0.22194444), (3,0.22222222), (1,0.22277778), (3,0.22333333), (1,0.22416667), (1,0.22555556), (2,0.22583333), (1,0.22666667), (1,0.22722222), (1,0.22750000), (5,0.22777778), (1,0.22805556), (2,0.22833333), (3,0.22916667), (1,0.22944444), (1,0.22972222), (1,0.23000000), (2,0.23027778), (1,0.23083333), (1,0.23166667), (1,0.23444444), (1,0.23500000), (1,0.23583333), (1,0.23638889), (2,0.23694444), (1,0.23722222), (1,0.23750000), (1,0.23805556), (1,0.23861111), (2,0.23888889), (1,0.23916667), (1,0.23944444), (1,0.23972222), (2,0.24000000), (2,0.24055556), (3,0.24083333), (1,0.24166667), (1,0.24194444), (1,0.24250000), (2,0.24388889), (1,0.24416667), (1,0.24555556), (1,0.24583333), (4,0.24611111), (2,0.24777778), (1,0.24833333), (1,0.24916667), (1,0.24972222), (1,0.25000000), (1,0.25083333), (1,0.25138889), (1,0.25166667), (2,0.25222222), (3,0.25250000), (1,0.25305556), (2,0.25361111), (2,0.25416667), (1,0.25444444), (2,0.25555556), (2,0.25638889), (1,0.25666667), (1,0.25777778), (1,0.25805556), (1,0.25833333), (2,0.25861111), (1,0.25916667), (1,0.25944444), (1,0.26000000), (2,0.26111111), (1,0.26138889), (2,0.26194444), (3,0.26222222), (2,0.26277778), (2,0.26388889), (1,0.26416667), (1,0.26444444), (1,0.26472222), (1,0.26583333), (1,0.26611111), (4,0.26638889), (1,0.26694444), (1,0.26750000), (1,0.26777778), (1,0.26861111), (1,0.26888889), (1,0.26944444), (1,0.26972222), (1,0.27027778), (1,0.27083333), (1,0.27111111), (1,0.27166667), (1,0.27361111), (1,0.27527778), (1,0.27666667), (1,0.27694444), (2,0.27722222), (1,0.27750000), (1,0.27805556), (1,0.27833333), (1,0.28000000), (2,0.28027778), (1,0.28055556), (2,0.28083333), (1,0.28166667), (1,0.28194444), (1,0.28277778), (1,0.28305556), (1,0.28388889), (1,0.28555556), (1,0.28611111), (1,0.28638889), (1,0.28694444), (1,0.28750000), (1,0.28777778), (1,0.28805556), (1,0.28861111), (1,0.28888889), (1,0.28944444), (1,0.29083333), (1,0.29111111), (1,0.29222222), (1,0.29250000), (1,0.29277778), (1,0.29388889), (1,0.29416667), (1,0.29444444), (1,0.29472222), (1,0.29500000), (2,0.29583333), (2,0.29611111), (2,0.29638889), (1,0.29666667), (1,0.29694444), (1,0.29750000), (1,0.29805556), (1,0.30000000), (1,0.30027778), (1,0.30055556), (2,0.30111111), (2,0.30166667), (1,0.30222222), (1,0.30250000), (2,0.30277778), (1,0.30361111), (2,0.30444444), (2,0.30500000), (1,0.30555556), (1,0.30583333), (1,0.30638889), (1,0.30694444), (1,0.30722222), (1,0.30777778), (1,0.30888889), (2,0.31000000), (1,0.31027778), (2,0.31055556), (1,0.31138889), (2,0.31222222), (1,0.31250000), (1,0.31388889), (1,0.31416667), (2,0.31527778), (1,0.31583333), (2,0.31638889), (1,0.31722222), (1,0.31777778), (1,0.31861111), (1,0.31888889), (1,0.32027778), (1,0.32083333), (1,0.32111111), (1,0.32222222), (1,0.32277778), (1,0.32388889), (2,0.32416667), (1,0.32444444), (1,0.32527778), (1,0.32583333), (1,0.32638889), (1,0.32694444), (1,0.32805556), (2,0.32833333), (1,0.32888889), (1,0.32972222), (1,0.33055556), (1,0.33111111), (1,0.33166667), (1,0.33333333), (1,0.33527778), (1,0.33611111), (2,0.33638889), (1,0.33666667), (3,0.33833333), (1,0.34000000), (1,0.34027778), (1,0.34166667), (1,0.34333333), (1,0.34388889), (1,0.34416667), (1,0.34444444), (1,0.34472222), (2,0.34555556), (1,0.34638889), (1,0.34666667), (1,0.34750000), (1,0.34777778), (1,0.34805556), (1,0.34833333), (1,0.34972222), (1,0.35055556), (1,0.35083333), (1,0.35111111), (1,0.35194444), (1,0.35277778), (1,0.35361111), (2,0.35444444), (1,0.35583333), (1,0.35777778), (1,0.35833333), (1,0.35916667), (1,0.36111111), (1,0.36222222), (1,0.36305556), (2,0.36555556), (1,0.36694444), (2,0.36722222), (1,0.36777778), (1,0.36944444), (1,0.37027778), (1,0.37111111), (1,0.37194444), (1,0.37416667), (1,0.37666667), (1,0.37750000), (1,0.37805556), (1,0.38222222), (1,0.38416667), (1,0.38555556), (1,0.38666667), (1,0.38805556), (1,0.39166667), (1,0.39194444), (1,0.39416667), (1,0.39666667), (1,0.39694444), (1,0.40055556), (1,0.40166667), (1,0.40388889), (1,0.40416667), (1,0.40444444), (1,0.40750000), (1,0.40805556), (1,0.41250000), (2,0.41361111), (1,0.41722222), (1,0.41916667), (1,0.42027778), (1,0.42194444), (1,0.42361111), (1,0.42388889), (1,0.42972222), (1,0.43222222), (1,0.43277778), (1,0.43750000), (1,0.43777778), (1,0.43833333), (1,0.44611111), (1,0.44638889), (1,0.44694444), (1,0.45027778), (1,0.45166667), (1,0.45500000), (1,0.45833333), (1,0.46083333), (1,0.46555556), (1,0.46722222), (2,0.46888889), (1,0.47083333), (1,0.47111111), (1,0.47416667), (1,0.47500000), (1,0.47694444), (2,0.48000000), (2,0.48027778), (1,0.48083333), (1,0.48111111), (1,0.48500000), (1,0.48611111), (1,0.49444444), (1,0.49555556), (1,0.50000000), (1,0.51000000), (1,0.51472222), (1,0.51916667), (1,0.52638889), (1,0.52833333), (1,0.53861111), (1,0.55055556), (1,0.55722222), (1,0.56111111), (1,0.56750000), (1,0.56833333), (1,0.56944444), (1,0.59500000), (1,0.59861111), (1,0.60472222), (1,0.60750000), (1,0.61000000), (1,0.61027778), (1,0.61138889), (1,0.61888889), (1,0.62472222), (1,0.62916667), (1,0.63000000), (1,0.63055556), (1,0.63138889), (1,0.63527778), (1,0.63666667), (1,0.64277778), (1,0.64666667), (1,0.66333333), (1,0.67277778), (1,0.68500000), (1,0.69083333), (1,0.69916667), (1,0.70166667), (1,0.71833333), (1,0.72527778), (1,0.72638889), (1,0.73861111), (2,0.73944444), (1,0.75944444), (1,0.76166667), (1,0.76194444), (1,0.77500000), (1,0.77888889), (1,0.78888889), (1,0.80083333), (1,0.80416667), (1,0.80611111), (2,0.80638889), (1,0.81027778), (1,0.81277778), (1,0.81722222), (1,0.81833333), (1,0.83138889), (1,0.84555556), (1,0.84833333), (1,0.85138889), (1,0.85277778), (1,0.85472222), (1,0.86444444), (1,0.86527778), (1,0.90027778), (1,0.90500000), (1,0.92305556), (1,0.93361111), (1,0.93527778), (1,0.94861111), (2,0.95055556), (1,0.95222222), (1,0.96000000), (1,0.96472222), (1,0.96527778), (1,0.97472222), (1,0.98166667), (1,1.00555556), (1,1.00666667), (1,1.00777778), (1,1.01250000), (1,1.01638889), (1,1.01861111), (1,1.02111111), (1,1.02305556), (1,1.02500000), (1,1.02666667), (1,1.02694444), (1,1.02805556), (1,1.03000000), (2,1.03638889), (1,1.04111111), (1,1.04222222), (1,1.04527778), (1,1.04583333), (1,1.04750000), (1,1.04805556), (1,1.04944444), (1,1.05138889), (1,1.05583333), (1,1.05638889), (1,1.05944444), (1,1.05972222), (1,1.06138889), (1,1.06250000), (1,1.06305556), (1,1.06500000), (1,1.06666667), (1,1.06750000), (1,1.07083333), (1,1.07277778), (1,1.07388889), (1,1.08361111), (1,1.08666667), (1,1.09055556), (1,1.09222222), (1,1.09805556), (1,1.11527778), (1,1.11805556), (1,1.12777778), (1,1.13222222), (1,1.13250000), (1,1.13361111), (1,1.13388889), (1,1.15027778), (1,1.15194444), (1,1.15444444), (1,1.15916667), (1,1.16111111), (1,1.16666667), (1,1.16750000), (1,1.17222222), (1,1.17694444), (1,1.18166667), (1,1.19305556), (1,1.20138889), (1,1.23777778), (1,1.27861111), (1,1.29916667), (1,1.33638889), (1,1.39750000), (1,1.42250000), (1,1.42611111), (1,1.50111111), (1,1.61583333), (1,1.62166667), (1,1.62500000), (1,1.63361111), (1,1.65916667), (1,1.68666667), (1,1.68722222), (1,1.71638889), (1,1.71861111), (1,2.10222222), (1,2.12611111), (1,2.19416667), (1,2.20055556), (1,2.53527778), (1,2.74638889), (1,2.79361111), (1,2.99055556), (1,3.06277778), (1,4.24166667), (1,5.93750000), (1,8.91694444), (1,13.53277778), (1,14.47305556), (1,14.52361111), (1,14.55805556), (1,14.56611111), (1,14.57138889), (1,14.60777778), (1,14.60972222), (1,14.61722222), (1,14.61888889), (1,14.63277778), (1,14.64305556), (1,14.66527778), (1,14.75722222), (1,14.76000000), (1,14.91777778), (1,14.92305556), (1,15.09722222), (1,16.23472222), (1,16.24388889), (1,16.24722222), (1,16.29055556), (1,16.29333333), (1,59.44916667), (1,59.71000000), (1,59.71583333), (1,59.82138889), (1,60.05555556), (1,61.05055556), (1,61.05694444), (1,61.11000000), (1,61.11222222), (1,61.11527778), (1,61.11555556), (1,61.12750000), (1,61.12972222), (1,61.13000000), (1,62.50416667), (1,62.51166667), (1,62.52833333), (1,62.55138889), (1,62.55750000), (1,62.56861111), (1,62.58777778), (1,62.58833333), (1,62.59472222), (1,62.60194444), (1,62.61416667), (1,62.64250000), (1,62.68222222), (1,62.68805556), (1,63.12638889), (1,63.15666667), (1,63.27472222) ) V(Count,Data)) INSERT INTO [dbo].[YourTable] SELECT Data FROM Vals JOIN Nums ON number <= Count
This is a simplified version of #MartinSmith's answer which avoids calculating two ROW_NUMBERs with different order: WITH T AS ( SELECT [Data], COUNT(*) OVER() AS cnt, CAST(#Percent * COUNT(*) OVER() AS INT)/2 AS NumToExclude, ROW_NUMBER() OVER (ORDER BY [Data] ASC) AS RN FROM [dbo].[YourTable] WHERE [Data] IS NOT NULL ) SELECT AVG([Data]) FROM T WHERE RN > NumToExclude AND RN <= cnt - NumToExclude;
The documentation states TRIMMEAN(array, percent) The TRIMMEAN function syntax has the following arguments: Array Required. The array or range of values to trim and average. Percent Required. The fractional number of data points to exclude from the calculation. For example, if percent = 0.2, 4 points are trimmed from a data set of 20 points (20 x 0.2): 2 from the top and 2 from the bottom of the set. TRIMMEAN rounds the number of excluded data points down to the nearest multiple of 2. If percent = 0.1, 10 percent of 30 data points equals 3 points. For symmetry, TRIMMEAN excludes a single value from the top and bottom of the data set. Something like the following should simulate the Excel function (demo). For 0.2 the query returns 0.11107474 which is the same as Excel (and 0.35 returns 0,10455721 which is also the same). DECLARE #Percent DECIMAL(2,2) = 0.2; WITH T AS ( SELECT [Data], CAST(#Percent * COUNT(*) OVER() AS INT)/2 AS NumToExclude, ROW_NUMBER() OVER (ORDER BY [Data] ASC) AS RNAsc, ROW_NUMBER() OVER (ORDER BY [Data] DESC) AS RNDesc FROM [dbo].[YourTable] WHERE [Data] IS NOT NULL ) SELECT AVG([Data]) FROM T WHERE RNAsc > NumToExclude AND RNDesc > NumToExclude;
Update multiple rows in same query using PostgreSQL
I'm looking to update multiple rows in PostgreSQL in one statement. Is there a way to do something like the following? UPDATE table SET column_a = 1 where column_b = '123', column_a = 2 where column_b = '345'
You can also use update ... from syntax and use a mapping table. If you want to update more than one column, it's much more generalizable: update test as t set column_a = c.column_a from (values ('123', 1), ('345', 2) ) as c(column_b, column_a) where c.column_b = t.column_b; You can add as many columns as you like: update test as t set column_a = c.column_a, column_c = c.column_c from (values ('123', 1, '---'), ('345', 2, '+++') ) as c(column_b, column_a, column_c) where c.column_b = t.column_b; sql fiddle demo
Based on the solution of #Roman, you can set multiple values: update users as u set -- postgres FTW email = u2.email, first_name = u2.first_name, last_name = u2.last_name from (values (1, 'hollis#weimann.biz', 'Hollis', 'Connell'), (2, 'robert#duncan.info', 'Robert', 'Duncan') ) as u2(id, email, first_name, last_name) where u2.id = u.id;
Yes, you can: UPDATE foobar SET column_a = CASE WHEN column_b = '123' THEN 1 WHEN column_b = '345' THEN 2 END WHERE column_b IN ('123','345') And working proof: http://sqlfiddle.com/#!2/97c7ea/1
For updating multiple rows in a single query, you can try this UPDATE table_name SET column_1 = CASE WHEN any_column = value and any_column = value THEN column_1_value end, column_2 = CASE WHEN any_column = value and any_column = value THEN column_2_value end, column_3 = CASE WHEN any_column = value and any_column = value THEN column_3_value end, . . . column_n = CASE WHEN any_column = value and any_column = value THEN column_n_value end if you don't need additional condition then remove and part of this query
Let's say you have an array of IDs and equivalent array of statuses - here is an example how to do this with a static SQL (a sql query that doesn't change due to different values) of the arrays : drop table if exists results_dummy; create table results_dummy (id int, status text, created_at timestamp default now(), updated_at timestamp default now()); -- populate table with dummy rows insert into results_dummy (id, status) select unnest(array[1,2,3,4,5]::int[]) as id, unnest(array['a','b','c','d','e']::text[]) as status; select * from results_dummy; -- THE update of multiple rows with/by different values update results_dummy as rd set status=new.status, updated_at=now() from (select unnest(array[1,2,5]::int[]) as id,unnest(array['a`','b`','e`']::text[]) as status) as new where rd.id=new.id; select * from results_dummy; -- in code using **IDs** as first bind variable and **statuses** as the second bind variable: update results_dummy as rd set status=new.status, updated_at=now() from (select unnest(:1::int[]) as id,unnest(:2::text[]) as status) as new where rd.id=new.id;
Came across similar scenario and the CASE expression was useful to me. UPDATE reports SET is_default = case when report_id = 123 then true when report_id != 123 then false end WHERE account_id = 321; Reports - is a table here, account_id is same for the report_ids mentioned above. The above query will set 1 record (the one which matches the condition) to true and all the non-matching ones to false.
The answer provided by #zero323 works great on Postgre 12. In case, someone has multiple values for column_b (referred in OP's question) UPDATE conupdate SET orientation_status = CASE when id in (66934, 39) then 66 when id in (66938, 49) then 77 END WHERE id IN (66934, 39, 66938, 49) In the above query, id is analogous to column_b; orientation_status is analogous to column_a of the question.
In addition to other answers, comments and documentation, the datatype cast can be placed on usage. This allows an easier copypasting: update test as t set column_a = c.column_a::number from (values ('123', 1), ('345', 2) ) as c(column_b, column_a) where t.column_b = c.column_b::text;
#Roman thank you for the solution, for anyone using node, I made this utility method to pump out a query string to update n columns with n records. Sadly it only handles n records with the same columns so the recordRows param is pretty strict. const payload = { rows: [ { id: 1, ext_id: 3 }, { id: 2, ext_id: 3 }, { id: 3, ext_id: 3 } , { id: 4, ext_id: 3 } ] }; var result = updateMultiple('t', payload); console.log(result); /* qstring returned is: UPDATE t AS t SET id = c.id, ext_id = c.ext_id FROM (VALUES (1,3),(2,3),(3,3),(4,3)) AS c(id,ext_id) WHERE c.id = t.id */ function updateMultiple(table, recordRows){ var valueSets = new Array(); var cSet = new Set(); var columns = new Array(); for (const [key, value] of Object.entries(recordRows.rows)) { var groupArray = new Array(); for ( const [key2, value2] of Object.entries(recordRows.rows[key])){ if(!cSet.has(key2)){ cSet.add(`${key2}`); columns.push(key2); } groupArray.push(`${value2}`); } valueSets.push(`(${groupArray.toString()})`); } var valueSetsString = valueSets.join(); var setMappings = new String(); for(var i = 0; i < columns.length; i++){ var fieldSet = columns[i]; setMappings += `${fieldSet} = c.${fieldSet}`; if(i < columns.length -1){ setMappings += ', '; } } var qstring = `UPDATE ${table} AS t SET ${setMappings} FROM (VALUES ${valueSetsString}) AS c(${columns}) WHERE c.id = t.id`; return qstring; }
I don't think the accepted answer is entirely correct. It is order dependent. Here is an example that will not work correctly with an approach from the answer. create table xxx ( id varchar(64), is_enabled boolean ); insert into xxx (id, is_enabled) values ('1',true); insert into xxx (id, is_enabled) values ('2',true); insert into xxx (id, is_enabled) values ('3',true); UPDATE public.xxx AS pns SET is_enabled = u.is_enabled FROM ( VALUES ( '3', false , '1', true , '2', false ) ) AS u(id, is_enabled) WHERE u.id = pns.id; select * from xxx; So the question still stands, is there a way to do it in an order independent way? ---- after trying a few things this seems to be order independent UPDATE public.xxx AS pns SET is_enabled = u.is_enabled FROM ( SELECT '3' as id, false as is_enabled UNION SELECT '1' as id, true as is_enabled UNION SELECT '2' as id, false as is_enabled ) as u WHERE u.id = pns.id;
How To Split Pipe-Delimited Column and insert each value into new table Once?
I have an old database with a gazillion records (more or less) that have a single tags column (with tags being pipe-delimited) that looks like so: Breakfast Breakfast|Brunch|Buffet|Burger|Cakes|Crepes|Deli|Dessert|Dim Sum|Fast Food|Fine Wine|Spirits|Kebab|Noodles|Organic|Pizza|Salad|Seafood|Steakhouse|Sushi|Tapas|Vegetarian Breakfast|Brunch|Buffet|Burger|Deli|Dessert|Fast Food|Fine Wine|Spirits|Noodles|Pizza|Salad|Seafood|Steakhouse|Vegetarian Breakfast|Brunch|Buffet|Cakes|Crepes|Dessert|Fine Wine|Spirits|Salad|Seafood|Steakhouse|Tapas|Teahouse Breakfast|Brunch|Burger|Crepes|Salad Breakfast|Brunch|Cakes|Dessert|Dim Sum|Noodles|Pizza|Salad|Seafood|Steakhouse|Vegetarian Breakfast|Brunch|Cakes|Dessert|Dim Sum|Noodles|Pizza|Salad|Seafood|Vegetarian Breakfast|Brunch|Deli|Dessert|Organic|Salad Breakfast|Brunch|Dessert|Dim Sum|Hot Pot|Seafood Breakfast|Brunch|Dessert|Dim Sum|Seafood Breakfast|Brunch|Dessert|Fine Wine|Spirits|Noodles|Pizza|Salad|Seafood Breakfast|Brunch|Dessert|Fine Wine|Spirits|Salad|Vegetarian Is there a way one could retrieve each tag and insert it into a new table tag_id | tag_nm using MySQL only?
Here is my attempt which uses PHP..., I imagine this could be more efficient with a clever MySQL query. I've placed the relationship part of it there too. There's no escaping and error checking. $rs = mysql_query('SELECT `venue_id`, `tag` FROM `venue` AS a'); while ($row = mysql_fetch_array($rs)) { $tag_array = explode('|',$row['tag']); $venueid = $row['venue_id']; foreach ($tag_array as $tag) { $rs2 = mysql_query("SELECT `tag_id` FROM `tag` WHERE tag_nm = '$tag'"); $tagid = 0; while ($row2 = mysql_fetch_array($rs2)) $tagid = $row2['tag_id']; if (!$tagid) { mysql_execute("INSERT INTO `tag` (`tag_nm`) VALUES ('$tag')"); $tagid = mysql_insert_id; } mysql_execute("INSERT INTO `venue_tag_rel` (`venue_id`, `tag_id`) VALUES ($venueid, $tagid)"); } }
After finding there is no official split function I've solved the issue using only MySQL like so: 1: I created the function strSplit CREATE FUNCTION strSplit(x varchar(21845), delim varchar(255), pos int) returns varchar(255) return replace( replace( substring_index(x, delim, pos), substring_index(x, delim, pos - 1), '' ), delim, '' ); Second I inserted the new tags into my new table (real names and collumns changed, to keep it simple) INSERT IGNORE INTO tag (SELECT null, strSplit(`Tag`,'|',1) AS T FROM `old_venue` GROUP BY T) Rinse and repeat increasing the pos by one for each collumn (in this case I had a maximum of 8 seperators) Third to get the relationship INSERT INTO `venue_tag_rel` (Select a.`venue_id`, b.`tag_id` from `old_venue` a, `tag` b WHERE ( a.`Tag` LIKE CONCAT('%|',b.`tag_nm`) OR a.`Tag` LIKE CONCAT(b.`tag_nm`,'|%') OR a.`Tag` LIKE CONCAT(CONCAT('%|',b.`tag_nm`),'|%') OR a.`Tag` LIKE b.`tag_nm` ) )