select ●全行数 from
-- ここからインラインビュー
(select count(*) as ●全行数,1 as SORT from 郵便番号マスタ
union all
select '●重複しない件数',2 as SORT from コントロールマスタ
union all
select count(*),3 as SORT from
(select distinct 郵便番号 from 郵便番号マスタ)
)
-- ここまでインラインビュー
order by SORT
SQLServer
select [●全行数] from
-- ここからインラインビュー
(select convert(varchar,count(*)) as [●全行数],1 as ソート
from 郵便番号マスタ
union all
select '●重複しない件数',2 as ソート
union all
select convert(varchar,count(distinct 郵便番号)),3 as ソート
from 郵便番号マスタ) as INVIEW
-- ここまでインラインビュー
order by ソート
MySQL 5.1
まず、単純に 2種類のデータを二つ取得する SQL を union all で並べます。
※ union all は全ての行を取得します。 all を省くと 重複行は排除されます
※ 同じテーブルを union すると明らかです
select count(*),1 as `ソート` from 郵便番号マスタ
union all
select count(distinct 郵便番号),3 as `ソート` from 郵便番号マスタ
さらにタイトルを加えて、ソート用の列番号でソートします
select '●全行数',1
union all
select count(*),2 from 郵便番号マスタ
union all
select '●重複しない件数',3
union all
select count(distinct 郵便番号),4 from 郵便番号マスタ
order by 2
select `●全行数` from
-- ここからインラインビュー
(select count(*) as '●全行数',2 as `ソート` from 郵便番号マスタ
union all
select '●重複しない件数',3
union all
select count(distinct 郵便番号),4 from 郵便番号マスタ) as INVIEW
-- ここまでインラインビュー
order by `ソート`
select "●全行数" from
-- ここからインラインビュー
(select
TO_CHAR(count(*)) as "●全行数",1 as ソート
from 郵便番号マスタ
union all
select '●重複しない件数',2 from dual
union all
select
TO_CHAR(count(distinct 郵便番号)) as "●重複しない件数",3
from 郵便番号マスタ)
-- ここまでインラインビュー
order by ソート
PostgreSQL
select "●全行数" from
-- ここからインラインビュー
(select
TO_CHAR(count(*),'FM9999999999') as "●全行数",1 as ソート
from 郵便番号マスタ
union all
select '●重複しない件数',2
union all
select
TO_CHAR(count(distinct 郵便番号),'FM9999999999') as "●重複しない件数",3
from 郵便番号マスタ) as INVIEW
-- ここまでインラインビュー
order by ソート