连接两个sql表的结果

发布于 2024-10-03 09:06:39 字数 766 浏览 3 评论 0原文

我想从按相同规则分组的两个表中获取结果,这些表在一​​个选择中连接在一起。

我有表 1

create table person AS 
id INTEGER,
gender INTEGER,
state VARCHAR2
name VARCHAR2
surname VARCHAR2

表 2

create table sampletest as
person_id FOREIGN KEY To person.id
result INTEGER

表 3

create table examtest as 
person_id FOREIGN KEY to person.id
examresult INTEGER

我想

按状态 | 获取此输出组按性别分组 |计数(考试结果>0) | count(result>0 and result<4)

我尝试了类似的方法

select state,gender,count(e.examresult),count(s.result) where 
p.id=s.person_id and p.id=e.person_id and 
s.result>0 and s.result<4 and 
e.examresult>0 group by state,gender

,但得到的结果相互依赖。如何将独立结果纳入一项选择?

I would like to get results from two tables grouped by same rules joined together in one select.

I have table 1

create table person AS 
id INTEGER,
gender INTEGER,
state VARCHAR2
name VARCHAR2
surname VARCHAR2

table 2

create table sampletest as
person_id FOREIGN KEY To person.id
result INTEGER

table 3

create table examtest as 
person_id FOREIGN KEY to person.id
examresult INTEGER

I would like to get this output

group by state | group by gender | count(examresult>0) | count(result>0 and result<4)

I tried something like this

select state,gender,count(e.examresult),count(s.result) where 
p.id=s.person_id and p.id=e.person_id and 
s.result>0 and s.result<4 and 
e.examresult>0 group by state,gender

but i get results that are dependent on each other. How do i get independent results into one select?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿嘿嘿 2024-10-10 09:06:39
  SELECT state,gender,
         SUM(CASE WHEN e.examresult > 0 THEN 1 ELSE 0 END) AS EXAM_GT_ZERO,
         SUM(CASE WHEN s.result BETWEEN 0 AND 4 THEN 1 ELSE 0 END) AS SMPL_0_to_4
    FROM person p
         LEFT JOIN sampletest s
         ON p.id = s.person_id 
         LEFT JOIN examtest e
         ON p.id = e.person_id
GROUP BY state,gender
  SELECT state,gender,
         SUM(CASE WHEN e.examresult > 0 THEN 1 ELSE 0 END) AS EXAM_GT_ZERO,
         SUM(CASE WHEN s.result BETWEEN 0 AND 4 THEN 1 ELSE 0 END) AS SMPL_0_to_4
    FROM person p
         LEFT JOIN sampletest s
         ON p.id = s.person_id 
         LEFT JOIN examtest e
         ON p.id = e.person_id
GROUP BY state,gender
噩梦成真你也成魔 2024-10-10 09:06:39

构建子选择

select
  p.state,
  p.gender,
  sum( 
    ( select count(1) from examtest e 
      where e.personid = p.personid 
      and e.examresult > 0 ) ) as examcount,
  sum( 
    ( select count(1) from sampletest s 
      where s.personid = p.personid
      and s.result > 0 and s.result < 4) ) as samplecount
from
  person p
group by
  p.state,
  p.gender

Build sub-selects

select
  p.state,
  p.gender,
  sum( 
    ( select count(1) from examtest e 
      where e.personid = p.personid 
      and e.examresult > 0 ) ) as examcount,
  sum( 
    ( select count(1) from sampletest s 
      where s.personid = p.personid
      and s.result > 0 and s.result < 4) ) as samplecount
from
  person p
group by
  p.state,
  p.gender
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文