2017-02-21 6 views
1

안녕하세요 저는 Oracle 데이터베이스에서이 부분을 보았고 Microsoft SQL Server에서이를 변경해야합니다.Oracle에서 SQL Server로 먼저 연결하여 선택 변경

with V_LOCHIERARHY_N 
(nr, nivel, location, parent, systemid, siteid, orgid, count_a, count_wo, children) 
AS 
SELECT  LEVEL, LPAD (' ', 2 * (LEVEL - 1)) || l.LOCATION nivel, 
       LOCATION, PARENT, systemid, siteid, orgid, 
      (SELECT COUNT (a.ancestor) 
      FROM locancestor a 
      WHERE a.LOCATION = l.LOCATION AND a.siteid = l.siteid), 
        NVL (COUNT (w.wonum), 0) 
      FROM maximo.workorder w 

      WHERE ( w.reportdate > 
          TO_TIMESTAMP ('2006-06-19 00:00:01', 
             'YYYY-MM-DD HH24:MI:SS.FF' 
             ) 
        AND w.istask = 0 
        AND w.worktype <> 'P' 
        AND w.LOCATION = l.LOCATION 
       ) 
       AND w.status <> 'CAN'), 
      l.children 
    FROM lochierarchy l 
    START WITH l.LOCATION = 'StartPoint' 
    CONNECT BY PRIOR l.LOCATION = l.PARENT AND l.siteid = 'SiteTest' 

이 스크립트에서 필요한 항목은 주어진 항목의 모든 하위 항목 (위치 표에서 찾을 수있는 하위 항목의 설명)을 반환하는 것입니다.

Location Parent  Systemid Children Siteid Origid Lochierarchyid 
A001  StartPoint Primary 2  SiteTest X  106372 
A002  A001  Primary 2  SiteTest X  105472 
A003  A002  Primary 0  SiteTest X  98654 
A004  A002  Primary 1  SiteTest X  875543 
A004B A004  Primary 0  SiteTest X  443216 
B005  StartPoint Primary 0  SiteTest X  544321 

주어진 항목 A001에 대한 예를 들어 나는 아래이보기를 만든하지만 난 방법을 모른다


A002  
A003  
A004 
    A004B  
B005 

반환합니다

나는 다음 열이있는 테이블이 그것을 첫 번째 것과 통합합니다. 또한 그것은 누군가가 나를 도와주세요 수 corectly 위해

Parent 
Children 1 of parent 
    Children a of children 1 
    children b of children 1 
children 2 of parent 
    children a1 of children 2 and so on. 

WITH testCTE AS 
(
    SELECT l.parent, l.location as child, l.location, l.lochierarchyid 
    FROM lochierarchy l 
    where location='SecondLocation' --and siteid='SiteTest' 
     UNION ALL 
    SELECT c.Parent, l.parent, l.location, l.lochierarchyid 
    FROM lochierarchy l 
    INNER JOIN testCTE c ON l.parent = c.location 
) 
    SELECT * 
    FROM testCTE c 
    order BY c.parent,child asc 
; 

나에게 목록을 반환하지 않습니다? :)

+0

시간이 있다면 해결책을 찾아 보겠습니다. 당신은 또한 도움을 받아 스스로 알아낼 수도 있습니다. 아래 기사에서는 재귀 하위 쿼리를 사용하여 "연결 기준"쿼리의 모든 기능을 재현하는 방법을 단계별로 보여줍니다. https://oracle-base.com/articles/11g/recursive-subquery-factoring-11gr2 – mathguy

+0

귀하의 게시물을 파기하지 * 마십시오. –

+0

