2015-01-28 6 views
1

SQL Server Express (고급 서비스 포함), 버전 10.50.1600.1을 사용하고 있습니다.FROM 절의 UNPIVOT 및 JOIN 구문 오류

하나의 행에서 여러 행을 작성하려면 UNPIVOT을 사용해야하고 마스터 데이터 테이블에서 필드를 가져 오려면 JOINS를 사용해야합니다.

나는 2 개의 작동하지만 별도의 SELECT 문을 만들 수 있습니다. 첫 번째는 UNPIVOT이고 두 번째는 JOIN을 포함합니다. 그러나 나는 그 (것)들을 함께 일하게 단순히 만들 수 없다! UNPIVOT 다음에 JOIN을 넣을 때, 항상 열 8156 (열이 여러 번 지정됨) 또는 4104 (여러 부분 식별자가 바인딩 될 수 없음)가 표시됩니다 ...

예제 데이터는이 SQL에 있습니다 바이올린 : 여기 http://sqlfiddle.com/#!2/452de/1

샘플 데이터의

Table TIMESHEET_LINE 

PROJECT_ID DATE1 DATE7 HOUR1 HOUR2 HOUR3 HOUR4 HOUR5 HOUR6 HOUR7 
16 2011-10-03 2011-10-09 0 0 0,5 0 0 0 0 
18 2011-10-03 2011-10-09 0 0 0,01111111 0 0 0 0 
18 2011-10-03 2011-10-09 0 0 0,001944444 0 0 0 0 
28 2011-10-03 2011-10-09 0 0 0 2 0 0 0 
13 2011-10-03 2011-10-09 0 0 0 0 0 0 0 
18 2011-10-03 2011-10-09 0 0 0 0 1,250556 0 0 
18 2011-10-03 2011-10-09 0 0 0 0 0,7141666 0 0 

표 프로젝트

Table PROJECT 

PROJECT_ID PROJECT_NUMBER PROJECT_NAME 
13 30013 Control Venta Negativa 
16 24464 Zonas de Transporte 
18 PRBRCOM2012_12 Garantia 
28 24466 Embalagens Retornáveis 

예상 출력은 다음과 같습니다

SELECT 
    [USER_ID], 
    [PROJECT_ID], 
    [TASK_GROUP_ID], 
    [TASK_ID], 
    DATEADD 
    (
     DAY, 
     CAST(RIGHT([WeekDay],1) 
      AS int) - 1, 
     Date1 
    ) As 'Date', 

    SUM(Hours) AS 'Hours' 

FROM [aceproject].[dbo].[TIMESHEET_LINE] 
    UNPIVOT 
    (
     Hours for [WeekDay] in (HOUR1, HOUR2, HOUR3, HOUR4, HOUR5, HOUR6, HOUR7) 
    ) upvt 

WHERE 
    [USER_ID] = '18' 

GROUP BY 
    [USER_ID], 
    [PROJECT_ID], 
    [TASK_GROUP_ID], 
    [TASK_ID], 
    [WeekDay], 
    [Date1] 

SQL 문은 테이블을 조인 :

SELECT 
    TSL.PROJECT_ID, 
    P.PROJECT_NUMBER, 
    P.PROJECT_NAME 

FROM [TIMESHEET_LINE] AS TSL 

    INNER JOIN [aceproject].[dbo].[PROJECT] AS P with (nolock) ON P.PROJECT_ID = TSL.PROJECT_ID 

PROJECT_NUMBER PROJECT_NAME DATE HOUR 
30013 Control Venta Negativa 2011-10-03 0 
30013 Control Venta Negativa 2011-10-04 0 
30013 Control Venta Negativa 2011-10-05 0,5 
30013 Control Venta Negativa 2011-10-06 0 
30013 Control Venta Negativa 2011-10-07 0 
30013 Control Venta Negativa 2011-10-08 0 
30013 Control Venta Negativa 2011-10-09 0 
PRBRCOM2012_12 Garantia 2011-10-03 0 
PRBRCOM2012_12 Garantia 2011-10-04 0 
PRBRCOM2012_12 Garantia 2011-10-05 0,01111111 
PRBRCOM2012_12 Garantia 2011-10-06 0 
PRBRCOM2012_12 Garantia 2011-10-07 0 
PRBRCOM2012_12 Garantia 2011-10-08 0 
PRBRCOM2012_12 Garantia 2011-10-09 0 

SQL 문이 TIMESHEET_LINE 테이블을 피벗 해제하기

나는 여러 가지 방법으로 그것들을 조합하려고 시도했다. 먼저 사용하여 동일한 SELECT 문에 조인 UNPIVOT :

SELECT 
    P.PROJECT_NUMBER, 
    P.PROJECT_NAME, 
    TSL.[USER_ID], 
    TSL.[PROJECT_ID], 
    TSL.[TASK_GROUP_ID], 
    TSL.[TASK_ID], 
    DATEADD 
    (
     DAY, 
     CAST(RIGHT(upvt.[WeekDay],1) 
      AS int) - 1, 
     TSL.Date1 
    ) As 'Date', 

    SUM(upvt.Hours) AS 'Hours' 

