plpgsql

SQL to merge rows

吃可爱长大的小学妹 提交于 2020-01-25 12:13:26
问题 Update How I can select the rows from a table that when ordered the first element matches some row the second element matches the next row the third element the next row after the second row the forth element the next row after the third row and so on until the end of values in array? The Logic Supposing I have these rows as result of a query (table token holds id and word and table positioning holds id and position ): id | word | textblockid |sentence |position 5 | Fear | 5 | 1 | 1 8 | of |

SQL to merge rows

佐手、 提交于 2020-01-25 12:13:20
问题 Update How I can select the rows from a table that when ordered the first element matches some row the second element matches the next row the third element the next row after the second row the forth element the next row after the third row and so on until the end of values in array? The Logic Supposing I have these rows as result of a query (table token holds id and word and table positioning holds id and position ): id | word | textblockid |sentence |position 5 | Fear | 5 | 1 | 1 8 | of |

Access dynamic column name of row type in trigger function

巧了我就是萌 提交于 2020-01-24 21:17:27
问题 I am trying to create a dynamic function to use for setting up triggers. CREATE OR REPLACE FUNCTION device_bid_modifiers_count_per() RETURNS TRIGGER AS $$ DECLARE devices_count INTEGER; table_name regclass := TG_ARGV[0]; column_name VARCHAR := TG_ARGV[1]; BEGIN LOCK TABLE device_types IN EXCLUSIVE MODE; EXECUTE format('LOCK TABLE %s IN EXCLUSIVE MODE', table_name); SELECT INTO devices_count device_types_count(); IF TG_OP = 'DELETE' THEN SELECT format( 'PERFORM validate_bid_modifiers_count(%s,

Understanding difference between int literal vs int parameter in PL/pgSQL function

妖精的绣舞 提交于 2020-01-24 20:42:23
问题 I have a function to left pad bit stings in PostgreSQL 9.5: CREATE OR REPLACE FUNCTION lpad_bits(val bit varying) RETURNS bit varying as $BODY$ BEGIN return val::bit(32) >> (32-length(val)); END; $BODY$ LANGUAGE plpgsql IMMUTABLE; which works fine: # select lpad_bits(b'1001100111000'); lpad_bits ---------------------------------- 00000000000000000001001100111000 (1 row) My problem is when I try to add a parameter to change the amount of padding: CREATE OR REPLACE FUNCTION lpad_bits(val bit

Error in dynamic statement PL/PGSQL (functions and operators can take at most one set argument)

萝らか妹 提交于 2020-01-24 14:38:28
问题 I am trying to execute the code below. I need this because, i wanted to pass the table names as parameters ( character varying ): EXECUTE 'CREATE TABLE '||tmp_table||' AS SELECT study, '||unnest(columns)||' AS variable_id, '||unnest_table(tblName)|| ' AS variable_value FROM '||tblName; However, I run into the error below: functions and operators can take at most one set argument Any ideas on how to solve this? Here is the code for the unnest_table function: CREATE OR REPLACE FUNCTION unnest

PostgreSQL custom week number - first week containing Feb 1st

无人久伴 提交于 2020-01-24 09:51:31
问题 I'm new to SQL functions and trying to create a calendar table that displays custom week numbers, with each week starting with Saturday and ending on Friday. The first week of each year always contains Feb.1st of that year. For example, if the day of the week of Feb. 1st for a particular year is Tuesday, then the first week for that year is from Jan. 29 to Feb. 4 . I've been struggling with this problem for a couple days and the only solution I can come up with is as follows: First, I created

PostgreSQL custom week number - first week containing Feb 1st

时光怂恿深爱的人放手 提交于 2020-01-24 09:51:04
问题 I'm new to SQL functions and trying to create a calendar table that displays custom week numbers, with each week starting with Saturday and ending on Friday. The first week of each year always contains Feb.1st of that year. For example, if the day of the week of Feb. 1st for a particular year is Tuesday, then the first week for that year is from Jan. 29 to Feb. 4 . I've been struggling with this problem for a couple days and the only solution I can come up with is as follows: First, I created

plpgsql Select statement in For loop to create multiple CSV files

无人久伴 提交于 2020-01-24 07:02:07
问题 I would like to repeat the following query 8760 times, replacing ‘2’ with 1 to 8760 for every hour in the year. The idea is to create a separate CSV file for each hour for further processing. COPY (SELECT * FROM public.completedsolarirad2012 WHERE completedsolarirad2012."UniquetmstmpID" = 2) TO 'C:\temp\2012hour2.csv' WITH DELIMITER ',' CSV HEADER I have put together the following function (testing with only a few hours): CREATE OR REPLACE FUNCTION everyhour() RETURNS void AS $BODY$BEGIN FOR

Postgres function: return multiple tables

非 Y 不嫁゛ 提交于 2020-01-23 12:11:35
问题 Is it possible to return several result sets of different types from postgres function? Something like: CREATE OR REPLACE FUNCTION getUserById() RETURNS setof ??? AS $$ BEGIN return query select id, name /* and other columns */ from users where id = 1; return query select id, phone_number from user_phones where user_id = 1 END $$ LANGUAGE plpgsql; I don't want to use joins because several phones for user are possible. Also it would be great to avoid using cursors. It's possible in MS SQL and

Rollback Transaction on Trigger ERROR

安稳与你 提交于 2020-01-23 02:38:11
问题 I'm trying to check if the room that is going to be inserted in the system is already rented at that date or not. I've though about counting the rows that match both the room number and the date, and then rolling back the transaction. But I'm getting the following error, even though I have changed the code to raise user-defined exceptions: ERROR: cannot begin/end transactions in PL/pgSQL HINT: Use a BEGIN block with an EXCEPTION clause instead. CONTEXT: PL/pgSQL function "checkRoom"() line 17