Sql 正在获取具有事务总数的员工总数

Sql 正在获取具有事务总数的员工总数,sql,oracle,Sql,Oracle,我有一张员工桌 employeeid, country, state, and city 然后我有一个事务表 transaction_id, employeeid, transaction-details 所以我需要 country state city total(no_of_employee), count(transactions), count(no_of_employee_done_transaction) 我已经试过并且能够得到 country state city count

我有一张员工桌

employeeid, country, state, and city
然后我有一个事务表

transaction_id, employeeid, transaction-details
所以我需要

country state city total(no_of_employee), count(transactions), count(no_of_employee_done_transaction)
我已经试过并且能够得到

country state city count(transactions), count(no_of_employee_done_transaction)
通过使用此查询:

 select em.Country, em.state, em.city , count(transaction_id) as "count(transaction)" , count(distinct(employeeid)) as "count(number of employee done transaction)" 
    from transaction tr
    right outer join employee em on tr.employeeid = em.employeeid
    where to_char(tran_date,'Mon-YYYY')='Jun-2014'
    group by em.country,em.state, em.city;
如果我把
count(employee\u id)
放在选择列表中,那么它总是等于
count(transaction\u id)

在上面的查询中,要修改什么以实现计数(员工id)。

似乎在一个位置需要从员工表中获取员工总数,在第二个位置,需要从交易表中获取已完成交易的员工。尝试:

 select em.Country, 
        em.state, 
        em.city , 
        count(distinct em.employeeid), 
        count(transaction_id) as "count(transaction)" , 
        count(distinct tr.employeeid) as "count(number of employee done transaction)" 
    from transaction tr
    right outer join employee em on tr.employeeid = em.employeeid
    where to_char(tran_date,'Mon-YYYY')='Jun-2014'
    group by em.country,em.state, em.city;
使用


不太清楚你的要求。请看看这是不是你想要的

select em.Country, em.state, em.city , count(transaction_id) as "count(transaction)" , count(distinct(employeeid)) as "count(number of employee done transaction)",
(select count(distinct(employeeid)) as total(no_of_employee) 
    from employee c 
    where em.Country = c.Country
          em.state   = c.state
          em.city    = c.city)
    from transaction tr
    right outer join employee em on tr.employeeid = em.employeeid
    where to_char(tran_date,'Mon-YYYY')='Jun-2014'
    group by em.country,em.state, em.city;

请提供样品表数据和预期结果。您希望每个城市的交易表中有两个不同的计数吗?我认为在没有子查询或CTE或类似查询的情况下,您不可能在单个查询中做到这一点。您是否尝试过将
COUNT(事务id不为NULL时的不同情况,然后是employeeid)作为“COUNT(没有员工完成的事务)”
yes@Rup,您是对的。请建议一些相关的queryagain计数(distinct em.employeeid)等于计数(distinct tr.employeeid)。这意味着,2014年6月,每位员工至少完成了一次交易。您可以从各个表中进行检查,一次从2014年6月的交易中进行检查,然后再次从员工表中进行计数。对不起,计数(distinct em.employeeid)等于计数(transaction_id),因为“计数(交易)”是。distinct是冗余的。谢谢你指出@DamienJoe。
select em.Country, em.state, em.city , count(transaction_id) as "count(transaction)" , count(distinct(employeeid)) as "count(number of employee done transaction)",
(select count(distinct(employeeid)) as total(no_of_employee) 
    from employee c 
    where em.Country = c.Country
          em.state   = c.state
          em.city    = c.city)
    from transaction tr
    right outer join employee em on tr.employeeid = em.employeeid
    where to_char(tran_date,'Mon-YYYY')='Jun-2014'
    group by em.country,em.state, em.city;