0

我正在尝试将三个临时表与不同指标的开始和结束跨度组合起来,以创建一个具有所有可能跨度的组合表。下面的脚本创建了所有三个临时表,并且(附加的 Excel)显示了结果应该是什么,但我还不能做到这一点。:)

IF OBJECT_ID(N'tempdb.dbo.#aoRepGroupMbrHistory', N'U') IS NOT NULL DROP TABLE #aoRepGroupMbrHistory
CREATE TABLE #aoRepGroupMbrHistory
(
     syUserID INT
     , aoRepGroupID INT
     , Hist_EffectStart DATETIME
     , Hist_EffectiveEnd DATETIME
     , RepGroupName VARCHAR(50)
)
INSERT INTO #aoRepGroupMbrHistory VALUES (17716, 165, '2010-06-03 00:00:00.000', '2012-03-01 23:59:59.997', 'UG - All Skills Reps')
INSERT INTO #aoRepGroupMbrHistory VALUES (17716, 772, '2012-03-02 00:00:00.000', '2012-06-26 23:59:59.997', 'UG - Tier 1 LC Reps')
INSERT INTO #aoRepGroupMbrHistory VALUES (17716, 797, '2012-06-27 00:00:00.000', '2012-11-08 23:59:59.997', 'UG - Aggregators Reps')
INSERT INTO #aoRepGroupMbrHistory VALUES (17716, 165, '2012-11-09 00:00:00.000', '2012-11-29 23:59:59.997', 'UG - All Skills Reps')
INSERT INTO #aoRepGroupMbrHistory VALUES (17716, 797, '2012-11-30 00:00:00.000', '9999-01-01 00:00:00.000', 'UG - Aggregators Reps')

IF OBJECT_ID(N'tempdb.dbo.#TeamMemberHistory', N'U') IS NOT NULL DROP TABLE #TeamMemberHistory
CREATE TABLE #TeamMemberHistory
(
     syUserID INT
     , CRMTeamID INT
     , Active INT
     , HistoryStartDate DATETIME
     , HistoryEndDate DATETIME
)
INSERT INTO #TeamMemberHistory VALUES (17716, 637, 1, '2011-04-20 00:00:00.000', '2012-05-15 23:59:59.997')
INSERT INTO #TeamMemberHistory VALUES (17716, 634, 1, '2012-05-16 00:00:00.000', '2012-06-20 23:59:59.997')
INSERT INTO #TeamMemberHistory VALUES (17716, 628, 1, '2012-06-21 00:00:00.000', '2012-10-23 23:59:59.997')
INSERT INTO #TeamMemberHistory VALUES (17716, 636, 1, '2012-10-24 00:00:00.000', '2012-12-13 23:59:59.997')
INSERT INTO #TeamMemberHistory VALUES (17716, 628, 1, '2012-12-14 00:00:00.000', '2013-04-17 23:59:59.997')
INSERT INTO #TeamMemberHistory VALUES (17716, 676, 1, '2013-04-18 00:00:00.000', '9999-01-01 00:00:00.000')



IF OBJECT_ID(N'tempdb.dbo.#TeamHistory', N'U') IS NOT NULL DROP TABLE #TeamHistory
CREATE TABLE #TeamHistory
(
     CRMTeamID INT
     , TeamOwner_TeamID INT
     , Active INT
     , TeamName VARCHAR(25)
     , HistoryStartDate DATETIME
     , HistoryEndDate DATETIME
)
INSERT INTO #TeamHistory VALUES (628, 222, 1, 'Team 126', '2011-01-10 00:00:00.000', '9999-01-01 00:00:00.000')
INSERT INTO #TeamHistory VALUES (634, 168, 1, 'Team 132', '2010-01-07 00:00:00.000', '9999-01-01 00:00:00.000')
INSERT INTO #TeamHistory VALUES (636, 935, 1, 'Team 134', '2011-05-09 00:00:00.000', '2013-02-12 23:59:59.997')
INSERT INTO #TeamHistory VALUES (636, 935, 1, 'Team 134', '2013-02-13 00:00:00.000', '9999-01-01 00:00:00.000')
INSERT INTO #TeamHistory VALUES (637, 192, 1, 'Team 135', '2010-08-03 00:00:00.000', '9999-01-01 00:00:00.000')
INSERT INTO #TeamHistory VALUES (676, 242, 1, 'Team 360', '2011-05-21 00:00:00.000', '9999-01-01 00:00:00.000')


SELECT * FROM #aoRepGroupMbrHistory ORDER BY Hist_EffectStart
SELECT * FROM #TeamMemberHistory ORDER BY HistoryStartDate
SELECT * FROM #TeamHistory ORDER BY CRMTeamID, HistoryStartDate


