SQL:给定时间内具有员工和员工头衔的销售列表
假设我有三张桌子:SQL:给定时间内具有员工和员工头衔的销售列表,sql,tsql,sql-server-2012,Sql,Tsql,Sql Server 2012,假设我有三张桌子: +------------+ +------------+ +-------------+ | SALES | | EMPLOYEES | | ASSIGNMENTS | +------------+ +------------+ +-------------+ | SaleID | | EmployeeID | | EmployeeID | | SaleDate | | Name | | AssignDate | | Employee
+------------+ +------------+ +-------------+
| SALES | | EMPLOYEES | | ASSIGNMENTS |
+------------+ +------------+ +-------------+
| SaleID | | EmployeeID | | EmployeeID |
| SaleDate | | Name | | AssignDate |
| EmployeeID | +------------| | ReleaseDate |
+------------+ | Description |
+-------------+
我希望我的输出看起来像:
+--------+--------------+--------------------+
| SALEID | NAME | DESCRIPTION |
+--------+--------------+--------------------+
| 91 | John Smith | District Sales |
| 178 | Jane Thomas | District Sales |
| 23 | John Smith | Junior Sales Assoc |
| 42 | Jane Thomas | Junior Sales Assoc |
| 56 | John Smith | Junior Sales Assoc |
| 73 | Suzy Johnson | Regional Sales |
| 156 | John Smith | Regional Sales |
+--------+--------------+--------------------+
如何根据SaleDate是否介于AssignDate和ReleaseDate之间来获取适当的描述?使事情复杂化的是,John当前的任务将以NULL作为发布日期。我需要一些帮助才能朝着正确的方向开始。谢谢大家
编辑
以下是一些示例数据:
+--------+------------+------------+
| SaleID | SaleDate | EmployeeID |
+--------+------------+------------+
| 23 | 2016-05-30 | 15 |
| 42 | 2016-06-12 | 62 |
| 56 | 2016-06-15 | 15 |
| 73 | 2016-10-28 | 49 |
| 91 | 2017-09-02 | 15 |
| 156 | 2018-02-04 | 15 |
| 178 | 2018-03-30 | 62 |
+--------+------------+------------+
+------------+--------------+
| EmployeeID | Name |
+------------+--------------+
| 15 | John Smith |
| 49 | Suzy Johnson |
| 62 | Jane Thomas |
+------------+--------------+
+------------+------------+--------------+--------------------+
| EmployeeID | AssignDate | Release Date | Description |
+------------+------------+--------------+--------------------+
| 15 | 2018-01-12 | NULL | Regional Sales |
| 15 | 2017-07-23 | 2018-01-11 | District Sales |
| 15 | 2016-01-01 | 2017-07-22 | Junior Sales Assoc |
| 49 | 2018-02-10 | NULL | Regional Sales |
| 62 | 2017-09-14 | NULL | District Sales |
| 62 | 2016-04-28 | 2017-09-13 | Junior Sales Assoc |
+------------+------------+--------------+--------------------+
您可以通过内部连接来实现它
如果您只是想获得一些入门帮助: 查看IsNull函数以处理John的发布日期 检查比较之间的差异,以便将销售日期放置在分配日期内。 请注意,您的样本数据不一致,因为Suzy的销售日期早于其指定日期。 既然我现在离开我的办公桌,我也会给你一个有效的解决方案。我强烈建议你先自己解决这个问题
-- for convenience I'm using Common Table Expressions (CTEs) to hold the test data
;with sales (saleId, saleDate, employeeId)
as
(
select 23 , '2016-05-30', 15
union select 42 , '2016-06-12', 62
union select 56 , '2016-06-15', 15
union select 73 , '2016-10-28', 49
union select 91 , '2017-09-02', 15
union select 156 , '2018-02-04', 15
union select 178 , '2018-03-30', 62
)
, employees (employeeId, name)
as
(
select 15 , 'John Smith'
union select 49 , 'Suzy Johnson'
union select 62 , 'Jane Thomas'
)
, assignments (employeeId, assignDate, releaseDate, description)
as
(
select 15 , '2018-01-12', NULL , 'Regional Sales'
union select 15 , '2017-07-23', '2018-01-11', 'District Sales'
union select 15 , '2016-01-01', '2017-07-22', 'Junior Sales Assoc'
union select 49 , '2018-02-10', NULL , 'Regional Sales'
union select 62 , '2017-09-14', NULL , 'District Sales'
union select 62 , '2016-04-28', '2017-09-13', 'Junior Sales Assoc'
)
SELECT s.saleDate, s.saleId, e.name, a.description
FROM sales s
JOIN employees e
ON s.employeeId = e.employeeId
JOIN assignments a
ON s.EmployeeID = a.employeeId
-- NOTE the use of IsNull allowing me to substitute the current date if there is no release date.
and s.saleDate between a.assignDate and ISNULL(a.releaseDate, getdate())
更新您的问题并添加与预期结果和您尝试的查询一致的适当数据示例..我从使用相同日期筛选器的简单联接开始,但它不会返回员工当前工作分配的销售额任何日期不小于NULL。我已为发布日期添加了NULL检查。现在检查更新的queryOoh传递今天的日期int,IsNull命令看起来很完美!我要试一试,看看会有什么结果。
-- for convenience I'm using Common Table Expressions (CTEs) to hold the test data
;with sales (saleId, saleDate, employeeId)
as
(
select 23 , '2016-05-30', 15
union select 42 , '2016-06-12', 62
union select 56 , '2016-06-15', 15
union select 73 , '2016-10-28', 49
union select 91 , '2017-09-02', 15
union select 156 , '2018-02-04', 15
union select 178 , '2018-03-30', 62
)
, employees (employeeId, name)
as
(
select 15 , 'John Smith'
union select 49 , 'Suzy Johnson'
union select 62 , 'Jane Thomas'
)
, assignments (employeeId, assignDate, releaseDate, description)
as
(
select 15 , '2018-01-12', NULL , 'Regional Sales'
union select 15 , '2017-07-23', '2018-01-11', 'District Sales'
union select 15 , '2016-01-01', '2017-07-22', 'Junior Sales Assoc'
union select 49 , '2018-02-10', NULL , 'Regional Sales'
union select 62 , '2017-09-14', NULL , 'District Sales'
union select 62 , '2016-04-28', '2017-09-13', 'Junior Sales Assoc'
)
SELECT s.saleDate, s.saleId, e.name, a.description
FROM sales s
JOIN employees e
ON s.employeeId = e.employeeId
JOIN assignments a
ON s.EmployeeID = a.employeeId
-- NOTE the use of IsNull allowing me to substitute the current date if there is no release date.
and s.saleDate between a.assignDate and ISNULL(a.releaseDate, getdate())