获得具有交易总数的员工总数

时间:2014-09-02 10:30:54

标签: 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(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_id)放入选择列表中,那么它总是等于count(transaction_id)  在上述查询中要修改以实现count(employee_id)的内容。

3 个答案:

答案 0 :(得分:0)

似乎在一个地方你想要员工总数的员工表,而在第二个地方,你想要从交易表中完成交易的员工。试试:

 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;

答案 1 :(得分:0)

使用SQL Subquery

select a.employeeid,a.country,a.state,a.city,
(select count(*) from employees) as count_employees,
(select count(*) from transactions) as count_transactions,
(select count(*) from employees where employee_id=a.employee_id) as no_of_employee_transactions
   from transactions a
where to_char(a.tran_date,'Mon-YYYY')='Jun-2014' 
group by a.employeeid,a.country,a.state,a.city;

答案 2 :(得分:0)

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

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;