**RESULTS:**
syUserID     TeamName   CRMTeamID   TeamOwner_TeamId     aoRepGroupID   RepGroupName    dwEffectiveBeginDate    dwEffectiveEndDate
17716   Team 135    637  192    165 UG - All Skills Reps     01/01/2012 03/01/2012
17716   Team 135    637  192    772 UG - Tier 1 LC Reps  03/02/2012 05/15/2012
17716   Team 132     634    168 772  UG - Tier 1 LC Reps    05/16/2012  06/20/2012
17716    Team 126   628 222  772    UG - Tier 1 LC Reps 06/21/2012   06/26/2012
17716    Team 126   628 222  797    UG - Aggregators Reps   06/27/2012   10/23/2012
17716    Team 134   636 222  797    UG - Aggregators Reps   10/24/2012   11/08/2012
17716    Team 134   636 222  165    UG - All Skills Reps    11/09/2012   11/29/2012
17716   Team 134     636    222 797  UG - Aggregators Reps  11/30/2012  12/13/2012
17716    Team 126   628 222  797    UG - Aggregators Reps   12/14/2012   04/17/2013
17716   Team 360     676    424 797  UG - Aggregators Reps  04/18/2013  01/01/9999
4

1 回答 1

0

可能这对你有帮助-

DECLARE @aoRepGroupMbrHistory TABLE 
(
       syUserID INT
     , aoRepGroupID INT
     , Hist_EffectStart DATETIME
     , Hist_EffectiveEnd DATETIME
     , RepGroupName VARCHAR(50)
)
INSERT INTO @aoRepGroupMbrHistory 
VALUES 
      (17716, 165, '2010-06-03 00:00:00.000', '2012-03-01 23:59:59.997', 'UG - All Skills Reps')
    , (17716, 772, '2012-03-02 00:00:00.000', '2012-06-26 23:59:59.997', 'UG - Tier 1 LC Reps')
    , (17716, 797, '2012-06-27 00:00:00.000', '2012-11-08 23:59:59.997', 'UG - Aggregators Reps')
    , (17716, 165, '2012-11-09 00:00:00.000', '2012-11-29 23:59:59.997', 'UG - All Skills Reps')
    , (17716, 797, '2012-11-30 00:00:00.000', '9999-01-01 00:00:00.000', 'UG - Aggregators Reps')

DECLARE @TeamMemberHistory TABLE
(
       syUserID INT
     , CRMTeamID INT
     , Active INT
     , HistoryStartDate DATETIME
     , HistoryEndDate DATETIME
)
INSERT INTO @TeamMemberHistory 
VALUES 
      (17716, 637, 1, '2011-04-20 00:00:00.000', '2012-05-15 23:59:59.997')
    , (17716, 634, 1, '2012-05-16 00:00:00.000', '2012-06-20 23:59:59.997')
    , (17716, 628, 1, '2012-06-21 00:00:00.000', '2012-10-23 23:59:59.997')
    , (17716, 636, 1, '2012-10-24 00:00:00.000', '2012-12-13 23:59:59.997')
    , (17716, 628, 1, '2012-12-14 00:00:00.000', '2013-04-17 23:59:59.997')
    , (17716, 676, 1, '2013-04-18 00:00:00.000', '9999-01-01 00:00:00.000')

DECLARE @TeamHistory TABLE
(
       CRMTeamID INT
     , TeamOwner_TeamID INT
     , Active INT
     , TeamName VARCHAR(25)
     , HistoryStartDate DATETIME
     , HistoryEndDate DATETIME
)
INSERT INTO @TeamHistory 
VALUES 
      (628, 222, 1, 'Team 126', '2011-01-10 00:00:00.000', '9999-01-01 00:00:00.000')
    , (634, 168, 1, 'Team 132', '2010-01-07 00:00:00.000', '9999-01-01 00:00:00.000')
    , (636, 935, 1, 'Team 134', '2011-05-09 00:00:00.000', '2013-02-12 23:59:59.997')
    , (636, 935, 1, 'Team 134', '2013-02-13 00:00:00.000', '9999-01-01 00:00:00.000')
    , (637, 192, 1, 'Team 135', '2010-08-03 00:00:00.000', '9999-01-01 00:00:00.000')
    , (676, 242, 1, 'Team 360', '2011-05-21 00:00:00.000', '9999-01-01 00:00:00.000')

SELECT
      th.syUserID
    , t.TeamName
    , t.CRMTeamID
    , t.TeamOwner_TeamID
    , h.aoRepGroupID
    , h.RepGroupName
    , dwEffectiveBeginDate = CONVERT(VARCHAR(10), th.HistoryStartDate, 101)
    , dwEffectiveEndDate = CONVERT(VARCHAR(10), th.HistoryEndDate, 101) 
FROM @TeamHistory t
JOIN @TeamMemberHistory th ON t.CRMTeamID = th.CRMTeamID
JOIN @aoRepGroupMbrHistory h ON th.syUserID = h.syUserID
于 2013-04-30T06:17:42.983 回答