假设我有三个表:
+------------+ +------------+ +-------------+
| 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 |
+------------+------------+--------------+--------------------+
答案 0 :(得分:1)
如果您只是想获得一些帮助,那就开始吧
IsNull
函数以处理John的发布日期between
比较,以将销售日期置于分配日期之内。 由于我现在要离开办公桌,所以我还会为您提供可行的解决方案。我强烈建议您先尝试自己解决它。
-- 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())
答案 1 :(得分:0)
您可以通过INNER JOIN
SELECT
SaleID
, Name
, Description
FROM SALES AS S
INNER JOIN EMPLOYEES AS E ON S.EmployeeID = E.EmployeeID
INNER JOIN ASSIGNMENTS AS A ON A.EmployeeID = E.EmployeeID
WHERE A.AssignDate IS NOT NULL
AND A.ReleaseDate IS NOT NULL
AND S.SaleDate >= A.AssignDate AND S.SaleDate <= A.ReleaseDate