SQL聚合超过3个表

时间:2017-11-07 23:00:15

标签: sql

嗯,这让我感到烦恼。任何帮助将不胜感激。

我试图计算出有多少项目ID和步骤。关系是:

  • 项目(n-1)页
  • 页面(n-1)状态步骤

示例项目数据

id  name    
1   est et
2   quia nihil

样本页数据

id  project_id  workflow_step_id    
1   1           1
2   1           1
3   1           2
4   1           1
5   2           3
6   2           3
7   2           4

示例步骤数据

id  name
1   a
2   b
3   c
4   d

预期产出

project_id  name  count_steps
1           a     3
1           b     1
2           c     2
2           d     1

谢谢!

1 个答案:

答案 0 :(得分:0)

满足预期结果的方法。也可以在SQL Fiddle

查看
#!/bin/sh
###########################

# add all added/modified files
git add *
# commit changes
git commit -m "made changes"
# push to git remote repository
git push origin master
###########################
echo Press Enter...
read

查询1

test <- c("ASSFGH","PSSWEE","ASSYNT","ASSFRK","ASSFGT","ASSRTV")

<强> Results

CREATE TABLE Pages
    ("id" int, "project_id" int, "workflow_step_id" int)
;

INSERT INTO Pages
    ("id", "project_id", "workflow_step_id")
VALUES
    (1, 1, 1),
    (2, 1, 1),
    (3, 1, 2),
    (4, 1, 1),
    (5, 2, 3),
    (6, 2, 3),
    (7, 2, 4)
;


CREATE TABLE workflow_steps
    ("id" int, "name" varchar(1))
;

INSERT INTO workflow_steps
    ("id", "name")
VALUES
    (1, 'a'),
    (2, 'b'),
    (3, 'c'),
    (4, 'd')
;


CREATE TABLE Projects
    ("id" int, "name" varchar(10))
;

INSERT INTO Projects
    ("id", "name")
VALUES
    (1, 'est et'),
    (2, 'quia nihil')
;