FROM [TIMESHEET_LINE] AS TSL 

    INNER JOIN PROJECT AS P with (nolock) ON P.PROJECT_ID = TSL.PROJECT_ID 

    UNPIVOT 
    (
     Hours for [WeekDay] in (HOUR1, HOUR2, HOUR3, HOUR4, HOUR5, HOUR6, HOUR7) 
    ) upvt 

WHERE 
    TSL.[USER_ID] = '18' 

GROUP BY 
    TSL.[USER_ID], 
    TSL.[PROJECT_ID], 
    TSL.[TASK_GROUP_ID], 
    TSL.[TASK_ID], 
    upvt.[WeekDay], 
    TSL.[Date1] 

는 또한 별도의 문장에서 선택 시도 :
SELECT 
    Project_ID, 
    Client_Country, 
    Project_Create_By, 
    Resource_Country, 
    Resource_IPN, 

    DATEADD 
    (
     DAY, 
     CAST(RIGHT([WeekDay],1) 
      AS int) - 1, 
     Date1 
    ) As 'Date', 

    Hours AS 'Hours' 

FROM 
(
    SELECT 

     TSL.[USER_ID], 
     TSL.[PROJECT_ID], 
     TSL.[TASK_GROUP_ID], 
     TSL.[TASK_ID], 

     TSL.PROJECT_ID AS 'Project_ID', 
     Left(C.CLIENT_NAME,2) AS 'Client_Country', 
     LTRIM(PU.USERNAME) AS 'Project_Create_By', 
     LEFT(UG1.USER_GROUP_NAME,2) AS 'Resource_Country', 
     LTRIM(U.USERNAME) AS 'Resource_IPN', 

     TSL.DATE1, 
     TSL.HOUR1, 
     TSL.HOUR2, 
     TSL.HOUR3, 
     TSL.HOUR4, 
     TSL.HOUR5, 
     TSL.HOUR6, 
     TSL.HOUR7 

    FROM [aceproject].[dbo].[TIMESHEET_LINE] AS TSL 

     INNER JOIN [aceproject].[dbo].[PROJECT] AS P with (nolock) ON P.PROJECT_ID = TSL.PROJECT_ID 
     LEFT JOIN CLIENT AS C with (nolock) ON C.CLIENT_ID = P.CLIENT_ID 
     LEFT JOIN USERS AS PU with (nolock) ON (PU.COMPANY_ID = P.COMPANY_ID and PU.USER_ID = P.PROJECT_CREATOR_ID) 
     LEFT JOIN USERS AS U with (nolock) ON U.USER_ID  = TSL.USER_ID 
     LEFT JOIN USER_GROUP AS UG1 with (nolock) ON (UG1.COMPANY_ID = U.COMPANY_ID and UG1.USER_GROUP_ID = U.USER_GROUP_ID) 

) d 

UNPIVOT 
(
    Hours for [WeekDay] in (HOUR1, HOUR2, HOUR3, HOUR4, HOUR5, HOUR6, HOUR7) 
) upvt 

는 또한 서브 SELECT로 UNPIVOT와 대향 시도. 하지만 아무 것도 효과가 없었습니다. 사전에

감사합니다!

+0

입니까? –

+0

안녕하세요 Sarath, 설명에 예상되는 출력을 추가했습니다. 다음 필드는 'PROJECT_NUMBER PROJECT_NAME DATE HOUR'입니다. – Rafael

답변

2

좋아, 나는 그것을 마침내 분류했다! =) 먼저 조인을 확인한 다음 당신은 그 위에 선택 중첩합니다 UR 예상 출력은 무엇

SELECT TOP 100 
    TIMESHEET_LINE_ID, 
    PROJECT_NUMBER, 
    DATEADD 
    (
     DAY, 
     CAST(RIGHT([WeekDay],1) 
      AS int) - 1, 
     Date1 
    ) As 'Date', 

    Hours AS 'Hours' 

FROM 
(
    SELECT 
     P.PROJECT_NUMBER, 
     TSL.TIMESHEET_LINE_ID, 
     TSL.DATE1, 
     TSL.HOUR1, 
     TSL.HOUR2, 
     TSL.HOUR3, 
     TSL.HOUR4, 
     TSL.HOUR5, 
     TSL.HOUR6, 
     TSL.HOUR7 
    FROM 
     [aceproject].[dbo].[TIMESHEET_LINE] AS TSL 
     INNER JOIN PROJECT AS P with (nolock) ON P.PROJECT_ID = TSL.PROJECT_ID 
) d 
    UNPIVOT 
    (
     Hours for [WeekDay] in (HOUR1, HOUR2, HOUR3, HOUR4, HOUR5, HOUR6, HOUR7) 
    ) upvt