diff --git "a/06\351\231\210\346\242\205\351\246\231/\344\275\234\344\270\232/20229.26\344\275\234\344\270\232.sql" "b/06\351\231\210\346\242\205\351\246\231/\344\275\234\344\270\232/20229.26\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1966b5833042cf7b45d263cc198d3169a7065647 --- /dev/null +++ "b/06\351\231\210\346\242\205\351\246\231/\344\275\234\344\270\232/20229.26\344\275\234\344\270\232.sql" @@ -0,0 +1,84 @@ +use BankTest +go + +--1)编写视图实现查询出所有银行卡账户信息,显示卡号,身份证,姓名,余额。 +create view v_account +as +select CardNo,AccountCode,RealName,CardMoney from AccountInfo a +join BankCard b on b.AccountId = a.AccountId +go +select * from v_account +go + + +--2)行转列常用做法: group by + sum(case when) /+count(case when) 数据分析+ over (paritition by, order by) +--​ 提示: pivot + +create database sec +go +use sec +go +create table secc +( +[year] int, +[month] int, +amount float +) +go +insert into secc([year],[month],amount) 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 * from secc +go + +------ +select * from secc +pivot( +MIN(amount) for [month] in ([1],[2],[3],[4]) +) as aa + +------ +select [year], +MIN((case when [month] = 1 then amount else 404 end))m1, +MIN((case when [month] = 2 then amount else 404 end))m2, +MIN((case when [month] = 3 then amount else 404 end))m3, +MIN((case when [month] = 4 then amount else 404 end))m4 +from secc +group by [year] + + +--列转行 union +--drop table stu +create table stu( +name varchar(20), +english int, +maths int, +music int +) +go + +insert into stu(name,english,maths,music) values +('Jim',90,88,99) +go + +----- +select name,'english' [subject],90 score from stu +union +select name,'maths' [subject],88 score from stu +union +select name,'music' [subject],99 score from stu +go + +----- +select name,subject,score from stu +unpivot( +score for subject in(english,maths,music) +)as v +go \ No newline at end of file diff --git "a/06\351\231\210\346\242\205\351\246\231/\347\254\224\350\256\260/2022.09.26\350\247\206\345\233\276(view).md" "b/06\351\231\210\346\242\205\351\246\231/\347\254\224\350\256\260/2022.09.26\350\247\206\345\233\276(view).md" new file mode 100644 index 0000000000000000000000000000000000000000..ebc59b6ded5eb227e90775253ccbb6334bed9442 --- /dev/null +++ "b/06\351\231\210\346\242\205\351\246\231/\347\254\224\350\256\260/2022.09.26\350\247\206\345\233\276(view).md" @@ -0,0 +1,66 @@ +2022.09.26视图(view) + +触发器,存储过程,proc + +为什么使用视图 + +重用SQL语句 +简化复杂的SQL操作 +使用表的一部分而不是整个表 +保护数据。可以授权用户访问表的特定部分的权限,而不是整个表的访问权限 +更改数据格式和表示 + +创建视图 + +``` +create view 名字 +as +查询条件 +``` + +利用视图简化复杂的联结 + +``` +CREATE VIEW ProductCustomers AS +SELECT cust_name,cust_contact,prod_id +FROM customers,orders,orderItems +WHERE customers.cust_id=orders.cust_id +AND orderitems.order_num=orders.order_num; +``` + +用视图重新格式化检索出的数据 + +``` +CREATE VendorLoactions AS +SELECT RTRIM(vend_name)+'('+RTTIM(vend_country)+')' AS vend_title +FROM Vendors; +``` + +用视图过滤不想要的数据 + +``` +CREATE VIEW CustomersEMailList AS +SELECT cust_id,cust_name,cust_email +FROM customers +WHERE Customers +WHERE cust_email IS NOT NULL; +``` + +with check option + +涉及增删改查对视图的无法进行操作 + +二级视图 + +并集A,B表合并 + +union 自动清除一个重复交集,共有的数据 + +intersect + +差集 + +except A-B=A表特有的数据 + +列转行:union + +行转列:pivot \ No newline at end of file