diff --git "a/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/9.26\344\275\234\344\270\232.txt" "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/9.26\344\275\234\344\270\232.txt" new file mode 100644 index 0000000000000000000000000000000000000000..149514e0c6c8c5ad6c724aa59d188a145ee2e0fd --- /dev/null +++ "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/9.26\344\275\234\344\270\232.txt" @@ -0,0 +1,62 @@ +--编写视图实现查询出所有银行卡账户信息,显示卡号,身份证,姓名,余额。 +create view V_AIfo(卡号,身份证,姓名,余额) +as +select b.CardNo,a.AccountCode,a.RealName,b.CardMoney from AccountInfo a +inner join BankCard b on a.AccountId = b.AccountId +go + +select * from V_AIfo + +create table [mod] ( + [year] int, + [month] int, + amount float +) +insert into [mod] values +(1991,1,1.1), +(1991,2,1.2), +(1991,3,1.3), +(1991,4,1.4), +(1992,1,2.1), +(1992,2,2.2), +(1992,3,2.3), +(1992,4,2.4) +go + +--列转行 +select [year], +max(case when [month] = 1 then amount else 0 end) m1, +max(case when [month] = 2 then amount else 0 end) m2, +max(case when [month] = 3 then amount else 0 end) m3, +max(case when [month] = 4 then amount else 0 end) m4 + from [mod] +group by [year] + + +select * from [mod] +pivot +( +sum(amount) +for [month] in ([1],[2],[3],[4]) +) as amount + + +CREATE TABLE Sales( +[Year] INT, +A1 INT, +A2 INT, +A3 INT, +A4 INT); +GO +INSERT INTO Sales VALUES +(2017,12,123,324,123), +(2018,13,455,324,878), +(2019,29,786,473,633); +--行转列 +select [Year],'A1' as peopel,A1 as Amonut from Sales union all +select [Year],'A2' as peopel,A2 as Amonut from Sales union all +select [Year],'A3' as peopel,A3 as Amonut from Sales union all +select [Year],'A4' as peopel,A4 as Amonut from Sales + +select * from Sales +unpivot (Amount for people in (A1,A2,A3,A4)) as a \ No newline at end of file diff --git "a/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/9.26\347\254\224\350\256\260.txt" "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/9.26\347\254\224\350\256\260.txt" new file mode 100644 index 0000000000000000000000000000000000000000..cc02c590460190316e6de1dff6bef842fcb6176d --- /dev/null +++ "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/9.26\347\254\224\350\256\260.txt" @@ -0,0 +1,49 @@ +1.视图 +1.1视图的概述 +试图(view)是数据库中的一个对象,它是数据库管理系统提供给用户的以多种角度观察数据库中数据的一种重要机制。它对应三种模式中的外模式。 + +在SQL中,试图是基于SQL语句的结果集的可视化的表。 + +视图包含行和列,就像一个真实的表。视图中的字段就是来自一个或多个数据库中的真实的表中的字段。但视图与基本表不同,试图是一个虚表。数据库中只存储视图的定义,而不存储视图所包含的数据,这些数据仍存放在原来的基本表中。这种模式有两个好处: + +视图数据始终与基本表数据保持一致,当基本表发生变化时,从视图中查询出的数据也会随之变化。 节省存储空间。当数据量非常大时,重复存储数据非常耗费空间。 视图可以从一个基本表中提取数据,也可以从多个基本表中提取数据,甚至还可以从其他视图中提取数据,合理利用可以带来很多好处: + +简化数据查询。 使用户能够从多角度看待同一数据。 提高数据的安全性。 提供了一定程度的逻辑独立性。 + +CREATE VIEW <视图名> [(列名 [, ...n])] +AS + SELECT语句 +SELECT语句中通常不包含ORDER BY 和DISTINCT子句 +在定义视图时要么制定视图的全部列名,要么全部省略不写,不能只写视图的部分列名。 +2.行转列和列转行 +2.1行转列 +2.1.1 case when 以及 sum/max +select [year], +max(case when [month] = 1 then amount else 0 end) m1, +max(case when [month] = 2 then amount else 0 end) m2, +max(case when [month] = 3 then amount else 0 end) m3, +max(case when [month] = 4 then amount else 0 end) m4 + from [mod] +group by [year] +2.1.2 pivot +select * from [mod] --1 +pivot +( +sum(amount) --3 +for [month] in ([1],[2],[3],[4]) --2 +) as amount + + +--1数据来源 +--2那些值做新列的名字 +--3对新列的值的要求 +2.2列转行 +2.2.1union +select [Year],'A1' as peopel,A1 as Amonut from Sales union all +select [Year],'A2' as peopel,A2 as Amonut from Sales union all +select [Year],'A3' as peopel,A3 as Amonut from Sales union all +select [Year],'A4' as peopel,A4 as Amonut from Sales +2.2.2 unpivot + +select * from Sales +unpivot (Amount for people in (A1,A2,A3,A4)) as a \ No newline at end of file