ソース掲示板




すべてから検索

キーワード   条件 表示 現行ログ 過去ログ トピックス 名前 本文
複数行のグループデータを1行にまとめるSQL
日時: 2007/07/11 23:06
名前: lightbox



MySQL
拡張子:
CREATE TABLE `data` (
  `コード` int(11) NOT NULL DEFAULT '0',
  `行` int(11) NOT NULL DEFAULT '0',
  `内容` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`コード`,`行`)
)
↓データ
コード内容
11あああ
12いいい
13ううう
21大阪
22北海道
23沖縄
↓加工後
コード内容1内容2内容3
1あああいいいううう
2大阪北海道沖縄
SQL
拡張子:
select 
	コード
	,max(case 行 
	when 1 then 内容
	else null
	end) as 内容1
	,max(case 行 
	when 2 then 内容
	else null
	end) as 内容2
	,max(case 行 
	when 3 then 内容
	else null
	end) as 内容3
 from data
 group by コード
メンテナンス

同一行中の複数列を一番大きい値の列として独立させる ( No.1 )
日時: 2007/08/24 13:03
名前: lightbox


日時: 2007/08/24 13:03
名前: lightbox
拡張子:
select 
	case
		when 年月日1 is NULL and 年月日2 is NULL then 年月日3
		when 年月日2 is NULL and 年月日3 is NULL then 年月日1
		when 年月日1 is NULL and 年月日3 is NULL then 年月日2
		when 年月日1 is NULL and 年月日2 <= 年月日3 then 年月日3 
		when 年月日1 is NULL and 年月日2 > 年月日3 then 年月日2
		when 年月日2 is NULL and 年月日1 <= 年月日3 then 年月日3
		when 年月日2 is NULL and 年月日1 > 年月日3 then 年月日1
		when 年月日3 is NULL and 年月日1 <= 年月日2 then 年月日2
		when 年月日3 is NULL and 年月日1 > 年月日2 then 年月日1
		when 年月日1 <= 年月日2 and 年月日3 <= 年月日2 then 年月日2
		when 年月日2 <= 年月日1 and 年月日3 <= 年月日1 then 年月日1
		when 年月日1 <= 年月日3 and 年月日2 <= 年月日3 then 年月日3
		else 年月日1
	end as 最終年月日
	, 年月日1
	, 年月日2
	, 年月日3
from テーブル
このアーティクルの参照用URLをクリップボードにコピー メンテナンス