尝试进行生成此类结果的查询
EmpId, EmpSickLeave,EmpCasualLeave, EmpAnnualLeave, TotalLeave
1, 1, 0, 0 1
2, 0, 0, 2 2
3, 0, 1, 0 1
4, 0, 1, 0 1
5, 1, 0, 0 1
虽然我有两张桌子
表EmpLeave ::包含一列EmpId,Date,EmpLeaveTypeID
表EmpLeaveType :: include EmpLeaveTypeID和LeaveName
DATA表格
EmpLeaveType Table
EmpLeaveTypeID , LeaveName
1, Sick Leave
2, Annual
3, Casual
EmpLeave Table
column EmpId, Date, EmpLeaveTypeID
1, 2015-07-01, 1
3, 2015-07-02, 2
5, 2015-07-04, 1
4, 2015-07-04, 2
2, 2015-07-05, 2
2, 2015-07-07, 2
我正在拉我的头发,无法提出这种结果。这可能吗?
EEmpId, EmpSickLeave,EmpCasualLeave, EmpAnnualLeave, TotalLeave
1, 1, 0, 0 1
2, 0, 0, 2 2
3, 0, 1, 0 1
4, 0, 1, 0 1
5, 1, 0, 0 1
我尝试过这个错误的事情!
SELECT * count(EmpLeaveTypeID) FROM `EmpLeaveType`
WHERE SwitchDate Between '2015-07-01' AND '2015-07-28'
group by EmpLeaveType, EmpId
答案 0 :(得分:1)
请尝试下面的SELECT查询。它没有考虑到有一天可能会改变休假类型。 SQL小提琴链接http://sqlfiddle.com/#!9/0fd13/2 查询只是执行条件聚合来计算每个员工的叶子。
SELECT
EmpId,
SUM(CASE WHEN EmpLeaveTypeID=1 THEN 1 ELSE 0 END) as EmpSickLeave,
SUM(CASE WHEN EmpLeaveTypeID=2 THEN 1 ELSE 0 END) as EmpCasualLeave,
SUM(CASE WHEN EmpLeaveTypeID=3 THEN 1 ELSE 0 END) as EmpAnnualLeave,
COUNT(1) as TotalLeave
FROM `EmpLeave`
WHERE Date Between '2015-07-01' AND '2015-07-28'
group by EmpId
答案 1 :(得分:1)
这看起来是一个条件计数,您只想计算某些行。为此,您可以使用带有select EmpId as EEmpId,
sum(case when EmpLeaveTypeID = 1 then 1 else 0 end) as EmpSickLeave,
sum(case when EmpLeaveTypeID = 2 then 1 else 0 end) as EmpCasualLeave,
sum(case when EmpLeaveTypeID = 3 then 1 else 0 end) as EmpAnnualLeave,
count(*) as TotalLeave
from EmpLeave
where ...
group by EmpId
语句的0
:
1
这样做会根据EmpLeaveTypeID
是否匹配为每行添加COUNT
或TotalLeave
。然后,您可以使用{{1}}获取{{1}}列的总行数,如上所示。
答案 2 :(得分:0)
我省略了package com.you.take.me.to.funky;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.path.Files;
import java.nio.path.Path;
import java.nio.path.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class town {
private static List<String> list = new ArrayList<>();
private static List<String> misclist = new ArrayList<>();
private static Pattern p0 = Pattern.compile("(Bench:)(.*)");
private static Pattern p1 = Pattern.compile("(Author):(.*)");
private static Pattern p2 = Pattern.compile("JUDGMENT(.*?)J[.]");
private static Pattern p3 = Pattern.compile("(([H|h]on)|(HON)).*((ble)|BLE)(.*)");
private static Pattern p4 = Pattern.compile(",\\s*([^,]+),[^,]*\\b(J|JJ)\\.");//CAKE
private static Pattern p5 = Pattern.compile("\\s\\w*(?=\\w*[,]\\sJ[.]*\\b)");
private static Pattern p4 =
Pattern.compile("\\w*(?=\\w*[,]*\\s*((JJ)|(L.J)|(C.J)|(J))[.]\\s\\b)");
private static Pattern p6 =
Pattern.compile("BENCH:.*?BENCH.*?CITATION", Pattern.DOTALL);
public static void main(String[] args) {
Path path = Paths.get(args[0]);
String str;
try {
str = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
Matcher match = p0.matcher(str);
if (match.find()) {
Matcher match1 = p1.matcher(str);
if (match1.find()) {
// pathname judgename authorname
list.add(path.toString() +
"\t" + match.group(2) +
"\t" + match1.group(2));
System.out.println(match1.group(2));
} else {
list.add(path.toString() + "\t" + match.group(2) + "\t ");
System.out.println(match.group(2));
}
} else {
match = p1.matcher(str);
if (match.find()) {
list.add(path.toString() + "\t \t" + match.group(2));
} else {
match = p2.matcher(str);
if (match.find()) {
list.add(path.toString() + "\t" + match.group() + "\t ");
} else {
match = p3.matcher(str);
if (match.find()) {
list.add(path.toString() + "\t" + match.group() + "\t ");
} else {
match = p4.matcher(str);
if (match.find()) {
//do nothing
} else {
match = p5.matcher(str);
if (match.find()) {
list.add(path.toString() + "\t" + match.group() + "\t ");
System.out.println(path.toString());
} else {
match = p6.matcher(str);
if (match.find()) {
System.out.println("DEBUG MARKER");
} else {
// list of documents which have no Judgenames
misclist.add(path.toString());
String name = UUID.randomUUID().toString();
try {
PrintWriter pw = new PrintWriter("/h/g/d/d/m/" + name);
pw.write(str);
pw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}
}
}
,因为它是一个简单的date between
添加。
尝试下面的SQL查询:
where
答案 3 :(得分:0)
请检查以下内容:
SELECT
temp.EmpId ,
temp.EmpSickLeave,
temp.EmpCasualLeave,
temp.EmpAnnualLeave,
(temp.EmpSickLeave + temp.EmpCasualLeave + temp.EmpAnnualLeave )
as TotalLeave
FROM
( SELECT
EmpId,
SUM( CASE WHEN EmpLeaveTypeID=1 THEN 1 ELSE 0 END ) as EmpSickLeave,
SUM( CASE WHEN EmpLeaveTypeID=2 THEN 1 ELSE 0 END ) as EmpCasualLeave,
SUM( CASE WHEN EmpLeaveTypeID=3 THEN 1 ELSE 0 END ) as EmpAnnualLeave
FROM EmpLeave
WHERE Date Between '2015-07-01' AND '2015-07-28'
group by EmpId ) temp