MySQL:如何按月收看活跃会员

时间:2017-11-20 04:19:28

标签: mysql

我有前一年的工作成员,减去前一年减轻员工的负担,然后得到上个月的缓解清单并从结果集中减去。然后在当月添加新添加的成员。

SQL Fiddle Link 我感觉我们可以对当前查询做很多改进。但是现在我没有想法,有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

IF 我已正确解释您的现有查询,我建议如下:

select
      mnth.num, count(*)
from (
    select 1 AS num union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all 
    select 7 union all select 8 union all select 9 union all select 10 union all select 11 union all select 12
    ) mnth
left join (    
      select
            e.emp_id
          , case 
               when e.hired_date < date_format(current_date(), '%Y-01-01') then 1
               else month(e.hired_date)
               end AS start_month
          , case 
               when es.relieving_date < date_format(current_date(), '%Y-01-01') then 0
               when es.relieving_date >= date_format(current_date(), '%Y-01-01') then month(es.relieving_date)
               else month(current_date())
               end AS end_month
      from employee e
      left join employee_separation es on e.emp_id = es.emp_id
      ) emp on mnth.num between emp.start_month and emp.end_month  
where mnth.num <= month(current_date())
group by
      mnth.num
;

这产生了以下结果(current_date()于2017年11月21日

| num | count(*) |
|-----|----------|
|   1 |        6 |
|   2 |        7 |
|   3 |        8 |
|   4 |        9 |
|   5 |       10 |
|   6 |        9 |
|   7 |       10 |
|   8 |       11 |
|   9 |       12 |
|  10 |       13 |
|  11 |       14 |

DEMO

根据数据量的不同,在emp子查询中添加where子句可能有所帮助,这也会影响案例表达式:

          , case 
               when es.relieving_date >= date_format(current_date(), '%Y-01-01') then month(es.relieving_date)
               else month(current_date())
               end AS end_month
      from employee e
      left join employee_separation es on e.emp_id = es.emp_id
      where es.relieving_date >= date_format(current_date(), '%Y-01-01')

答案 1 :(得分:0)

我认为您需要做的是让所有已经在员工表中工作的员工:

import java.util.ArrayList;
import java.util.Scanner;

public class Store {
    ArrayList<Customer> Customers = new ArrayList<Customer>();
   Customer[] csa = new Customer[1000];

    public void addSale(String customerName, double amount) {
   String cn = customerName;
   double am = amount;
   Customer cs = new Customer(cn, am);
   Customers.add(cs);
}

public String nameOfBestCustomer() {
   String name = null;
   //Customer csa=new Customer();
   double largest = Customers.get(0).getAmount();

   // gives me:java.lang.NullPointerException

   for (int i = 1; i < Customers.size(); i++) {

       if (largest < Customers.get(i).getAmount()) {
           largest = Customers.get(i).getAmount();
           name = Customers.get(i).getName();
       }
   }

   // return name+""+largest;
   return name;
}


public static void main(String[] args) {

   Store s = new Store();
   double am;

   Scanner scanner = new Scanner(System.in);

   while (true) {

       System.out.println("Enter Customer name:");
       String cn = scanner.next();

       if (cn.equals("done")) {
           am = 0;
           scanner.close();

           break;
       } else {
           System.out.println("Enter Amount:");
           am = scanner.nextDouble();
           s.addSale(cn, am);

       }
   }

   System.out.println("Best customer " + s.nameOfBestCustomer());

}

           }

class Customer {
private String name;
private double amount;


public Customer(){

}
//@SuppressWarnings("null")
public  Customer(String name,double price) {
    this.name=name;
    this.amount= price;

}





public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public double getAmount() {
    return amount;
}
public void setAmount(double amount) {
    this.amount = amount;
}

                 }




Output :
Enter Customer name:
ramesh
Enter Amount:
100
Enter Customer name:
pramod
Enter Amount:
200
Enter Customer name:
done
Best customer pramod

然后使用以下方式获取其救助日期仍未来的员工名单:

SELECT * FROM employee WHERE hired_date<= CURRENT_DATE;

然后按照重播日期的月份和年份加入两个结果和分组,如下所示:

SELECT * FROM employee_separation WHERE relieving_date > CURRENT_DATE;

这是sql小提琴上的Demo