SQL資料表縱橫轉換

2022-07-20 11:12:09 字數 4297 閱讀 3889

select

distinct

'(select b.risk from rhwl_easy_genes_new_risk b where b.genes_id=a.id and b.disease=

'''|| b."disease" ||

''') as "

'|| b."disease" ||'",

'from

rhwl_easy_genes_new a,

rhwl_easy_genes_new_risk b

where

b.genes_id

=a.id

and a.name in

(select

a .name

from

rhwl_easy_genes_new a

where

a.active

=true

and a.name not

like

'cs%

'and a.package_id in

(select

id from

rhwl_genes_base_package

where

code

in ( '

fv', '

je', '

jc' )));

通過去重,會生成列的select語句:

(select b.risk from rhwl_easy_genes_new_risk b where  b.active=true and b.genes_id=a.id and b.disease=

'不寧腿症候群

') as

"不寧腿症候群",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease=

'萘普生

') as

"萘普生",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease='耐力

') as

"耐力",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease=

'抗雀斑能力

') as

"抗雀斑能力",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease=

'利培酮

') as

"利培酮",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease=

'帕金森病

') as

"帕金森病",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease='順鉑

') as

"順鉑",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease=

'霍奇金淋巴瘤

') as

"霍奇金淋巴瘤",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease='哮喘

') as

"哮喘",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease=

'酒精性肝硬化

') as "酒精性肝硬化"

把上面的語句插入, 生成最終查詢:

select

p.name

as"**",

a.name

as"樣本編號",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease=

'不寧腿症候群

') as

"不寧腿症候群",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease=

'萘普生

') as

"萘普生",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease='耐力

') as

"耐力",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease=

'抗雀斑能力

') as

"抗雀斑能力",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease=

'利培酮

') as

"利培酮",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease=

'帕金森病

') as

"帕金森病",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease='順鉑

') as

"順鉑",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease=

'霍奇金淋巴瘤

') as

"霍奇金淋巴瘤",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease='哮喘

') as

"哮喘",

(select b.risk from rhwl_easy_genes_new_risk b where b.active=true and b.genes_id=a.id and b.disease=

'酒精性肝硬化

') as

"酒精性肝硬化"

from

rhwl_easy_genes_new a,

rhwl_genes_base_package p

where

a.state

in ('

report_done

', '

done

', '

result_done

', '

deliver')

and a.active =

true

and a.name not

like

'cs%

'and a.package_id in (select id from rhwl_genes_base_package where code in ('

fv', '

je', 'jc'

))and p.id =

a.package_id

order

by p.name, a.name

MySQL資料庫縱橫表轉換

1.縱表 score 目標 切換為橫表 sql語句為 select name as 姓名 max case subject when 語文 then value else 0 end as 語文 max case subject when 數學 then value else 0 end as 數學...

縱橫表轉換學習

動態橫表轉縱表 建表語句 if not object id class1 is null drop table class1 gocreate table class1 student nvarchar 2 數學 int,物理 int,英語 int,語文 int insert class1 sele...

sql 2005 資料表行列轉換顯示

1.建立 學生成績表 2.填充 部分資料 3.一般的直接查詢 4.sql 2000 下 行列轉換 5.sql 2005 下 pivot 建立 學生成績表 create table stusources stuid char 10 collate chinese prc ci as not null ...