Search By Label
-- Selecting and unnesting an array of integers SELECT unnest(ARRAY[1, 2]); -- Expands the array [1, 2] into individual rows
SUM
and get decimals complementing with numeric
in the following way:select us.product_id, ROUND(SUM(us.units * p.price)::numeric / SUM(us.units), 2) AS average_price from Prices p left join UnitsSold us on us.product_id = p.product_id and us.purchase_date between p.start_date and p.end_date group by us.product_id order by us.product_id
view
s and which are table
s:SELECT * FROM information_schema.tables
heroku pg:backups:capture -a your-production-name-app
pg_restore --verbose --clean --no-acl --no-owner -h localhost -d <data-base-name> /Users/juanequex/backups/<your-local-backup>
-- Assigning Values to Variables DECLARE @variable_name data_type; SELECT column_name INTO @variable_name FROM table_name WHERE condition;-- Creating a New Table
SELECT column1, column2 INTO new_table FROM old_table WHERE condition;-- Inserting Data into an Existing Table
INSERT INTO existing_table (column1, column2)
SELECT column1, column2 FROM source_table WHERE condition;
-- set a global variable SET my_var = 42;-- use the global variable
SELECT * FROM my_table WHERE column = my_var;-- unset the global variable RESET my_var;
-- Let's say we update some data in a table. UPDATE my_table SET column1 = 'new_value' WHERE id = 42;-- Now, we want to know how many rows got changed and if there's a unique ID for the last row.
GET DIAGNOSTICS rows_affected = ROW_COUNT, last_oid = RESULT_OID;-- We can print these values to see what happened.
RAISE NOTICE 'Rows affected: %, Last OID: %', rows_affected, last_oid;
CREATE OR REPLACE FUNCTION example_function() RETURNS void AS $$ BEGIN -- Some code here -- Raise a notice message RAISE NOTICE 'This is a notice message: %', 'Additional data'; -- More code here END; $$ LANGUAGE plpgsql;
AVG(column_name)
ROUND(numeric_expression, number_of_decimal_places)
expresion operator ANY(subquery)
SELECT 3 = ANY (ARRAY[1, 2, 3, 4]); -- Returns TRUE SELECT 42 = ANY (SELECT column FROM some_table); -- Returns TRUE if 42 exists in the column SELECT 'apple' = ANY (VALUES ('banana'), ('apple'), ('cherry')); -- Returns TRUE
SELECT COALESCE(null, 42, 'Hello'); -- Returns 42
REPLACE(original_string, search_string, replacement_string)
or
SELECT REPLACE('Hello, World!', 'World', 'Universe'); -- Returns 'Hello, Universe!'
SELECT CEIL(5.2); -- Returns 6
SELECT CEIL(-3.8); -- Returns -3
SELECT CEIL(10); -- Returns 10 (no change as it's already an integer)