diff --git "a/01\346\235\216\351\237\246\345\263\260/\344\275\234\344\270\232/2022-09-22\345\217\230\351\207\217.sql" "b/01\346\235\216\351\237\246\345\263\260/\344\275\234\344\270\232/2022-09-22\345\217\230\351\207\217.sql" index 1f21a0b647d8d0d2e8d8bb5cfc961fd8de7bc12c..d7a9ec910551629bfc5c853ce2c28abf9a0f9740 100644 --- "a/01\346\235\216\351\237\246\345\263\260/\344\275\234\344\270\232/2022-09-22\345\217\230\351\207\217.sql" +++ "b/01\346\235\216\351\237\246\345\263\260/\344\275\234\344\270\232/2022-09-22\345\217\230\351\207\217.sql" @@ -72,13 +72,16 @@ declare @a int = 0 --​ 6.打印99乘法表 -declare @i int = 1 ,@j int = 1 +declare @i int = 1 while(@i<=9) begin + declare @j int =1 + declare @b varchar(250) = '' while(@j<=@i) - begin - print cast(@i as varchar(1))+'*'+cast(@j as varchar(1))+'='+cast(@i*@j as varchar(40)) - set @j += 1 - end + begin + set @b = @b+cast(@j as varchar(1))+'*'+cast(@i as varchar(1))+'='+cast(@i*@j as varchar(40))+CHAR(9) + set @j += 1 + end + print @b set @i += 1 end \ No newline at end of file diff --git "a/01\346\235\216\351\237\246\345\263\260/\344\275\234\344\270\232/2022-09-26\350\247\206\345\233\276.sql" "b/01\346\235\216\351\237\246\345\263\260/\344\275\234\344\270\232/2022-09-26\350\247\206\345\233\276.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fdc390210c0e381e8f84bf28009e434f3ec38035 --- /dev/null +++ "b/01\346\235\216\351\237\246\345\263\260/\344\275\234\344\270\232/2022-09-26\350\247\206\345\233\276.sql" @@ -0,0 +1,60 @@ + +use BankTest +go +--1дͼʵֲѯп˻Ϣʾţ֤ + +select AccountCode,AccountPhone,RealName,CardMoney from AccountInfo +join BankCard on AccountInfo.AccountId = BankCard.AccountId + +go +create view V_UserInfo(ʾ,֤,,) +as +select AccountCode,AccountPhone,RealName,CardMoney from AccountInfo +join BankCard on AccountInfo.AccountId = BankCard.AccountId +go + +select * from V_UserInfo + + + + + +--2תг: group by + sum(case when) /+count(case when) ݷ+ over (paritition by, order by) +create table Securities( + id int primary key identity(1025001,1), + [year] int, + [month] int, + amonth decimal(10,1) +) +insert into Securities +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) + +select [year], +max(case when [month]=1 then amonth else 0 end) m1, +max(case when [month]=2 then amonth else 0 end) m2, +max(case when [month]=3 then amonth else 0 end) m3, +max(case when [month]=4 then amonth else 0 end )m4 +from Securities +group by [year] + +--***************************ʹpivot +select * from Securities +pivot( + sum([amonth]) + for [month] in ([1],[2],[3],[4] ) +)as TT + + + +select * from Securities + +-- ʵתУpivot +-- תУunion diff --git "a/01\346\235\216\351\237\246\345\263\260/\347\254\224\350\256\260/2022-09-26\350\247\206\345\233\27607.md" "b/01\346\235\216\351\237\246\345\263\260/\347\254\224\350\256\260/2022-09-26\350\247\206\345\233\27607.md" new file mode 100644 index 0000000000000000000000000000000000000000..b3faf3fe2358b8c150d3fffc972e34d5ddceabd9 --- /dev/null +++ "b/01\346\235\216\351\237\246\345\263\260/\347\254\224\350\256\260/2022-09-26\350\247\206\345\233\27607.md" @@ -0,0 +1,111 @@ +#### 1.1关系(Relations) + +三种类型关系: + +- 表(Table)--基表,存储关系(Base Tables,Sorted relations) +- 视图(Views)--虚拟关系(Virtual relations)(不是物理存在的是虚拟的) +- 临时结果(Temporary results)--用于构建**子查询**的结果 + + + +#### 1.2什么是视图 + +- 视图是一张虚拟表,它表示一张表的部分数据或多张表的综合数据,其结构和数据是建立在对表的查询基础上 +- 视图中并不存放数据,而是存放在视图所引用的原始表(基表)中,即基本中的数据发生变化,从视图中查询的数据也随之改变。 +- 同一张原始表,根据不同用户的不同需求,可以创建不同的视图 + +**视图的作用** + +- 视图能够简化用户的操作 +- 视图使用户能以多种角度看待同一数据 +- 视图对重构数据库提供了一定的逻辑独立性 +- 视图能够对机密数据提供安全保护 + + + +#### 1.3三级模式结构 + +![](J:/SQL/Sql_server/上课内容/20220925-视图/04-索引与视图/04索引与视图.assets/1-2022-9-1423_01_58.png) + + + +## 1.4基本操作 + +- **创建视图** + +- ```sql + --格式 + CREATE VIEW <视图名> [(列名 [, ...n])] + AS + SELECT语句 + ``` + +练习: + +```sql +--1)编写视图实现查询出所有银行卡账户信息,显示卡号,身份证,姓名,余额。 + +select AccountCode,AccountPhone,RealName,CardMoney from AccountInfo +join BankCard on AccountInfo.AccountId = BankCard.AccountId + +-- 最好用个作用域来区分 +go +create view V_UserInfo(显示卡号,身份证,姓名,余额) +as +select AccountCode,AccountPhone,RealName,CardMoney from AccountInfo +join BankCard on AccountInfo.AccountId = BankCard.AccountId +go + +select * from V_UserInfo +``` + + + +## 2.1行转列 与 列转行 + +#### 2.1.1行转列 + +- case when用法 结合聚合函数sum or max + +```sql +select [year], +max(case when [month]=1 then amonth else 0 end) m1, +max(case when [month]=2 then amonth else 0 end) m2, +max(case when [month]=3 then amonth else 0 end) m3, +max(case when [month]=4 then amonth else 0 end )m4 +from Securities +group by [year] + +``` + +- pivot函数 + +```sql +-- 语法(格式) +table_source + +PIVOT( + +聚合函数(value_column) + +FOR pivot_column + +IN() + +) + + +--例子 +select * from Securities +pivot( + sum([amonth]) + for [month] in ([1],[2],[3],[4] ) +)as TT + +``` + + + +#### 2.2.1列转行 + +union \ No newline at end of file