英文:
How to get 2 distinct rows from 1 row with 3 tables in sql oracle?
问题
我有3个表,看起来有点像这样。
PersonTable
ID | Person |
---|---|
1 | 姓名1 |
2 | 姓名2 |
3 | 姓名3 |
TypeToPersonTable
SPECID | PersonID |
---|---|
4 | 1 |
8 | 1 |
10 | 3 |
RequestSatusTable
SPECID | StatusID |
---|---|
4 | 1 |
4 | 1 |
4 | 0 |
8 | 0 |
8 | 1 |
我需要计算每个人有多少个哪种状态 ID。因此,结果应该如下所示。
人名 | 状态 | 计数 |
---|---|---|
姓名1 | 良好 | 3 |
姓名1 | 差 | 2 |
我尝试使用DISTINCT,但它不起作用。
SELECT PersonTable.Perosn AS 人名,
(DISTINCT RequestSatusTable.StatusId) AS 状态,
COUNT(RequestSatusTable.StatusId)
FROM PersonTable
JOIN TypeToPersonTable ON PersonTable.ID = TypeToPersonTable.PersonID
JOIN RequestSatusTable ON RequestSatusTable.SPECID= TypeToPersonTable.SPECID
GROUP BY PersonTable.Person
我无法弄清楚。任何帮助将不胜感激。
英文:
I have 3 tables that looks a bit like this.
PersonTable
ID | Person |
---|---|
1 | name1 |
2 | name2 |
3 | name3 |
TypeToPersonTable
SPECID | PersonID |
---|---|
4 | 1 |
8 | 1 |
10 | 3 |
RequestSatusTable
SPECID | StatusID |
---|---|
4 | 1 |
4 | 1 |
4 | 0 |
8 | 0 |
8 | 1 |
I need to calculate how many of which status id there are for each person. So the result has to look like this.
person | status | count |
---|---|---|
name1 | good | 3 |
name1 | bad | 2 |
I've tried using distinct like this but it's not working.
SELECT PersonTable.Perosn AS person,
(DISTINCT RequestSatusTable.StatusId) AS status,
COUNT(RequestSatusTable.StatusId)
FROM PersonTable
JOIN TypeToPersonTable ON PersonTable.ID = TypeToPersonTable.PersonID
JOIN RequestSatusTable ON RequestSatusTable.SPECID= TypeToPersonTable.SPECID
GROUP BY PersonTable.Person
I can't figure it out. Any help would be much appreciated.
答案1
得分: 1
在GROUP BY
中包括statusid
,并使用CASE
表达式将statusid
的1
/0
转换为good
/bad
:
SELECT p.Person,
CASE r.StatusId WHEN 1 THEN 'good' ELSE 'bad' END AS status,
COUNT(*) AS count
FROM PersonTable p
JOIN TypeToPersonTable t
ON p.ID = t.PersonID
JOIN RequestSatusTable r
ON r.SPECID = t.SPECID
GROUP BY
p.Person,
r.StatusId
对于示例数据:
CREATE TABLE PersonTable (ID, Person) AS
SELECT 1, 'name1' FROM DUAL UNION ALL
SELECT 2, 'name2' FROM DUAL UNION ALL
SELECT 3, 'name3' FROM DUAL;
CREATE TABLE TypeToPersonTable (SPECID, PersonID) AS
SELECT 4, 1 FROM DUAL UNION ALL
SELECT 8, 1 FROM DUAL UNION ALL
SELECT 10, 3 FROM DUAL;
CREATE TABLE RequestSatusTable (SPECID, StatusID) AS
SELECT 4, 1 FROM DUAL UNION ALL
SELECT 4, 1 FROM DUAL UNION ALL
SELECT 4, 0 FROM DUAL UNION ALL
SELECT 8, 0 FROM DUAL UNION ALL
SELECT 8, 1 FROM DUAL;
输出:
PERSON | STATUS | COUNT |
---|---|---|
name1 | good | 3 |
name1 | bad | 2 |
英文:
Include statusid
in the GROUP BY
and use a CASE
expression to conver the 1
/0
of statusid
to good
/bad
:
SELECT p.Person,
CASE r.StatusId WHEN 1 THEN 'good' ELSE 'bad' END AS status,
COUNT(*) AS count
FROM PersonTable p
JOIN TypeToPersonTable t
ON p.ID = t.PersonID
JOIN RequestSatusTable r
ON r.SPECID= t.SPECID
GROUP BY
p.Person,
r.StatusId
Which, for the sample data:
CREATE TABLE PersonTable (ID, Person) AS
SELECT 1, 'name1' FROM DUAL UNION ALL
SELECT 2, 'name2' FROM DUAL UNION ALL
SELECT 3, 'name3' FROM DUAL;
CREATE TABLE TypeToPersonTable (SPECID, PersonID) AS
SELECT 4, 1 FROM DUAL UNION ALL
SELECT 8, 1 FROM DUAL UNION ALL
SELECT 10, 3 FROM DUAL;
CREATE TABLE RequestSatusTable (SPECID, StatusID) AS
SELECT 4, 1 FROM DUAL UNION ALL
SELECT 4, 1 FROM DUAL UNION ALL
SELECT 4, 0 FROM DUAL UNION ALL
SELECT 8, 0 FROM DUAL UNION ALL
SELECT 8, 1 FROM DUAL;
Outputs:
PERSON | STATUS | COUNT |
---|---|---|
name1 | good | 3 |
name1 | bad | 2 |
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论