重要なお知らせ

「教えて! goo」は2025年9月17日(水)をもちまして、サービスを終了いたします。詳細はこちら>

【GOLF me!】初月無料お試し

1つのSQLにしたいです
以下のSQLを1つにしたいです。

select a, b, c, '1' event_type from sample where type = '1'
select a, b, c, '2' event_type from sample where type = '2'
select a, b, c, '3' event_type from sample where type = '3'

ポイントはselect分に結果によって'1'、'2'、'3'といれたいです。
初心者的質問で申し訳ありません。

よろしくお願いします。

A 回答 (4件)

select a, b, c,


(case when type = '1' then '1'
when type = '2' then '2'
when type = '3' then '3'
else '4' end) as event_type
from sample

その他に、
select a, b, c,
decode(type, '1', '1', '2', '2', '3', '3','4') as event_type
from sample
    • good
    • 0
この回答へのお礼

ありがとうございます。

case文は考えになかったので勉強になりましたということで
ベストアンサーに選ばせて頂きました。

早い回答感謝致します。

お礼日時:2010/11/15 14:08

select a, b, c, '1' event_type from sample where type = '1'


union all
select a, b, c, '2' event_type from sample where type = '2'
union all
select a, b, c, '3' event_type from sample where type = '3'
    • good
    • 0
この回答へのお礼

ありがとうございます。
期待する結果となりました。

早い回答感謝致します。

お礼日時:2010/11/15 13:59

意味不明なところもありますが



(1)単純にSQLをUNIONでつなげる
(2)select a, b, c, type as event_type from sample where type IN('1','2','3')

のどちらかでしょうか?
    • good
    • 0
この回答へのお礼

ありがとうございます。
(1)にすると期待する結果となりました。

早い回答感謝致します。

お礼日時:2010/11/15 14:00

select a, b, c, type as event_type from sample where type in ('1'

);
    • good
    • 0
この回答へのお礼

ありがとうございます。

私の質問が悪かったせいで期待通りではありませんでしたが、
早い回答感謝致します。

お礼日時:2010/11/15 14:03

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

関連するカテゴリからQ&Aを探す