如何联接三个表?计数和平均SQL

时间:2018-12-02 20:45:14

标签: mysql sql

我在下面的三个表中提供以下信息

@NonNull
@Override
public Filter getFilter() {
    return nameFilter;
}

/*
 * Custom Filter implementation for custom suggestions we provide.
 */
private Filter nameFilter = new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence inputChars) {

        if (inputChars != null) {

            try {
                suggestions.clear();
            } catch (Exception e) {
                e.printStackTrace();
            }

            for (String acTvItem : tempItems) {
                if (acTvItem.toLowerCase().contains(inputChars.toString().toLowerCase())) {
                    suggestions.add(acTvItem);
                }
            }

            FilterResults filterResults = new FilterResults();
            filterResults.values = suggestions;
            filterResults.count = suggestions.size();
            return filterResults;
        } else {
            return new FilterResults();
        }
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        List<String> filterList = (ArrayList<String>) results.values;
        if (results.count > 0) {
            clear();
            if (filterList != null && !filterList.isEmpty()) {

                for (String acTvItem : filterList) {

                if (listener != null && acTvItem.equalsIgnoreCase(inputChars.toString()) && results.count == 1) {
                    listener.onCompleteInputListener(key);
                }

                    add(acTvItem);
                    notifyDataSetChanged();
                }
            }
        }
    }
};


/*  ---------- Interface ----------  */

public interface CompleteInputListener {
    void onCompleteInputListener(String key);
}

我如何确定按月和州列出哪些电子邮件(email_id)驱使员工从事项目(金额)?

project.analytics 
        | proj_id | list_date  | state  
        |   1     |   03/05/10 |   CA   
        |   2     |   04/05/10 |   WA   
        |   3     |   03/05/10 |   WA   
        |   4     |   04/05/10 |   CA   
        |   5     |   03/05/10 |   WA   
        |   6     |   04/05/10 |   CA   

employees.analytics 
        | employee_id  | proj_id  | worked_date
        |   20         |   1      | 3/12/10    
        |   30         |   1      | 3/11/10         
        |   40         |   2      | 4/15/10     
        |   50         |   3      | 3/16/10     
        |   60         |   3      | 3/17/10     
        |   70         |   4      | 4/18/10   

email.forward 
        | email_id    | event_id  | employee_id
        |   1         |   1          | 20    
        |   2         |   2          | 80        
        |   3         |   3          | 40     
        |   4         |   4          | 50     
        |   5         |   5          | 50     
        |   6         |   6          | 60

我很困惑,因为我不确定如何一起操纵这三个桌子。 本质上我希望能够回答

这个email_id在本月和该州有那么多员工从事此项目。

1 个答案:

答案 0 :(得分:0)

类似的事情应该起作用:

SELECT c.email_id, month(b.worked_date), a.state, count(distinct b.employee_id)
FROM project.analytics a
JOIN employees.analytics b 
ON a.project_id = b.project_id
JOIN email.forward c 
on c.employee_id = b.employee_id
GROUP BY c.email_id, month(b.worked_date)

我不确定您使用的是哪种DBMS,因此MONTH()可能存在或不存在,但是您应该能够找到类似的功能。