2016-05-10 10 views
2

第三のテーブルから任意の語句が含まれているかどうかを示すために、2つのテーブルとブールフラグに参加する私は1または0を選択するために、ケースを使用しようとしていますSQLクエリは、それは私が次のスキーマに3つのテーブルを持っている

create table main (
    main_id int PRIMARY KEY, 
    secondary_id int NOT NULL 
); 

create table secondary (
    secondary_id int NOT NULL, 
    tags varchar(100) 
); 

create table bad_words (
    words varchar(100) NOT NULL 
); 

insert into main values (1, 1001); 
insert into main values (2, 1002); 
insert into main values (3, 1003); 
insert into main values (4, 1004); 

insert into secondary values (1001, 'good word'); 
insert into secondary values (1002, 'bad word'); 
insert into secondary values (1002, 'good word'); 
insert into secondary values (1002, 'other word'); 
insert into secondary values (1003, 'ugly'); 
insert into secondary values (1003, 'bad word'); 
insert into secondary values (1004, 'pleasant'); 
insert into secondary values (1004, 'nice'); 

insert into bad_words values ('bad word'); 
insert into bad_words values ('ugly'); 
insert into bad_words values ('worst'); 

expected output 
---------------- 
1, 1000, good word, 0 (boolean flag indicating whether the tags contain any one of the words from the bad_words table) 
2, 1001, bad word,good word,other word , 1 
3, 1002, ugly,bad word, 1 
4, 1003, pleasant,nice, 0 

最後の列を使用して、メインとセカンダリのテーブルに参加するために結合を使用しますが、混乱してしまいます。誰かが私に質問をしてくれますか?これらのテーブルはredshiftに格納されており、私はredshiftとのクエリ互換性が必要です。

あなたはsqlfiddle

EDITでクエリをしようとする上記のスキーマを使用することができます簡単にはbad_wordsテーブルと結合しているので、私は、二次の表にPRIMARY KEYを除去することにより、今スキーマと予想される出力を更新しました。あなたが使用することができます

+0

タグのデータ型は? –

+0

質問をスキーマで更新しました – srini

答えて

0
SELECT m.main_id, m.secondary_id, t.tags, t.is_bad_word 
FROM srini.main m 
JOIN (
    SELECT st.secondary_id, st.tags, exists (select 1 from srini.bad_words b where st.tags like '%'+b.words+'%') is_bad_word 
    FROM 
    (SELECT secondary_id, LISTAGG(tags, ',') as tags 
     FROM srini.secondary 
     GROUP BY secondary_id) st 
) t on t.secondary_id = m.secondary_id; 

これは私のために働きました上記のスキーマで次の出力を生成しました。

1 1001 good word false 
3 1003 ugly,bad word true 
2 1002 good word,other word,bad word true 
4 1004 pleasant,nice false 
0

select main_id, a.secondary_id, tags, case when c.words is not null then 1 else 0 end from main a join secondary b on b.secondary_id = a.secondary_id left outer join bad_words c on c.words like b.tags

+0

タグ内の単語の部分文字列一致を探しています。あなたのクエリは '2、1001、良いと悪い単語、0を返しました ' – srini

2

が存在し、\ mと\ Mと正規表現の比較(それぞれ単語の先頭と末尾のマーカー):

with 
    main(main_id, secondary_id) as (values (1, 1000), (2, 1001), (3, 1002), (4, 1003)), 
    secondary(secondary_id, tags) as (values (1000, 'very good words'), (1001, 'good and bad words'), (1002, 'ugly'),(1003, 'pleasant')), 
    bad_words(words) as (values ('bad'), ('ugly'), ('worst')) 

select *, exists (select 1 from bad_words where s.tags ~* ('\m'||words||'\M'))::int as flag 
from main m 
join secondary s using (secondary_id) 
+0

私はあなたのクエリ(およびいくつかの小さなバリエーション)をsqlfiddleで試してみました。どこであなたのクエリを試すことができますか? – srini

+0

@ sriniこれもsqlfiddleで動作します。http://sqlfiddle.com/#!15/d36fe/2/0 –

+0

これはsqlfiddleで動作します。しかし、残念ながら、 'ERROR:このタイプの相関サブクエリパターンは内部エラーのためにサポートされていません.'で赤いシフトで実行すると失敗します。詳細はこちら[こちら](http://docs.aws.amazon.com/redshift/latest/dg/r_correlated_subqueries.html)をご覧ください。また[redshift docs](http://docs.aws.amazon.com/redshift/latest/dg/pattern-matching-conditions.html)は、LIKEが正規表現より優れていると言います。 – srini

関連する問題