时间:2021-07-01 10:21:17 帮助过:5人阅读
WITH users_tasks AS (
SELECT
users.email,
array_agg(tasks.name) as task_list,
projects.title
FROM
users,
tasks,
project
WHERE
users.id = tasks.user_id
projects.title = tasks.project_id
GROUP BY
users.email,
projects.title
)
16 1WITH users_tasks AS (
2 SELECT
3 users.email,
4 array_agg(tasks.name) as task_list,
5 projects.title
6 FROM
7 users,
8 tasks,
9 project
10 WHERE
11 users.id = tasks.user_id
12 projects.title = tasks.project_id
13 GROUP BY
14 users.email,
15 projects.title
16)
通过这样定义临时表users_tasks,我就可以在后面加上对users_tasks基本查询语句,像:
SELECT *
FROM users_tasks;
2 1SELECT *
2FROM users_tasks;
total_tasks_per_project AS (
SELECT
project_id,
count(*) as task_count
FROM tasks
GROUP BY project_id
),
tasks_per_project_per_user AS (
SELECT
user_id,
project_id,
count(*) as task_count
FROM tasks
GROUP BY user_id, project_id
),
16 1total_tasks_per_project AS (
2 SELECT
3 project_id,
4 count(*) as task_count
5 FROM tasks
6 GROUP BY project_id
7),
8
9tasks_per_project_per_user AS (
10 SELECT
11 user_id,
12 project_id,
13 count(*) as task_count
14 FROM tasks
15 GROUP BY user_id, project_id
16),
overloaded_users AS (
SELECT tasks_per_project_per_user.user_id
FROM tasks_per_project_per_user,
total_tasks_per_project
WHERE tasks_per_project_per_user.task_count > (total_tasks_per_project / 2)
)
6 1overloaded_users AS (
2 SELECT tasks_per_project_per_user.user_id
3 FROM tasks_per_project_per_user,
4 total_tasks_per_project
5 WHERE tasks_per_project_per_user.task_count > (total_tasks_per_project / 2)
6)
--- Query highlights users that have over 50% of tasks on a given project
--- Gives comma separated list of their tasks and the project
--- Initial query to grab project title and tasks per user
WITH users_tasks AS (
SELECT
users.id as user_id,
users.email,
array_agg(tasks.name) as task_list,
projects.title
FROM
users,
tasks,
project
WHERE
users.id = tasks.user_id
projects.title = tasks.project_id
GROUP BY
users.email,
projects.title
),
--- Calculates the total tasks per each project
total_tasks_per_project AS (
SELECT
project_id,
count(*) as task_count
FROM tasks
GROUP BY project_id
),
--- Calculates the projects per each user
tasks_per_project_per_user AS (
SELECT
user_id,
project_id,
count(*) as task_count
FROM tasks
GROUP BY user_id, project_id
),
--- Gets user ids that have over 50% of tasks assigned
overloaded_users AS (
SELECT tasks_per_project_per_user.user_id
FROM tasks_per_project_per_user,
total_tasks_per_project
WHERE tasks_per_project_per_user.task_count > (total_tasks_per_project / 2)
)
SELECT
email,
task_list,
title
FROM
users_tasks,
overloaded_users
WHERE
users_tasks.user_id = overloaded_users.user_id
58 1--- Query highlights users that have over 50% of tasks on a given project
2--- Gives comma separated list of their tasks and the project
3
4--- Initial query to grab project title and tasks per user
5WITH users_tasks AS (
6 SELECT
7 users.id as user_id,
8 users.email,
9 array_agg(tasks.name) as task_list,
10 projects.title
11 FROM
12 users,
13 tasks,
14 project
15 WHERE
16 users.id = tasks.user_id
17 projects.title = tasks.project_id
18 GROUP BY
19 users.email,
20 projects.title
21),
22
23--- Calculates the total tasks per each project
24total_tasks_per_project AS (
25 SELECT
26 project_id,
27 count(*) as task_count
28 FROM tasks
29 GROUP BY project_id
30),
31
32--- Calculates the projects per each user
33tasks_per_project_per_user AS (
34 SELECT
35 user_id,
36 project_id,
37 count(*) as task_count
38 FROM tasks
39 GROUP BY user_id, project_id
40),
41
42--- Gets user ids that have over 50% of tasks assigned
43overloaded_users AS (
44 SELECT tasks_per_project_per_user.user_id
45 FROM tasks_per_project_per_user,
46 total_tasks_per_project
47 WHERE tasks_per_project_per_user.task_count > (total_tasks_per_project / 2)
48)
49
50SELECT
51 email,
52 task_list,
53 title
54FROM
55 users_tasks,
56 overloaded_users
57WHERE
58 users_tasks.user_id = overloaded_users.user_id
WITH moved_rows AS (
DELETE FROM products
WHERE
"date" >= ‘2010-10-01‘
AND "date" < ‘2010-11-01‘
RETURNING *
)
INSERT INTO products_log
SELECT * FROM moved_rows;
9 1WITH moved_rows AS (
2 DELETE FROM products
3 WHERE
4 "date" >= ‘2010-10-01‘
5 AND "date" < ‘2010-11-01‘
6 RETURNING *
7)
8INSERT INTO products_log
9SELECT * FROM moved_rows;
Postgresql 特性 CTEs (with)
标签:keyword cal 简单 osi 事务性 任务 relative group by ali