Oracle行轉列SQL

來源:互聯網
上載者:User

標籤:des   blog   http   ar   使用   sp   資料   div   on   


-- Create table/*create table TEST_TABLE(  STUDENT VARCHAR2(200),  SUBJECT VARCHAR2(200),  GRADE   NUMBER)tablespace SYSTEM  pctfree 10  pctused 40  initrans 1  maxtrans 255  storage  (    initial 64K    minextents 1    maxextents unlimited  );*//*insert into test_table(student , Subject , grade) values(‘張三‘ , ‘語文‘ , 80)insert into test_table(student , Subject , grade) values(‘張三‘ , ‘數學‘ , 70)insert into test_table(student , Subject , grade) values(‘張三‘ , ‘英語‘ , 60)insert into test_table(student , Subject , grade) values(‘李四‘ , ‘語文‘ , 100)insert into test_table(student , Subject , grade) values(‘李四‘ , ‘數學‘ , 80)insert into test_table(student , Subject , grade) values(‘李四‘ , ‘英語‘ , 90)go*/--oracle中Decode()函數使用 然後將這些累計求和(sum部分)select t.student AS 姓名,sum(decode(t.subject,‘語文‘,grade,null))語文 ,sum(decode(t.subject,‘數學‘,grade,null)) 數學,sum(decode(t.subject,‘英語‘,grade,null)) 英語 from test_table t group by student --靜態SQL,指subject只有語文、數學、英語這三門課程。select student 姓名,  max(case subject when ‘語文‘ then grade else 0 end) 語文,  max(case subject when ‘數學‘ then grade else 0 end) 數學,  max(case subject when ‘英語‘ then grade else 0 end) 英語from test_table group by student/*姓名         語文        數學        英語         ---------- ----------- ----------- -----------李四         100          80          90張三         80           70          60*/--動態SQL,指subject不止語文、數學、英語這三門課程。 sql server  下測試declare @sql varchar(8000)set @sql = ‘select student as ‘ + ‘姓名‘select @sql = @sql + ‘ , max(case Subject when ‘‘‘ + Subject + ‘‘‘ then grade else 0 end) [‘ + Subject + ‘]‘from (select distinct Subject from test_table) as aset @sql = @sql + ‘ from test_table group by student‘exec(@sql)having子句可以過濾組函數結果或是分組的資訊,且寫在group by子句後 模糊查詢 以及排序select username,userno,userpassword from userinfo t where  username like ‘%a%‘  group  by username,userno,userpassword  having(userno)>40 order by username desc數量關係:   多對一關聯性 一對多關聯性   一對一關聯性  多對多關係第一範式,所有的屬性都必須是單值,也就是屬性只表示單一的意義。(記錄可以重複,沒有任何限制)第二範式,屬性要求唯一且非空,(記錄不可重複,但是資料可能會出現冗餘)。第三範式,非主屬性只能依賴於主屬性,不能依賴於其他非主屬性。(解決資料冗餘問題)一個表中只能儲存一個LONG類型 CLOB 儲存大的文字物件   BLOB 儲存大的二進位對象Oralce支援串聯刪除,不支援串聯更新

Oracle行轉列SQL

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.