我正在尝试为由其他表格数据组成的报表制作表格。 “报告”表需要包含每个站点需要多少假期和病假时间,但员工要分别花费几个小时的信息。所有这些表都是通过外键关系连接的。
[dbo]。[员工]:
$('#contact-modal').on('show.bs.modal', function() {
$("input[name=name]").val('ëtestí');
});
网站:
[dbo]。[网站]:
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#contact-modal">
modal
</button>
<div id="contact-modal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<a class="close" data-dismiss="modal">◊</a>
<h3>Contact Form</h3>
</div>
<form id="contactForm" name="contact" role="form">
<div class="modal-body">
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" class="form-control">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" class="form-control">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea name="message" class="form-control"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-success" id="submit">
</div>
</form>
</div>
</div>
</div>
[dbo]。[HolidayRequestForm]:
EmployeeID |Name |SiteID |
------------+----------------+---------------+
1 | Joe |1 |
2 | Tom |2 |
3 | Mary |3 |
4 | Bill |1 |
5 | 16 |2 |
[dbo]。[SickLeaveRequestForm]:
SiteID |Site Name |
------------+----------------+
1 | SiteA |
2 | SiteB |
3 | SiteC |
我已经尝试通过使用联接来实现此目的,然后从不同的表中选择相关数据,但是我没有运气。
EmployeeID |HolidayHours |
------------+----------------+
1 | 8 |
2 | 16 |
3 | 16 |
创建视图更好吗?
这是预期的结果!
EmployeeID |SickHours |
------------+----------------+
1 | 8 |
2 | 8 |
3 | 8 |
答案 0 :(得分:2)
SELECT dbo.Site.SiteName,
SUM(dbo.HolidayRequestForm.HolidayHours) AS HolidayHours,
SUM(dbo.SickLeaveRequestForm.SickHours) AS SickHours
FROM dbo.Employee INNER JOIN
dbo.Site ON dbo.Employee.SiteID = dbo.Site.SiteID INNER JOIN
dbo.HolidayRequestForm ON dbo.Employee.EmployeeID =
dbo.HolidayRequestForm.EmployeeID INNER JOIN
dbo.SickLeaveRequestForm ON dbo.Employee.EmployeeID = dbo.SickLeaveRequestForm.EmployeeID
GROUP BY dbo.Site.SiteName
创建视图更好吗? 这完全取决于这是一次查询还是要定期使用它来进行报告。
此外,如果您想包括所有没有员工请病假或休假的网站(例如,对于MSSQL):
SELECT dbo.Site.SiteName,
{ fn IFNULL(SUM(dbo.HolidayRequestForm.HolidayHours), 0) } AS HolidayHours,
{ fn IFNULL(SUM(dbo.SickLeaveRequestForm.SickHours), 0) } AS SickHours
FROM dbo.Employee
LEFT OUTER JOIN dbo.HolidayRequestForm ON dbo.Employee.EmployeeID = dbo.HolidayRequestForm.EmployeeID
LEFT OUTER JOIN dbo.SickLeaveRequestForm ON dbo.Employee.EmployeeID = dbo.SickLeaveRequestForm.EmployeeID
RIGHT OUTER JOIN dbo.Site ON dbo.Employee.SiteID = dbo.Site.SiteID
GROUP BY dbo.Site.SiteName
答案 1 :(得分:1)
您可以尝试以下查询。
create table Employee(EmployeeID int, Name varchar(20), SiteID int)
insert into Employee values
(1, 'Joe', 1),
(2, 'Tom', 2),
(3, 'Mary', 3),
(4, 'Bill', 1),
(5, '16', 2)
create table [Site] (SiteID int, [Site Name] Varchar(20))
insert into [Site] Values
(1, 'SiteA'),
(2, 'SiteB'),
(3, 'SiteC')
Create table HolidayRequestForm(EmployeeID int, HolidayHours int)
insert into HolidayRequestForm Values
(1, 8),
(2, 16),
(3, 16)
Create table SickLeaveRequestForm(EmployeeID int, SickHours int)
insert into SickLeaveRequestForm Values
(1 ,8),
(2,8),
(3,8)
Select [Site Name], SUM(HolidayHours) as HolidayHours, SUM(SickHours) as
SickHours
from(
Select
[Site].[Site Name], HolidayRequestForm.HolidayHours, SickLeaveRequestForm.SickHours
from [Site]
inner join Employee on [Site].SiteId = Employee.SiteId
inner join HolidayRequestForm on HolidayRequestForm.EmployeeID = Employee.EmployeeID
inner join SickLeaveRequestForm on Employee.EmployeeID = SickLeaveRequestForm.EmployeeID
)a group by [Site Name]