如何从SQL Oracle中的1行和3个表中获取2个不同的行?

huangapple go评论64阅读模式
英文:

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表达式将statusid1/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

fiddle

英文:

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

fiddle

huangapple
  • 本文由 发表于 2023年3月9日 18:12:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定