I have the table as like below.
CREATE TABLE my.categories (id bigint, parent_id bigint, name varchar(128));
INSERT INTO my.categories (id, parent_id, name) VALUES (1, null, 'LEVEL 1');
INSERT INTO my.categories (id, parent_id, name) VALUES (2, 1, 'LEVEL 2.1');
INSERT INTO my.categories (id, parent_id, name) VALUES (3, 1, 'LEVEL 2.2');
INSERT INTO my.categories (id, parent_id, name) VALUES (4, 2, 'LEVEL 3.1.1');
INSERT INTO my.categories (id, parent_id, name) VALUES (5, 2, 'LEVEL 3.1.2');
INSERT INTO my.categories (id, parent_id, name) VALUES (6, 3, 'LEVEL 3.2.1');
+----+-----------+---------------+
| id | parent_id | name |
+----+-----------+---------------+
| 1 | null | 'LEVEL 1' |
| 2 | 1 | 'LEVEL 2.1' |
| 3 | 1 | 'LEVEL 2.2' |
| 4 | 2 | 'LEVEL 3.1.1' |
| 5 | 2 | 'LEVEL 3.1.2' |
| 6 | 3 | 'LEVEL 3.2.1' |
+----+-----------+---------------+
I need to get all id's for parent categories.
WITH RECURSIVE tree(theId) AS (
SELECT id
FROM my.categories
WHERE id = theId -- wrong here, because its not a procedure
UNION ALL
SELECT table1.id
FROM my.categories AS table1
JOIN tree AS parent ON theId = table1.parent_id
)
SELECT DISTINCT theId FROM tree WHERE theId = 6;
Example result with data but actually I need only id's.
+----+-----------+---------------+
| id | parent_id | name |
+----+-----------+---------------+
| 1 | null | 'LEVEL 1' |
| 3 | 1 | 'LEVEL 2.2' |
| 6 | 3 | 'LEVEL 3.2.1' |
+----+-----------+---------------+
Or like this:
+----+-----------+---------------+
| id | parent_id | name |
+----+-----------+---------------+
| 3 | 1 | 'LEVEL 2.2' |
| 6 | 3 | 'LEVEL 3.2.1' |
+----+-----------+---------------+
The trouble is I'm not allowed to use procedures. This query should be used as sub-query for many other queries. And please dont look at name column it is irrelevant.
If I get you, this is what you need.
First, with the folowing query you can get all the parent ids:
WITH RECURSIVE t(id, parentlist) AS (
SELECT id, ARRAY[]::bigint[] FROM my.categories WHERE parent_id IS NULL
UNION
SELECT my.categories.id, my.categories.parent_id || t.parentlist
FROM my.categories
JOIN t ON categories.parent_id = t.id
) SELECT * FROM t
-- outputs:
-- id | parentlist
-- ----+------------
-- 1 | {}
-- 2 | {1}
-- 3 | {1}
-- 4 | {2,1}
-- 5 | {2,1}
-- 6 | {3,1}
If you want to get a record of the parents of one id you just need to change the query like:
WITH RECURSIVE t(id, parentlist) AS (
SELECT id, ARRAY[]::bigint[] FROM my.categories WHERE parent_id IS NULL
UNION
SELECT my.categories.id, my.categories.parent_id || t.parentlist
FROM my.categories
JOIN t ON categories.parent_id = t.id
) SELECT unnest(parentlist) as parents_ids FROM t WHERE id=6;
-- outputs:
-- parents_ids
-- -----------
-- 3
-- 1
Note that the last query does not output the "current" id (6).
Related
Data table:
| WINNER | FOOT CLUB|
| -------- | -------- |
| 1 | Beşiktaş |
| 2 | Beşiktaş |
| 3 |Galatasaray |
| 4 |Galatasaray |
| 5 | Beşiktaş |
| 6 | Istanbul |
| 7 | Istanbul |
| 8 | Istanbul |
| 9 |Galatasaray |
| 10 |Galatasaray |
| 11 |Fenerbahçe |
| 12 |Fenerbahçe |
| 13 |Fenerbahçe |
| 14 | Istanbul |
Help, please. I need to make a sorted array of a sequence of identical values appear. Use SQL syntax of any version. I need this result:
Beşiktaş 2
Galatasaray 2
Beşiktaş 1
Istanbul 3
Galatasaray 2
Fenerbahçe 3
Istanbul 1
CREATE TABLE football (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
INSERT INTO football VALUES (1, 'Beşiktaş');
INSERT INTO football VALUES (2, 'Beşiktaş');
INSERT INTO football VALUES (3, 'Galatasaray');
INSERT INTO football VALUES (4, 'Galatasaray');
INSERT INTO football VALUES (5, 'Beşiktaş');
INSERT INTO football VALUES (6, 'Istanbul');
INSERT INTO football VALUES (7, 'Istanbul');
INSERT INTO football VALUES (8, 'Istanbul');
INSERT INTO football VALUES (9, 'Galatasaray');
INSERT INTO football VALUES (10, 'Galatasaray');
INSERT INTO football VALUES (11, 'Fenerbahçe');
INSERT INTO football VALUES (12, 'Fenerbahçe');
INSERT INTO football VALUES (13, 'Fenerbahçe');
INSERT INTO football VALUES (14, 'Istanbul');
SELECT name,
RANK() OVER()
FROM football
it turned out like this:
Beşiktaş|1
Beşiktaş|1
Galatasaray|1
Galatasaray|1
Beşiktaş|1
Istanbul|1
Istanbul|1
Istanbul|1
Galatasaray|1
Galatasaray|1
Fenerbahçe|1
Fenerbahçe|1
Fenerbahçe|1
Istanbul|1
The below was adapted from this solution.
Dbfiddle for your solution if desired
select name, count(*) as cnt
from (select t.*,
(row_number() over (order by id) - row_number() over (partition by name order by id)
) as grp
from football t
) t
group by name, grp
order by min(id) asc
I need to turn a 1:n relationship into a 1:1 relationship with the data remaining the same.
I want to know if is it possible to achieve this with a single pure sql (no plpgsql, no external language).
Below there are more details, a MWE and some extra context.
To illustrate, if I have
+------+--------+ +------+----------+--------+
| id | name | | id | foo_id | name |
|------+--------| |------+----------+--------|
| 1 | foo1 | | 1 | 1 | baz1 |
| 2 | foo2 | | 2 | 1 | baz2 |
| 3 | foo3 | | 3 | 2 | baz3 |
+------+--------+ | 4 | 2 | baz4 |
| 5 | 3 | baz5 |
+------+----------+--------+
I want to get to
+------+--------+ +------+----------+--------+
| id | name | | id | foo_id | name |
|------+--------| |------+----------+--------|
| 4 | foo1 | | 1 | 4 | baz1 |
| 5 | foo1 | | 2 | 5 | baz2 |
| 6 | foo2 | | 3 | 6 | baz3 |
| 7 | foo2 | | 4 | 7 | baz4 |
| 8 | foo3 | | 5 | 8 | baz5 |
+------+--------+ +------+----------+--------+
Here is some code to set up the tables if needed:
drop table if exists baz;
drop table if exists foo;
create table foo(
id serial primary key,
name varchar
);
insert into foo (name) values
('foo1'),
('foo2'),
('foo3');
create table baz(
id serial primary key,
foo_id integer references foo (id),
name varchar
);
insert into baz (foo_id, name) values
(1, 'baz1'),
(1, 'baz2'),
(2, 'baz3'),
(2, 'baz4'),
(3, 'baz5');
I managed to work out the following query that updates only one entry (ie, the
pair <baz id, foo id> has to be provided):
with
existing_foo_values as (
select name from foo where id = 1
),
new_id as (
insert into foo(name)
select name from existing_foo_values
returning id
)
update baz
set foo_id = (select id from new_id)
where id = 1;
The real case scenario (a db migration in a nodejs environment) was solved using
something similar to
const existingPairs = await runQuery(`
select id, foo_id from baz
`);
await Promise.all(existingPairs.map(({
id, foo_id
}) => runQuery(`
with
existing_foo_values as (
select name from foo where id = ${foo_id}
),
new_id as (
insert into foo(name)
select name from existing_foo_values
returning id
)
update baz
set foo_id = (select id from new_id)
where id = ${id};
`)));
// Then delete all the orphan entries from `foo`
Here's a solution that works by first putting together what we want foo to look like (using values from the sequence), and then making the necessary changes to the two tables based on that.
WITH new_ids AS (
SELECT nextval('foo_id_seq') as foo_id, baz.id as baz_id, foo.name as foo_name
FROM foo
JOIN baz ON (foo.id = baz.foo_id)
),
inserts AS (
INSERT INTO foo (id, name)
SELECT foo_id, foo_name
FROM new_ids
),
updates AS (
UPDATE baz
SET foo_id = new_ids.foo_id
FROM new_ids
WHERE new_ids.baz_id = baz.id
)
DELETE FROM foo
WHERE id < (SELECT min(foo_id) FROM new_ids);
I am trying to produce an aggregated output table using aggregates from two different tables. I am unclear on how to join the two outcomes.
The two tables, one listing all products in each store, the other the price variation for each product are as presented below.
| product_id | daily_price | date |
|------------|-------------|------------|
| 1 | 1.25$ | 01-01-2000 |
| 1 | ... | ... |
| 1 | 1$ | 31-12-2000 |
| 2 | 4.5$ | 01-01-2000 |
| 2 | ... | ... |
| 2 | 4.25$ | 31-12-2000 |
| store_id | product_id |
|----------|------------|
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 3 |
| 3 | 2 |
The first aggregation gets the average daily price (it varies) of all products.
SELECT product_id, ROUND((AVG(price)),2) as average_price FROM product_dailyprices
GROUP BY product_id;
| product_id | average_price |
|------------|---------------|
| 1 | 50 |
| 2 | 100 |
| 3 | 250 |
The second query gets me the number of different products available in each store
SELECT store, COUNT(product_id) as product_count FROM products
GROUP BY store;
| store_id | product_count |
|----------|---------------|
| 1 | 200 |
| 2 | 250 |
| 3 | 225 |
I am a bit lost on how to perform a query to produce the following:
| store_id | product_count | average_price_at_store |
|----------|---------------|------------------------|
| 1 | 34 | 6.51$ |
| 2 | 45 | 3.23$ |
| 3 | 36 | 5.37$ |
Thanks for the help!
As you did not provide an SQL for the tables, lets use the following bare bone structure:
CREATE TABLE products
(
id SERIAL NOT NULL,
name text NOT NULL,
CONSTRAINT products_pk PRIMARY KEY (id)
);
CREATE TABLE stores
(
id SERIAL NOT NULL,
name text NOT NULL,
CONSTRAINT stores_pk PRIMARY KEY (id)
);
CREATE TABLE daily_prices
(
product_id INTEGER NOT NULL,
daily_price DOUBLE PRECISION NOT NULL,
date timestamptz,
CONSTRAINT daily_prices_product FOREIGN KEY (product_id) REFERENCES products (id)
);
CREATE TABLE locations
(
store_id INTEGER NOT NULL,
product_id INTEGER NOT NULL,
CONSTRAINT products_product_fk FOREIGN KEY (product_id) REFERENCES products (id),
CONSTRAINT products_store_fk FOREIGN KEY (store_id) REFERENCES stores (id)
);
And let enter some sample data to help use verify tthat the query works:
INSERT INTO products(name)
VALUES ('product 1');
INSERT INTO products(name)
VALUES ('product 2');
INSERT INTO products(name)
VALUES ('product 3');
INSERT INTO stores(name)
VALUES ('store 1');
INSERT INTO stores(name)
VALUES ('store 2');
insert into locations (store_id, product_id)
values (1, 1),
(1, 2),
(2, 2),
(2, 3);
INSERT INTO daily_prices(product_id, daily_price, date)
VALUES (1, 2.0, '01-01-2020');
INSERT INTO daily_prices(product_id, daily_price, date)
VALUES (1, 4.0, '02-01-2020');
INSERT INTO daily_prices(product_id, daily_price, date)
VALUES (2, 3.0, '01-01-2020');
INSERT INTO daily_prices(product_id, daily_price, date)
VALUES (2, 5.0, '02-01-2020');
INSERT INTO daily_prices(product_id, daily_price, date)
VALUES (3, 10.0, '01-01-2020');
INSERT INTO daily_prices(product_id, daily_price, date)
VALUES (3, 20.0, '02-01-2020');
Then the query to produce your desired table would look like:
select l.store_id as store_id,
count(distinct l.product_id) as number_of_products,
avg(dp.daily_price) as average_price
from locations l
join daily_prices dp on dp.product_id = l.product_id
group by l.store_id;
And we can manually verify that it calculated the expected result:
+--------+------------------+-------------+
|store_id|number_of_products|average_price|
+--------+------------------+-------------+
|1 |2 |3.5 |
|2 |2 |9.5 |
+--------+------------------+-------------+
I would like to select a number of tables and select the geometry (geom) and Name columns in each of the tables and append below each other. I have gotten as far as selecting the tables and their columns as shown below:
SELECT TABLE_NAME COLUMN_NAME
FROM INFORMATION_SCHEMA.columns
WHERE (TABLE_NAME LIKE '%HESA' OR
TABLE_NAME LIKE '%HEWH') AND
(COLUMN_NAME = 'geom' AND
COLUMN_NAME = 'Name');
How do you then take the tables:
id | geom | Name | id | geom | Name |
____________________ ____________________
1 | geom1 | Name1 | 1 | geom4 | Name4 |
2 | geom2 | Name2 | 2 | geom5 | Name5 |
3 | geom3 | Name3 | 3 | geom6 | Name6 |
And append the second table below the first, like this:
id | geom | Name |
____________________
1 | geom1 | Name1 |
2 | geom2 | Name2 |
3 | geom3 | Name3 |
1 | geom4 | Name4 |
2 | geom5 | Name5 |
3 | geom6 | Name6 |
Do I use UNION ALL or something else?
https://www.db-fiddle.com/f/75fgQMEWf9LvPj4xYMGWvA/0
based on your sample data:
do
'
declare
r record;
begin
for r in (
SELECT a.TABLE_NAME
FROM INFORMATION_SCHEMA.columns a
JOIN INFORMATION_SCHEMA.columns b on a.TABLE_NAME = b.TABLE_NAME and a.COLUMN_NAME = ''geom'' and b.COLUMN_NAME = ''name''
WHERE (a.TABLE_NAME LIKE ''oranges%'' OR a.TABLE_NAME LIKE ''%_db'')
) loop
execute format(''insert into rslt select geom, name from %I'',r.table_name);
end loop;
end;
'
;
Union All will do the job just fine:
SELECT
*
FROM (
(SELECT * FROM table_one)
UNION ALL
(SELECT * FROM table_two)
) AS tmp
ORDER BY name ASC;
I have added the external SELECT, to show you how you can order the whole result.
DB Fiddle can be found here
So, I have the next table:
time | name | ID |
12:00:00| access | 1 |
12:05:00| select | null |
12:10:00| update | null |
12:15:00| insert | null |
12:20:00| out | null |
12:30:00| access | 2 |
12:35:00| select | null |
The table is bigger (aprox 1-1,5 mil rows) and there will be ID equal to 2,3,4 etc and rows between.
The following should be the result:
time | name | ID |
12:00:00| access | 1 |
12:05:00| select | 1 |
12:10:00| update | 1 |
12:15:00| insert | 1 |
12:20:00| out | 1 |
12:30:00| access | 2 |
12:35:00| select | 2 |
What is the most simple method to update the rows without making the log full? Like, one ID at a time.
You can do it with a sub query:
UPDATE YourTable t
SET t.ID = (SELECT TOP 1 s.ID
FROM YourTable s
WHERE s.time < t.time AND s.name = 'access'
ORDER BY s.time DESC)
WHERE t.name <> 'access'
Index on (ID,time,name) will help.
You can do it using CTE as below:
;WITH myCTE
AS ( SELECT time
, name
, ROW_NUMBER() OVER ( PARTITION BY name ORDER BY time ) AS [rank]
, ID
FROM YourTable
)
UPDATE myCTE
SET myCTE.ID = myCTE.rank
SELECT *
FROM YourTable ORDER BY ID