5

以下のような2つのテーブルがあります(日付形式:yyyy-MM-dd):
1)Table1-EMPLOYEE_OVERTIMES(エイリアス:EO)

EMPLOYEE_ID | OVERTIME_DATE
------------------------------------------------
1           | 2012-04-01
2           | 2012-08-14
3           | 2012-07-22
4           | 2012-10-30
5           | 2012-06-07

2)表2-EMPLOYEE_HOLIDAYS(別名:EH)

EMPLOYEE_ID | START_DATE | END_DATE   |
-----------------------------------------
1           | 2012-03-28 | 2012-04-10
2           | 2012-01-14 | 2012-01-30
3           | 2012-07-15 | 2012-07-25
4           | 2012-10-10 | 2012-10-13
5           | 2012-06-01 | 2012-06-07

テーブルEMPLOYEE_OVERTIMESとEMPLOYEE_HOLIDAYSは、他のテーブルから結合されています。次の基準を満たすすべてのレコードを検索したい:EH.START_DATE <= EO.OVERTIME_DATE <= EH.END_DATE

3)結果表

EMPLOYEE_ID | START_DATE | END_DATE   | OVERTIME_DATE
-------------------------------------------------------
1           | 2012-03-28 | 2012-04-10 | 2012-04-01
3           | 2012-07-15 | 2012-07-25 | 2012-07-22
5           | 2012-06-01 | 2012-06-07 | 2012-06-07
4

3 に答える 3

0

これは機能しますか?

SELECT
  EO.Employee_ID,
  EH.Start_Date,
  EH.End_Date,
  EO.OverTime_Date
FROM
  EMPLOYEE_OVERTIMES EO INNER JOIN
  EMPLOYEE_HOLIDAYS EH ON EO.Employee_ID = EH.Employee_ID
WHERE
  EO.Overtime_Date BETWEEN EH.Start_date and EH.End_Date
于 2013-02-13T08:34:40.173 に答える
0

単純な INNER JOIN を使用できます

;WITH EO AS 
(
    SELECT 1 AS EID, '2012-04-01' AS OD
    UNION ALL
    SELECT 2, '2012-08-14'
    UNION ALL
    SELECT 3, '2012-07-22'
    UNION ALL
    SELECT 4, '2012-10-30'
    UNION ALL
    SELECT 5, '2012-06-07'
), EH AS
(
    SELECT 1 AS EID, '2012-03-28' AS SD, '2012-04-10' AS ED
    UNION ALL
    SELECT 2, '2012-01-14', '2012-01-30'
    UNION ALL
    SELECT 3, '2012-07-15', '2012-07-25'
    UNION ALL
    SELECT 4, '2012-10-10', '2012-10-13'
    UNION ALL
    SELECT 5, '2012-06-01', '2012-06-07'
)

SELECT EH.EID, EH.SD, EH.ED, EO.OD 
FROM EO
INNER JOIN EH
    ON EH.SD <= EO.OD AND EO.OD <= EH.ED
于 2013-02-13T08:37:25.033 に答える