This question already has answers here:
PostgreSQL "Column does not exist" but it actually does
(6 answers)
sql statement error: "column .. does not exist"
(1 answer)
Closed 10 months ago.
In postgres, I have generated table user and table organizations. There is some relationship between them, i.e.: multiple users belong to one organization.
The table user_organizations_organization was auto-generated by postgres.
Here I have:
development=# select * from user_organizations_organization;
userId | organizationId
--------+----------------
1 | 1
2 | 1
3 | 2
4 | 2
5 | 1
6 | 1
7 | 2
8 | 2
9 | 3
10 | 3
11 | 4
12 | 4
13 | 3
14 | 3
15 | 4
16 | 4
17 | 5
18 | 5
19 | 6
20 | 6
21 | 5
22 | 5
23 | 6
24 | 6
25 | 7
26 | 7
27 | 8
28 | 8
29 | 7
30 | 7
31 | 8
32 | 8
33 | 9
34 | 9
35 | 10
36 | 10
37 | 9
38 | 9
39 | 10
40 | 10
(40 rows)
I want to delete relationships related to organizations 5,6,7,8:
development=# delete from user_organizations_organization where organizationId in (5,6,7,8);
ERROR: column "organizationid" does not exist
LINE 1: delete from user_organizations_organization where organizati...
^
HINT: Perhaps you meant to reference the column "user_organizations_organization.organizationId".
development=#
How can I delete them?
Related
select * from rankings;
students | stu_id | card_id
----------+--------+---------
Charlie | 11 | 1
Shakira | 22 | 2
Selena | 33 | 3
Ross | 44 | 4
Chandler | 55 | 5
James | 66 | 6
Kylie | 77 | 7
Here is the table kindly tell me how we can apply the rank function in it with Postgresql?
I am making GROUP BY hour for one day. In this case there are some time intervals with no data. How can i take these missing hours as 0 value?. Then there would be always 23 value in my query result.
Below what I tried so far.
SELECT COUNT(*), extract(hour from created_date) as hourr
FROM client_requests
WHERE created_date > '2020-02-24 00:00:00' and created_date < '2020-02-24 23:59:59'
GROUP BY extract(hour from created_date)
ORDER BY extract(hour from created_date) ASC
result is like below:
count | hourr |
------------------------------------
1 | 0
1 | 6
5 | 8
14 | 9
35 | 10
37 | 11
40 | 12
32 | 13
18 | 14
28 | 15
39 | 16
31 | 17
30 | 18
16 | 19
7 | 20
11 | 21
14 | 22
But i need like this
count | hourr |
------------------------------------
1 | 0
0 | 1
0 | 2
0 | 3
0 | 4
0 | 5
1 | 6
0 | 7
5 | 8
14 | 9
35 | 10
37 | 11
40 | 12
32 | 13
18 | 14
28 | 15
39 | 16
31 | 17
30 | 18
16 | 19
7 | 20
11 | 21
14 | 22
0 | 23
Left join with a generated table of hours:
SELECT coalesce(count(r.*), 0) AS count,
h.h as hour
FROM generate_series(0, 23) AS h
LEFT JOIN client_requests AS r
ON extract(hour from r.created_date) = h.h
AND r.created_date >= '2020-02-24 00:00:00'
AND r.created_date < '2020-02-25 00:00:00'
GROUP BY h.h
ORDER BY h.h;
This is to confirm if my design is good enough or get the better ideas to solve the bus routing problem with time. Here is my solution with the primary steps given below:
Have one edges table which represents all the edges (the source and target represent vertices (bus stops):
postgres=# select id, source, target, cost from busedges;
id | source | target | cost
----+--------+--------+------
1 | 1 | 2 | 1
2 | 2 | 3 | 1
3 | 3 | 4 | 1
4 | 4 | 5 | 1
5 | 1 | 7 | 1
6 | 7 | 8 | 1
7 | 1 | 6 | 1
8 | 6 | 8 | 1
9 | 9 | 10 | 1
10 | 10 | 11 | 1
11 | 11 | 12 | 1
12 | 12 | 13 | 1
13 | 9 | 15 | 1
14 | 15 | 16 | 1
15 | 9 | 14 | 1
16 | 14 | 16 | 1
Have a table which represents bus details like from time, to time, edge etc.
NOTE: I have used integer format for "from" and "to" column for faster results as I can do an integer query, but I can replace it with any better format if available.
postgres=# select id, "busedgeId", "busId", "from", "to" from busedgetimes;
id | busedgeId | busId | from | to
----+-----------+-------+-------+-------
18 | 1 | 1 | 33000 | 33300
19 | 2 | 1 | 33300 | 33600
20 | 3 | 2 | 33900 | 34200
21 | 4 | 2 | 34200 | 34800
22 | 1 | 3 | 36000 | 36300
23 | 2 | 3 | 36600 | 37200
24 | 3 | 4 | 38400 | 38700
25 | 4 | 4 | 38700 | 39540
Use dijkstra algorithm to find the nearest path.
Get the upcoming buses from the busedgetimes table in the earliest first order for the nearest path detected by dijkstra algorithm. => This leads to a bit complex query though.
Can I do any kind of improvements to this, or are there any better designs?
Links to docs, articles related to this would be really helpful.
This is totally normal and the regular way to do it. See also,
PgRouting Example
I want to use a query to get this result:
Postid(20) ---> 4 type(0) et 2 type(1)
Postid(21) ---> 3 type(0) et 3 type(1).
From this table:
id | userid | postid | type
1 | 465 | 20 | 0
2 | 465 | 21 | 1
3 | 466 | 20 | 1
4 | 466 | 21 | 0
5 | 467 | 20 | 0
6 | 467 | 21 | 0
7 | 468 | 20 | 1
8 | 468 | 21 | 1
9 | 469 | 20 | 0
10 | 469 | 21 | 1
11 | 470 | 20 | 0
12 | 470 | 21 | 0
I think I have to use GROUP BY, I tried it but I get no results.
How can I achieve that result?
You need to use an aggregation function, alongside the columns you want to group by in the SELECT part.
Note: Any column that is selected alongside an aggregation function MUST come up in the GROUP BY section.
The following code should answer your question:
SELECT COUNT(id), postid, type FROM table_name GROUP BY postid, type
When using multiple GROUP BY columns, those entries that have all those columns in common will be grouped up, see here: https://stackoverflow.com/a/2421441/9743294
My table is:
id sub_id datetime resource
---|-----|------------|-------
1 | 10 | 04/03/2009 | 399
2 | 11 | 04/03/2009 | 244
3 | 10 | 04/03/2009 | 555
4 | 10 | 03/03/2009 | 300
5 | 11 | 03/03/2009 | 200
6 | 11 | 03/03/2009 | 500
7 | 11 | 24/12/2008 | 600
8 | 13 | 01/01/2009 | 750
9 | 10 | 01/01/2009 | 760
10 | 13 | 01/01/2009 | 570
11 | 11 | 01/01/2009 | 870
12 | 13 | 01/01/2009 | 670
13 | 13 | 01/01/2009 | 703
14 | 13 | 01/01/2009 | 705
I need to select for each sub_id only 2 times
Result would be:
id sub_id datetime resource
---|-----|------------|-------
1 | 10 | 04/03/2009 | 399
3 | 10 | 04/03/2009 | 555
5 | 11 | 03/03/2009 | 200
6 | 11 | 03/03/2009 | 500
8 | 13 | 01/01/2009 | 750
10 | 13 | 01/01/2009 | 570
How can I achieve this result in postgres ?
Use the window function row_number():
select id, sub_id, datetime, resource
from (
select *, row_number() over (partition by sub_id order by id)
from my_table
) s
where row_number < 3;
look at the order column (I use id to match your sample):
t=# with data as (select *,count(1) over (partition by sub_id order by id) from t)
select id,sub_id,datetime,resource from data where count <3;
id | sub_id | datetime | resource
----+--------+------------+----------
1 | 10 | 2009-03-04 | 399
3 | 10 | 2009-03-04 | 555
2 | 11 | 2009-03-04 | 244
5 | 11 | 2009-03-03 | 200
8 | 13 | 2009-01-01 | 750
10 | 13 | 2009-01-01 | 570
(6 rows)