postgreSQL7.3で以下のようなデータの並び替えを実現したいと思っています。
下記のようなデータを・・・
no | recno | name
----+-------+------
1 | 1 | a
1 | 1 | b
1 | 2 | c
1 | 4 | a
2 | 3 | a
2 | 3 | c
2 | 3 | b
3 | 5 | b
4 | 6 | a
下記のような並びにSQLで取得することは可能でしょうか。
no | recno | name1 | name2 | name3
----+-------+------+------+------
1 | 1 | a | b |
1 | 2 | c | |
1 | 4 | a | |
2 | 3 | a | b | c
3 | 5 | b | |
4 | 6 | a | |
noとrecnoのグループごとにnameを横に並べていきたいのですが、
可能なのでしょうか。
できれば、nameは個々のカラムに出力したいのですが、
配列のように1つのカラム内にカンマ区切りでの出力でも構いません。
上記のような表示が可能であれば、SQL文もご教授いただけますと幸いです。
宜しくお願いいたします。
A 回答 (4件)
- 最新から表示
- 回答順に表示
No.4
- 回答日時:
ちょっとインチキですけど、こんなのはどうでしょうか。
select no, recno, (case when s=1 then 'a'
when s=2 then 'b'
when s=3 then 'a,b'
when s=4 then 'c'
when s=5 then 'a,c'
when s=6 then 'b,c'
when s=7 then 'a,b,c'
else '' end ) as names
from
(select no, recno, sum(case when name = 'a' then 1
when name = 'b' then 2
when name = 'c' then 4
else 0 end) as s
from ex5 group by no,recno order by no,recno) as b;
いちおう動きます(笑)
no | recno | names
----+-------+-------
1 | 1 | a,b
1 | 2 | c
1 | 4 | a
2 | 3 | a,b,c
3 | 5 | b
4 | 6 | a
(6 rows)
No.2
- 回答日時:
postgresqlは専門外なのですが、こんな感じで処理できないですかね。
select
no,
recno,
max(case when r=1 thwn name else null end) as name1,
max(case when r=2 thwn name else null end) as name2,
max(case when r=3 thwn name else null end) as name3
from
(
select
no,
recno,
name,
(select count(*) from x x2 where x2.no=x1.no and x2.recno=x1.renco and x2.name<=x1.name) as r
from x x1
)
group by no,recno
No.1
- 回答日時:
この回答への補足
ご回答ありがとうございます。
ARRAY関数はPostgreSQL7.4からの機能でして、
当方が使用しているPostgreSQL7.3の環境で使用することができませんでした。
他に方法をご存知でしたら、ご教授いただけますと幸いです。
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
- ・ゆるやかでぃべーと タイムマシンを破壊すべきか。
- ・歩いた自慢大会
- ・許せない心理テスト
- ・字面がカッコいい英単語
- ・これ何て呼びますか Part2
- ・人生で一番思い出に残ってる靴
- ・ゆるやかでぃべーと すべての高校生はアルバイトをするべきだ。
- ・初めて自分の家と他人の家が違う、と意識した時
- ・単二電池
- ・チョコミントアイス
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
テーブルからのselectにおいて...
-
postgreSQLのint型は桁数指定が...
-
クエリアナライザのsp_helpコマ...
-
全テーブルのデータの行数
-
Viewにインデックスは張れ...
-
異なるスキーマからデータを抽...
-
datapumpの実行方法について
-
DELETE文でFROM句を省略した場合
-
ORA-00959: 表領域'****'は...
-
SQL*Loaderで、データを加工し...
-
データを削除しても表領域の使...
-
ACCESS 複数テーブル・複数フィ...
-
viewのバックアップ
-
Access レコードを追加できませ...
-
object browser で処理を中断す...
-
エクセルからアクセスにインポ...
-
他の処理でselectさせないよう...
-
カラムの存在チェック
-
CLOB型へのINSERT
-
ExcelVBAにて外部データ(*.csv...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
postgreSQLのint型は桁数指定が...
-
テーブルからのselectにおいて...
-
クエリアナライザのsp_helpコマ...
-
UNIQUEをつけたときのINDEXテー...
-
PostgresSQL8.4でツリー上に取...
-
PostgressからMySQL(MariaDB)...
-
近い時間によるテーブル結合
-
特定カラムの値を変更したいの...
-
自動で生成されるユニークID
-
チェックボックスから、データ...
-
pl/pgsqlで変数の扱い
-
Perl DBI でSELECT ilikeの結果...
-
SQL SELECT文 別テーブルのレコ...
-
SELECT結果から重複行を除く方法
-
行の結合?
-
SQLで同じフィールドを2つ取り...
-
全テーブルのデータの行数
-
もうちょっと賢いSELECT文が書...
-
次の条件を満たすSQL文をご...
-
SQL文作成のお願い
おすすめ情報