简体   繁体   English

多个GROUP和COUNT个Oracle SQL

[英]Multiple GROUP and COUNT Oracle SQL

I have an Oracle 11g database with two tables I need to pull, group, and count data from. 我有一个带有两个表的Oracle 11g数据库,我需要从中提取,分组和计数数据。 The data in the tables is: 表中的数据为:

Table 'ED'
|NAME | SSN       | IDG   | IDE | IDD | SPOUSE | CHILD |
========================================================
|John | 111111111 | 12345 | 123 | 0   | FALSE  | FALSE |
|Sue  | 111221111 | 12345 | 123 | 1   | TRUE   | FALSE |
|Joe  | 111331111 | 12345 | 123 | 2   | FALSE  | TRUE  |
|Sam  | 111441111 | 12345 | 321 | 0   | FALSE  | TRUE  |
|Jane | 111551111 | 12345 | 321 | 1   | TRUE   | FALSE |
|Jim  | 111661111 | 12345 | 555 | 0   | FALSE  | TRUE  |
|Zach | 111771111 | 12345 | 555 | 2   | FALSE  | TRUE  |

Table 'EL'
|IDG   | IDE | IDD | FLAG01 | FLAG02 | FLAG03 | FLAG04 |
========================================================
|12345 | 123 | 0   | TRUE   | FALSE  | TRUE   | TRUE   |
|12345 | 123 | 1   | TRUE   | FALSE  | FALSE  | FALSE  |
|12345 | 123 | 2   | TRUE   | TRUE   | TRUE   | TRUE   |
|12345 | 321 | 0   | TRUE   | FALSE  | TRUE   | TRUE   |
|12345 | 321 | 1   | TRUE   | FALSE  | FALSE  | FALSE  |
|12345 | 555 | 0   | TRUE   | FALSE  | TRUE   | TRUE   |
|12345 | 555 | 1   | TRUE   | FALSE  | FALSE  | FALSE  |

So the first 3 people are a family, the father, spouse, and child. 因此,前三个人是一家人,父亲,配偶和孩子。 I need to count all of them as a group "family" under IDG=12345 and IDE=123. 我需要将它们全部归为IDG = 12345和IDE = 123下的一个“家庭”。 The next 2 people are a couple, husband and wife. 接下来的两个人是一对夫妻。 I need to group them as a "couple". 我需要将它们归为“一对”。 The last 2 people are a father and child so they need to be grouped as "dad-child". 最后两个人是父亲和孩子,因此需要将其分组为“爸爸孩子”。 The next grouping is a count of each of the above groupings where FLAG01=T then a separate count where FLAG02 is true, and FLAG03 is true and FLAG04 is true. 下一个分组是上述每个分组的计数,其中FLAG01 = T,然后是FLAG02为true,FLAG03为true和FLAG04为true的单独计数。 So the desired output would be: 因此,所需的输出将是:

FLAG01=T:
Family: 3
Couple: 2
Dad-Child: 2

FLAG02=T:
Family: 1
Couple: 0
Dad-Child: 0

FLAG03=T:
Family: 2
Couple: 1
Dad-Child: 1

FLAG04=T:
Family: 2
Couple: 1
Dad-Child: 1

I know there must be a join on the two tables for the IDG and IDE fields but I'm not sure how to group by the "family", "couple", "dad-child" categories and then count the numbers based on the flags being true or false. 我知道IDG和IDE字段的两个表上必须有一个联接,但是我不确定如何按“家庭”,“夫妇”,“大孩子”类别进行分组,然后根据标志为真或假。 I appreciate any and all assistance! 感谢您的协助!

This should do the trick, of course if I understood correctly your problem 如果我正确理解了您的问题,这应该可以解决问题

SELECT COUNT(EL.FLAG01), COUNT(EL.FLAG02), COUNT(EL.FLAG03), COUNT(EL.FLAG04), ED.IDE 
FROM ED
INNER JOIN EL on ED.IDE=EL.IDE
GROUP BY ED.IDE

looking to you data and based on you explanation you could retrieve the value you need using some aggregation function (assuming TRUE and FALSE are string otherwise .. remove quotes in query) 查看数据并根据您的解释,您可以使用一些聚合函数来检索所需的值(假设TRUE和FALSE是字符串,否则..删除查询中的引号)

select IDG
  , IDE
  , count(*)
  , case when max(spouse) = 'TRUE' AND max(child) = 'FALSE' then 'Couple'
         when max(spouse) = 'TRUE' AND max(child) = 'TRUE' then 'Family'
         when max(spouse) = 'FALSE' AND max(child) = 'TRUE' AND count(*)>1 then dad-child
         END type_of_ralation
FROM ED
group by IDG , IDE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM