2017-04-10 1 views
-1

SQL Server 2008에 코드가 있고 다른 행에 조인해야합니다. SQL Server : 한 행에 두 행

코드

select 
    usuario.SK_Representative, 
    sum(devo.NM_Material) as deve 
from 
    DW_DTS_Representative usuario 
inner join 
    DIS_DTS_Sales_Return_Fact devo on devo.SK_Representative = usuario.SK_Representative 
inner join 
    DW_DTS_Operation_Nature cfop on cfop.SK_Operation_Nature = devo.SK_Operation_Nature 
inner join 
    DW_DTS_Emitter cli on cli.SK_Emitter = devo.SK_Emitter 
where 
    devo.CD_Currency = '0' 
    and devo.CD_Site = '001' 
    and cli.CD_Customer_Group = '10' 
    and usuario.SK_Representative != '2' 
    and usuario.SK_Representative != '3' 
    and usuario.SK_Representative != '4' 
    and usuario.SK_Representative != '41' 
    and usuario.SK_Representative != '48' 
    and usuario.SK_Representative != '49' 
    and usuario.SK_Representative != '43' 
    and devo.DT_Day between DateAdd(yyyy, DateDiff(yyyy,0,GetDate()), 0) 
         and dateadd([month], datediff([month], '18991231', dateadd(month, -1, getdate())), '18991231') 
group by 
    usuario.SK_Representative 
입니다

해당 스크립트의 결과는 다음과 같습니다

enter image description here

하지만 난에 대한 SK_Representative에 숫자 '33'에 대한 SK_Representative 정보를 집계 할 필요가 번호 '47'. SK_Representative 인 기본 키를 잃지 않고이 작업을 수행해야합니다.

+0

현재 결과입니까 아니면 원하는 결과입니까? –

+0

"숫자 33에 대한 SK_ 대표 값 정보를 '47 '에 대한 SK_ 대표 형으로 통합" –

+0

SK_Representative'33 '의'deve '값은 SK_Representative'47 ' –

답변

0

내가 귀하의 질문을 정확하게 이해한다면, 나는 이것이 당신이 원하는 것이라고 믿습니다. 또한 usuario.SK_Representative NOT IN을 사용하여 @ juan-carlos-oropeza의 제안을 고려했습니다.

select 
    CASE 
     WHEN usuario.SK_Representative IN ('33', '47') THEN '47' 
     ELSE usuario.SK_Representative 
    END AS SK_Representative, 
    sum(devo.NM_Material) as deve 
from 
    DW_DTS_Representative usuario 
inner join 
    DIS_DTS_Sales_Return_Fact devo on devo.SK_Representative = usuario.SK_Representative 
inner join 
    DW_DTS_Operation_Nature cfop on cfop.SK_Operation_Nature = devo.SK_Operation_Nature 
inner join 
    DW_DTS_Emitter cli on cli.SK_Emitter = devo.SK_Emitter 
where 
    devo.CD_Currency = '0' 
    and devo.CD_Site = '001' 
    and cli.CD_Customer_Group = '10' 
    AND usuario.SK_Representative NOT IN ('2', '3', '4', '41', '48', '49', '43') 
    and devo.DT_Day between DateAdd(yyyy, DateDiff(yyyy,0,GetDate()), 0) 
         and dateadd([month], datediff([month], '18991231', dateadd(month, -1, getdate())), '18991231') 
group by 
    CASE 
     WHEN usuario.SK_Representative IN ('33', '47') THEN '47' 
     ELSE usuario.SK_Representative 
    END AS SK_Representative 

희망이 있으면 올바른 길로 가야합니다.

노엘

+0

그것은 효과가 있었지만 코드의 마지막 경우에서 별칭을 제거했습니다. –