2017-02-21 7 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

再帰的なクエリを使用してこれを(私が知っている唯一の味である)オラクルで行う方法は次のとおりです。 「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 Serverは再帰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 - OK、私は私の答えに追加しました。私は初めから基本的な "検索深度"を実装しました。 – mathguy

+0

:-)これは、OracleとSQL Serverの両方を知っている人の助けが必要な理由です。はい、 '||'はOracleの連結であり、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 
; 

出力リレー:

location 
------------------------------------------------------------------- 
A001 
    A002 
    A003 
    A004 
     A004B 
B005 
関連する問題