本來只是想解決怎么把數據的行和列進行轉換的,但最近覺得一些數據庫SQL語句的操作,很久沒用了,有點陌生。所以也就隨筆記錄一些簡單但很基本的操作。
我的數據庫是MSSQL2005.
第一部分主要的操作包含:數據庫的創建、刪除,表的增、刪、改,表中數據的增、刪、改、查,視圖的操作。
1 --查詢數據庫是否存在
2 if exists ( select * from sysdatabases where [name]='TestDB')
3 print 'Yes, the DB exists'
4 else
5 print 'No, need a new one?'
6
7 --新建一個數據庫
8 create database TestDB on
9 (
10 name = 'TestData',
11 filename = 'G:\DBS\KeyTest.mdf',
12 size = 3,
13 filegrowth = 2
14 )
15 log on
16 (
17 name = 'TestLog',
18 filename = 'G:\DBS\KeyTest.ldf',
19 size = 3,
20 filegrowth = 10
21 )
22
23 --drop database TestDB
24
25 use TestDB
26 go
27
28 --新建一個表
29 create table [Scores]
30 (
31 [ID] int identity(1,1) primary key,
32 [Student] varchar(20) ,
33 [Subject] varchar(30),
34 [Score] float
35 )
36
37 --drop table [Scores]
38
39 --修改表中的一列
40 alter table Scores alter column [Student] varchar(20) not null
41
42 --新增一列
43 alter table Scores add Birthday datetime
44
45 --刪除一列
46 alter table Scores drop column Birthday
47
48 --往表中插入單條數據,方法1:帶列名
49 insert into Scores(Student,Subject,Score)
50 values('張三','語文','90')
51
52 --往表中插入單條數據,方法2:不帶列名,但要求值的類型要和列字段類型對應
53 insert into Scores
54 values('張三','英語','95')
55
56 --插入多條數據:用union或者union all
57 insert into Scores(Student,Subject,Score)
58 select '李四','語文','89'
59 union all
60 select '李四','英語','78'
61
62 --刪除表中數據,沒有條件時,刪除所有
63 delete from Scores where ID in(7,8)
64
65 --修改表中數據
66 update Scores
67 set Student='王五',Score='94'
68 where ID=10
69
70 --查看數據
71 select * from Scores
72
73 --查看表中最大的identity值
74 select @@identity
75
76 --或者利用dbcc命令查看表中最大的identity值
77 dbcc checkident('Scores',noreseed)
78
79 --創建視圖,全部省略視圖的屬性列名,由子查詢目標列的字段組成
80 create view StudentView
81 as
82 select Student,Subject,Score
83 from Scores
84
85 --加上with check option,以后對視圖的操作(增,改,刪,查)都會自動加上where ID>3
86 /*
87 create view StudentView
88 as
89 select Student,Subject,Score
90 from Scores
91 where ID>3
92 with check option
93 */
94
95 --創建視圖,全部定義屬性列名,需要定義列名的情況:
96 ----某個目標列(子查詢)不是單純的屬性列,而是聚集函數或列表達式
97 ----多表連接時選出了幾個同名列
98 ----需要在視圖中為某個列啟用新的更合適的名字
99 create view IS_Student(Student,Subject,MaxScore)
100 as
101 select Student,Subject,Score
102 from Scores
103 where Score=(select max(Score) from Scores)
104
105
106 --查詢視圖,和基本表完全樣,只不過如果視圖中有with check option,會自動加上那個條件
107 select *
108 from StudentView
109
110 --查詢自定義列名的視圖
111 select *
112 from IS_Student
113
114 --對視圖的insert/delete/update,和對基本表的操作一樣,並且最終都是用RDBMS自動轉換為對基本表的更新
115 --並不是所有的視圖都是可更新的,因為有些視圖的更新不能有意義的轉換成對相應基本表的更新
116
117 --刪除視圖
118 drop view StudentView
第二部分,這次練習的主題。
【一】行轉列
1,查詢原始的數據
/***這次練習的主題,行轉列,列轉行***/
select * from Scores

2,得到姓名,通過group by
select Student as '姓名'
from Scores
group by Student
order by Student

3,再加上max, case……when
select Student as '姓名',
max(case Subject when '語文' then Score else 0 end) as '語文' ,--如果這個行是“語文”,就選此行作為列
max(case Subject when '英語' then Score else 0 end ) as '英語'
from Scores
group by Student
order by Student

查看其它資料時,看到另外一種方法,用pivot
--group by, avg/max, pivot。這里用max和avg,結果都一樣,有什么區別嗎?有點不明白
--參考網上的資料,用法如下
/*
pivot(
聚合函數(要轉成列值的列名)
for 要轉換的列
in(目標列名)
)
*/
select Student as '姓名',
avg(語文) as '語文',
avg(英語) as '英語'
from Scores
pivot(
avg(Score) for Subject
in (語文,英語)
)as NewScores
group by Student
order by Student asc