[SQL Server의 SELECT에서 UPDATE하는 방법] 가능한 복제본 (영문) (http://stackoverflow.com/questions/2334712/how-to-update-from-a-select-in-sql-server) – Madalina

답변

0

다음은 재귀 쿼리를 사용하여 수행 할 수있는 방법입니다 (Oracle에서 아는 유일한 맛). "The Web"은 SQL Server가 재귀 쿼리와 동일한 구문을 구현한다고보고합니다. (이 모든 것이 SQL 표준과 호환되므로 그렇게 놀랄 일은 아닙니다.) 시도 해봐.

테이블을 만드는 대신 모든 테스트 데이터를 첫 번째 CTE에 넣습니다. 이 솔루션을 시도 할 때 inputs이라는 CTE를 먼저 삭제하고 나머지 쿼리에서 실제 테이블 이름을 사용하십시오.

with 
    inputs (location, parent) as (
     select 'A001' , 'Downstream' from dual union all 
     select 'A002' , 'A001'  from dual union all 
     select 'A003' , 'A002'  from dual union all 
     select 'A004' , 'A002'  from dual union all 
     select 'A004B', 'A004'  from dual union all 
     select 'B005' , 'Downstream' from dual 
    ), 
    r (lvl, location) as (
     select 1, location 
     from inputs 
     where parent = 'Downstream' 
     union all 
     select r.lvl + 1, i.location 
     from r join inputs i on r.location = i.parent 
    ) 
    search depth first by lvl set ord 
select lpad(' ', 2 * (lvl-1), ' ') || location as location 
from r 
order by ord 
; 


LOCATION 
-------------------- 
A001 
    A002 
    A003 
    A004 
     A004B 
B005 

6 rows selected. 

추가 : SQL 서버는 재귀 CTE의 (혹은 구문이 다른)에 대한 search depth/breadth first 조항이없는 것 같다. 어떤 경우에는 여기에서 원시적 "수동"동일의 구현 : MSSQL에 대한 수정 mathguy에 의해 제안 된 쿼리에 따라

with ( ......... ), 
    r (lvl, location, ord) as (
     select 1, location, location 
     from inputs 
     where parent = 'Downstream' 
     union all 
     select r.lvl + 1, i.location, r.location || '/' || i.location 
     from r join inputs i on r.location = i.parent 
    ) 
select lpad(' ', 2 * (lvl-1), ' ') || location as location 
from r 
order by ord 
; 
+0

@AndreeaEnache 아마도 SQL Server에는 재귀 쿼리에 대한 SEARCH 절이 없거나 구문이 다를 수 있습니다. 나는 SQL Server가 없지만, 그 줄과 가장 끝에 ORDER BY 절을 주석 처리 한 후에 다시 시도하십시오. 나머지는 작동합니까? 그렇다면 SQL Server에 대해 SEARCH DEPTH FIRST와 동일한 기능을 조사 할 수 있습니다. 내 쿼리에서 'ord'에 대한 올바른 공식이 무엇인지 알기 위해 약간 생각할 것입니다. 오라클이 올바른 순서를 얻으려는 동일한 공식입니다. – mathguy

+0

@ AndreeaEnache - 알았어, 나는 나의 대답에 다음과 같이 덧붙였다 : 나는 처음부터 "처음부터 깊이있게"검색 깊이를 구현했다. – mathguy

+0

:-) 이것이 오라클과 SQL Server를 모두 아는 사람의 도움이 필요한 이유입니다. 예,'||'은 오라클의 연결이며 SQL Server가 사용하는 것이 확실하지 않습니다. 당신이 얻는 오류에 대해서 : 나는 그것이'ord' 일이라고 가정하고, 당신의 LOCATION이 실제로는 문자열이 아니라고 추측한다. 권리? NUMBER 인 경우 변환하지 않아도 문자열로 처리되므로 문제가 발생할 수 있습니다. 숫자가 NUMBER이면 ord는 앵커의 NUMBER이지만 재귀 분기에서 연결 후 문자열이됩니다. 실제로 위치가 일부 SQL Server 종류의 숫자 인 경우 TO_CHAR() 또는 이와 동등한 위치에 넣으십시오. – mathguy

0

(2012)

with 
     inputs (location, parent) as (
      select 'A001' , 'StartPoint' union all 
      select 'A002' , 'A001'  union all 
      select 'A003' , 'A002'  union all 
      select 'A004' , 'A002'  union all 
     select 'A004B', 'A004'  union all 
     select 'B005' , 'StartPoint' 
    ), 
    r (lvl, location, ord) as (
     select 1, location, CAST(location AS VARCHAR(400)) 
     from inputs 
     where parent = 'StartPoint' 
     union all 
     select r.lvl + 1, i.location, CAST(r.location + '/' + i.location AS VARCHAR(400)) 
     from r join inputs i on r.location = i.parent 
    ) 
select REPLICATE(' ', 2 * (lvl-1)) + location as location 
from r 
order by ord 
; 

OUPUT :

location 
------------------------------------------------------------------- 
A001 
    A002 
    A003 
    A004 
     A004B 
B005