简体   繁体   English

涉及带条件的部分组的 SQL 查询?

[英]SQL query involving partial group with condition?

Below is a SQL query problem for which I am not able to understand correct approach:以下是我无法理解正确方法的 SQL 查询问题:

DB tables:数据库表:

Employee: emp_id, emp_name
Credit: credit_id, emp_id, credit_date, credit_amount
debit: debit_id, emp_id, debit_date, debit_amount

Here, each person can have multiple incomes and expenses.在这里,每个人都可以有多种收入和支出。

Query requirement: At the end of each day, each employee will have some asset('credit till now' - 'debit till now').查询要求:每天结束时,每个员工都会有一些资产('贷到现在' - '借到现在')。 We need to find top five employees in terms of maximum asset and the date on which they had this maximum asset.我们需要根据最大资产以及他们拥有此最大资产的日期来查找前五名员工。

I have tried the below query but seems like I am missing something:我已经尝试了以下查询,但似乎我遗漏了一些东西:

select Credit.emp_id, Credit.date, (Credit.income_amount - Debit.credit_amount) from 
(select emp_id, sum(amount) as credit_amount 
from credit) Credit
LEFT JOIN LATERAL (
     select emp_id, sum(amount) as debit_amount
     from debits
     where debits.emp_id = Credit.emp_id and Credit.date >= debits.date
     group by debits.emp_id
    ) Debit
ON true

Here I'm breaking the query to make it more readable.在这里,我打破了查询以使其更具可读性。

First of all, we need to get the total amount on a day-level for both credit and debit both, so that we can join the credit and debit table on the day level with the same emp_id.首先,我们需要获取credit和debit两者在一天级别的总金额,以便我们可以使用相同的emp_id加入天级别的credit和debit表。

with 
credit as(
    select emp_id,credit_date date,sum(credit_amount) as amount
    from credit  
    group by 1,2),

debit as(
    select emp_id,debit_date,sum(debit_amount) as amount
    from expenses 
    group by 1,2),

Now we need to full outer join the "credit" and "debit" subqueries现在我们需要完全外部加入“credit”和“debit”子查询

payments as (
    select distinct
    case when c.emp_id is null then d.person_id else c.emp_id  end as emp_id ,
    case when c.emp_id is null then d.date else c.date end as date,
    case when c.emp_id is null then 0 else i.amount end as credit ,
    case when d.emp_id is null then 0 else d.amount end as debit 
    from credit c
    full outer join debit d on d.emp_id=c.emp_id and d.date=c.date
    ),

Now we will take day-wise cumulative sum for credit, debit and total balance as shown below.现在我们将按天计算贷方、借方和总余额的累计金额,如下所示。

total_balance as(
    SELECT emp_id, date, 
    sum(credit) OVER (PARTITION BY emp_id ORDER BY date asc) AS total_credit,
    sum(debit) OVER (PARTITION BY emp_id ORDER BY date asc) AS total_debit,
    (sum(income) OVER (PARTITION BY person_id ORDER BY date asc) - 
    sum(expense) OVER (PARTITION BY person_id ORDER BY date asc)) as total_balance
    FROM group_payment
    ORDER BY person_id, date),

Now we need to use the rank() function to assign rank based on total balance (desc) for an emp_id (ie. rank=1 will be assigned to the largest total balance on a day for a particular emp_id).现在我们需要使用 rank() 函数根据总余额 (desc) 为 emp_id 分配排名(即 rank=1 将分配给特定 emp_id 一天中最大的总余额)。 The query is shown below.查询如下所示。

ranks as (select emp_id,date,total_balance,
rank() over (partition by emp_id order by total_balance desc) as rank
from total_balance ),

Now pick the rows having rank=1 (ie. MAX of total_balance on a day for an emp_id and the date on which it was MAX).现在选择 rank=1 的行(即 emp_id 的一天中 total_balance 的 MAX 和它是 MAX 的日期)。 Order it by total_balance descending and pick the top 5 rows按 total_balance 降序排序并选择前 5 行

emp_order as (select emp_id,date,total_balance 
from ranks
where rank=1
order by 3 desc
limit 5)

Now pick the name from the employee table.现在从员工表中选择姓名。

select emp_id,name, date, total_balance as balance
from emp_order eo
join Employee e on e.emp_id = eo.emp_id
order by 4 desc

Group by and sum allows you to get the total credit for each person into 1 record. Group by 和 sum 允许您将每个人的总学分分成 1 条记录。 You can do a similar thing in a subquery to subtract the debit.您可以在子查询中做类似的事情来减去借方。

Select top 5 emp_id, credit_date, (sum(credit_amount) - 
(select sum(debit_amount) from debit d 
where c.emp_id = d.emp_id and c.credit_date = d.debit_date)
) as total
from Credit c group by emp_id, credit_date order by total

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

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