From ef9d8c30d68c6eb27f60d6e66bb16fa11829d805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=AC=A3=E7=87=95?= <932306904@qq.com> Date: Tue, 27 Sep 2022 23:21:22 +0800 Subject: [PATCH] =?UTF-8?q?31=E5=90=B4=E6=AC=A3=E7=87=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\350\247\206\345\233\276.md" | 85 +++++++++++++++++++ .../2022.09.27\350\247\206\345\233\276.md" | 62 ++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 "31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\350\247\206\345\233\276.md" create mode 100644 "31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022.09.27\350\247\206\345\233\276.md" diff --git "a/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\350\247\206\345\233\276.md" "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\350\247\206\345\233\276.md" new file mode 100644 index 0000000..5758484 --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\350\247\206\345\233\276.md" @@ -0,0 +1,85 @@ +``` +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 +``` \ No newline at end of file diff --git "a/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022.09.27\350\247\206\345\233\276.md" "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022.09.27\350\247\206\345\233\276.md" new file mode 100644 index 0000000..fb1147f --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022.09.27\350\247\206\345\233\276.md" @@ -0,0 +1,62 @@ +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 -- Gitee