不同表上的COUNT和GROUP BY

时间:2017-11-24 11:10:44

标签: oracle group-by count

我有三张桌子: T_BUSINESS_LOC(包含营业地点)

Person_ID | Business_loc
---------------------
101         Delhi      
102         Mumbai     
103         Noida      
104         Mumbai     
105         Noida      
106         Delhi      
107         Mumbai     
108         Delhi      
109         Mumbai     

T_LOG_ACCESS(包含会话详细信息作为日期和时间)

Person_ID | ACCESS_DATE
---------------------
101         23-OCT-17 12.07.55.027206000 AM
101         25-OCT-17 12.10.36.522369000 AM
102         23-OCT-17 12.52.52.289198000 AM
103         23-OCT-17 12.15.58.494122000 AM
103         23-OCT-17 12.15.58.494122000 AM
103         24-OCT-17 12.23.41.205421000 AM
104         23-OCT-17 12.05.05.726392000 AM
104         25-OCT-17 12.05.05.726392000 AM
108         23-OCT-17 06.48.36.399221000 AM
109         23-OCT-17 06.53.13.026891000 AM
109         25-OCT-17 12.05.05.726392000 AM

PERSON_ROLE(包含用户角色)

Person_ID | assigned_role
---------------------
101         HR_Role
102         MN_Role
103         HR_Role
104         HR_Role
105         MN_Role
106         Cont_Role      
107         HR_Role     
108         Cont_Role
109         MN_Role

我正在努力获取仅在10月23日访问过的用户数量;按位置分组,输出如下:

Location  |  MN_Role_Count   |   HR_Role_Count   |   Cont_Role_Count
--------------------------------------------------------------------
Delhi       1                   1                    1
Mumbai      1                   0                    0
Noida       0                   2                    0

我试图通过以下查询实现此目的,但没有成功:

select a.location,
count (a.Person_id) AS MN_Role_Count, count (b.oprid) AS HR_Role_Count,  
count (c.oprid) AS Cont_Role_Count from T_BUSINESS_LOC  a, T_BUSINESS_LOC  
b, T_BUSINESS_LOC c where a.Person_id in (select distinct (ac1.Person_id) 
from T_LOG_ACCESS ac1 where ac1.T_LOG_ACCESS like '23-OCT-17%' and 
ac1.Person_id in (select ps1.assigned_role from PERSON_ROLE ps1 where 
ps1.assigned_role='MN_Role'))
and b.oprid in (select distinct (ac2.Person_id) from T_LOG_ACCESS ac2 where 
ac2.T_LOG_ACCESS like '23-OCT-17%' and ac2.Person_id in (select 
ps2.assigned_role from PERSON_ROLE ps2 where ps2.rolename='HR_Role'))
and c.oprid in (select distinct (ac3.Person_id) from T_LOG_ACCESS ac3 where 
ac3.T_LOG_ACCESS like '23-OCT-17%' and ac3.Person_id in (select 
ps3.assigned_role from PERSON_ROLE ps3 where ps3.rolename='Cont_Role'))
group by a.location;

有人可以指导我吗?

2 个答案:

答案 0 :(得分:0)

尝试这个:

   with T_BUSINESS_LOC (Person_ID, Business_loc) as (
    select 101, 'Delhi'  from dual union all
    select 102, 'Mumbai' from dual union all
    select 103, 'Noida'  from dual union all
    select 104, 'Mumbai' from dual union all
    select 105, 'Noida'  from dual union all
    select 106, 'Delhi'  from dual union all
    select 107, 'Mumbai' from dual union all
    select 108, 'Delhi'  from dual union all
    select 109, 'Mumbai' from dual ),
  T_LOG_ACCESS(Person_ID, ACCESS_DATE) as (
    select 101, timestamp '2017-10-23 12:07:55.027206000' from dual union all
    select 101, timestamp '2017-10-25 12:10:36.522369000' from dual union all
    select 102, timestamp '2017-10-25 12:52:52.289198000' from dual union all
    select 103, timestamp '2017-10-23 12:15:58.494122000' from dual union all
    select 103, timestamp '2017-10-23 12:15:58.494122000' from dual union all
    select 103, timestamp '2017-10-24 12:23:41.205421000' from dual union all
    select 104, timestamp '2017-10-23 12:05:05.726392000' from dual union all
    select 104, timestamp '2017-10-25 12:05:05.726392000' from dual union all
    select 108, timestamp '2017-10-23 06:48:36.399221000' from dual union all
    select 109, timestamp '2017-10-23 06:53:13.026891000' from dual union all
    select 109, timestamp '2017-10-25 12:05:05.726392000' from dual ),
  PERSON_ROLE(Person_ID, assigned_role) as (
    select 101, 'HR_Role'   from dual union all
    select 102, 'MN_Role'   from dual union all
    select 103, 'HR_Role'   from dual union all
    select 104, 'HR_Role'   from dual union all
    select 105, 'MN_Role'   from dual union all
    select 106, 'Cont_Role' from dual union all
    select 107, 'HR_Role'   from dual union all
    select 108, 'Cont_Role' from dual union all
    select 109, 'MN_Role'   from dual )   
    select
    business_loc,
    count(DISTINCT CASE WHEN SUB2.assigned_role='MN_Role'   THEN SUB1.ACCESS_DATE END)MN_Role_Count ,
    count(DISTINCT CASE WHEN SUB2.assigned_role='HR_Role'   THEN SUB1.ACCESS_DATE END)HR_Role_Count ,
    count(DISTINCT CASE WHEN SUB2.assigned_role='Cont_Role' THEN SUB1.ACCESS_DATE END)Cont_Role_Count   
    from
    T_BUSINESS_LOC MAIN
    INNER JOIN T_LOG_ACCESS  SUB1 ON MAIN.PERSON_ID=SUB1.PERSON_ID and TRUNC(SUB1.ACCESS_DATE)=TO_DATE('20171023','YYYYMMDD')
    INNER JOIN PERSON_ROLE   SUB2 ON MAIN.PERSON_ID=SUB2.PERSON_ID
    group by business_loc

答案 1 :(得分:0)

如果您使用的是Oracle 11或更高版本您可以使用C://Projects/MobileApp/android/build

BTW, this is how I call my root component 
import { AppRegistry } from 'react-native';
import { MyApp} from './build';

AppRegistry.registerComponent('MyApp', () => MyApp);

结果:

niv <- c("A","B","C","D","E","X","Y")
from <- c("A","A","A","A","B","C","D","E", "X", "B")
to <- c("B","C","D","E","X","X","Y","Y","Y", "C")
temp <- data.table(from=factor(from, levels=niv),
to=factor(to,levels=niv), col=c(rep("blue",5), rep("black",5)))

nodes <-   create_node_df(  n=length(niv), label=niv,  width=0.3) 
edges <- create_edge_df(from = temp$from, to = temp$to, 
rel = "leading_to", label=temp$from, color=temp$col)   
graph <- create_graph(  nodes_df = nodes, edges_df = edges)
render_graph(graph)