2016-10-06 3 views
1

このデータベースの他のテーブルの1つから派生テーブルを作成する必要があることを知りました。しかし、私は希望の効果のステートメントを正しく組み合わせる方法がわかりません。基本的には、いくつかの基準で3つの列を移動しています.4番目の列は、派生テーブルの2つの列に解析しています。ケースステートメントを使用して1つの挿入物にSQL文を結合する

私はロジックを解決してこのクエリを機能させようとしていますが、どのように構造化するかについては少し迷っています。

INSERT INTO table (Col1, Col2, Col3, Col4, Col5) 
SELECT 
r.col1, 
r.col2, 
r.col3, 

(case 
when FirstBackslashPos > 0 and FirstUnderscorePos > 0 
then substring(ParseValue,FirstBackslashPos+1,FirstUnderscorePos-FirstBackslashPos-1) 
end) as Col4, 

(case 
when FirstUnderscorePos > 0 and SecondBackslashPos > 0 
then substring(ParseValue,FirstUnderscorePos+1,SecondBackslashPos-FirstUnderscorePos-1) 
end) as Col5 

FROM (
    select 
    r.ThisValue as ParseValue, 
    charindex('\',ThisValue) as FirstBackslashPos, 
    charindex('_',ThisValue , charindex('_',ThisValue)+1) asFirstUnderscorePos, 
    charindex('\',ThisValue,charindex('\',ThisValue)+1) as SecondBackslashPos 
from sometable r Where somecolumn1 = 'SomeValue%') 

GO 

すべてが同じテーブルから来ています。私はこのデータを派生しています。どのように構造化するか分からないので、charindexesはサブケースのステートメントで見つけることができます。

+0

どのようなエラーが表示されますか? – NonProgrammer

+0

インラインビュー(サブクエリ)を避け、charindex呼び出しを直接 'case'式に埋め込むことを意味しますか?単純なカットアンドペーストでそれを行うことはできますが、同じcharindexコールを複数回繰り返すことになります。 charindexの結果を再利用したいのであれば、現在のクエリはそれを行うための合理的な方法です。 – SlimsGhost

答えて

1

あなたがCROSS APPLYを使用することができますデータセットのすべての行に同じ関数を実行し、その結果をステートメントの通常の領域で使用できるようにします。

INSERT INTO table (Col1, Col2, Col3, Col4, Col5) 

SELECT 
    r.col1, 
    r.col2, 
    r.col3, 

    case 
     when ca.FirstBackslashPos > 0 and ca.FirstUnderscorePos > 0 
     then substring(r.ThisValue,ca.FirstBackslashPos+1,ca.FirstUnderscorePos-ca.FirstBackslashPos-1) 
    end as Col4, 

    case 
     when ca.FirstUnderscorePos > 0 and ca.SecondBackslashPos > 0 
     then substring(r.ThisValue,ca.FirstUnderscorePos+1,ca.SecondBackslashPos-ca.FirstUnderscorePos-1) 
    end as Col5 

FROM sometable r 

CROSS APPLY (
    select 
     charindex('\',r.ThisValue) as FirstBackslashPos, 
     charindex('_',r.ThisValue,charindex('_',r.ThisValue)+1) as FirstUnderscorePos, 
     charindex('\',r.ThisValue,charindex('\',r.ThisValue)+1) as SecondBackslashPos 
    ) ca 

WHERE r.somecolumn1 = 'SomeValue%'; 

GO 
0

あなたはFirstUnderscorePosの計算方法が間違っていました。

このサンプルデータを使用して:

USE tempdb 
Go 

CREATE TABLE dbo.SomeTable (ThisValue varchar(1000)); 
INSERT dbo.SomeTable 
VALUES ('fistRecord\1st_monkey\xxx'), ('second\2_xxx\zzz'), ('fruit\orange_orange\apple'); 
あなたのクエリは(私はちょうどCOL1、COL2とCOL3のためのダミーの値を使用しています)次のようになります

SELECT col1 = 'xxx', col2 = 'xxx', col3 = 'xxx', --ParseValue, 
case 
when FirstBackslashPos > 0 and FirstUnderscorePos > 0 
then substring(ParseValue,FirstBackslashPos+1,FirstUnderscorePos FirstBackslashPos-1) 
end 
as Col4 
case 
when FirstUnderscorePos > 0 and SecondBackslashPos > 0 
then substring(ParseValue,FirstUnderscorePos+1,SecondBackslashPos- FirstUnderscorePos-1) 
end as Col5 
FROM 
(
    select 
    r.ThisValue as ParseValue, 
    charindex('\',ThisValue) as FirstBackslashPos, 
    charindex('_',ThisValue) as FirstUnderscorePos, 
    charindex('\',ThisValue,charindex('\',ThisValue)+1) as SecondBackslashPos 
    from dbo.SomeTable r 
) x; 
関連する問題