sqlserver豎表轉橫表備忘

2022-07-02 05:54:13 字數 1482 閱讀 5535

select *  from edustatdata where 指標類別='

小學教育資源

' and (指標='

學校數' or 指標='

學齡人口數

' ) and 時間='

2023年

' and 地區** in(select [sys_fld_class_code] from [lnedu].[dbo].[cyfd_cls2_new] where sys_fld_class_code like '

轉表:

select 地區**,地區,sum(case 指標 when '

學校數' then 數值 else

0 end) as 學校數,sum(case 指標 when '

學齡人口數

' then 數值 else

0 end) as 學齡人口數 from edustatdata where 指標類別='

小學教育資源

' and 時間='

2023年

' and 地區** in(select [sys_fld_class_code] from [lnedu].[dbo].[cyfd_cls2_new] where sys_fld_class_code like '

xj06%

' and (sys_fld_class_grade=1 or sys_fld_class_grade=2)) group by 地區**,地區 order by 地區**

轉表:

select * from [qmgbdata]   as c pivot(max(數值) for [時間] in([2023年],[2023年],[2023年],[2023年],[2023年])) as t where (學校標識碼='

2121000001

' or 學校名稱 = '

2121000001

') and 指標 in ('

教學班數

','在校學生數

','專任教師

','生師比

','生均圖書

','在校學生鞏固率

','生均固定資產總值

') and 學校標識碼 in (select [學校標識碼] from [lnedu].[dbo].[schoolinfo_2010] where 學校類別='

小學' and 地區** like '

xj06%

')

SQL豎表轉橫表 橫表轉豎表

豎表轉橫表 豎表結構 name course grade 張三語文 75張三 數學80 張三英語 90李四 語文95 李四數學 55轉換後橫表結構 name 語文數學 英語張三 7580 90李四 9555 0sql語句 1 select name,2sum case course when 語文 ...

豎表轉橫表

今天遇到乙個要求將豎表轉換成橫表。以前看過豎表轉橫表但沒寫過,現記錄下來以供學習。任務大體要求如下 教師號 星期號 是否有課 有 有 有 有 有 寫一條sql語句讓你變為這樣的表 教師號 星期一 星期二 星期三 建表 create table teac info teac no number,day...

oracle SQL豎表轉橫表

oracle sql豎表轉橫表 t t student表查詢記錄如下,要轉成橫表 姓名 課程 成績 1 張飛 語文 80 2 張飛 數學 87 3 關羽 語文 97 4 張飛 英語 68 5 關羽 數學 53 6 劉備 語文 90 方法一 用decode實現,select t.name,sum de...