diff --git "a/38\345\274\240\345\205\210\346\235\260/\344\275\234\344\270\232/2022.9.26\344\275\234\344\270\232--\350\247\206\345\233\276\343\200\201\345\210\227\350\275\254\350\241\214.sql" "b/38\345\274\240\345\205\210\346\235\260/\344\275\234\344\270\232/2022.9.26\344\275\234\344\270\232--\350\247\206\345\233\276\343\200\201\345\210\227\350\275\254\350\241\214.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d6c882b61122bac1f54849b2c0997757e9007af3 --- /dev/null +++ "b/38\345\274\240\345\205\210\346\235\260/\344\275\234\344\270\232/2022.9.26\344\275\234\344\270\232--\350\247\206\345\233\276\343\200\201\345\210\227\350\275\254\350\241\214.sql" @@ -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/38\345\274\240\345\205\210\346\235\260/\347\254\224\350\256\260/2022.9.26\347\254\224\350\256\260--\350\247\206\345\233\276\343\200\201\350\241\214\350\275\254\345\210\227.md" "b/38\345\274\240\345\205\210\346\235\260/\347\254\224\350\256\260/2022.9.26\347\254\224\350\256\260--\350\247\206\345\233\276\343\200\201\350\241\214\350\275\254\345\210\227.md" new file mode 100644 index 0000000000000000000000000000000000000000..1f4ff2c518c6ce10cc6da57ae69d3e7e0099c89d --- /dev/null +++ "b/38\345\274\240\345\205\210\346\235\260/\347\254\224\350\256\260/2022.9.26\347\254\224\350\256\260--\350\247\206\345\233\276\343\200\201\350\241\214\350\275\254\345\210\227.md" @@ -0,0 +1,79 @@ +# 1.瑙嗗浘 + +## 1.1瑙嗗浘鐨勬杩 + +璇曞浘锛坴iew锛夋槸鏁版嵁搴撲腑鐨勪竴涓璞★紝瀹冩槸鏁版嵁搴撶鐞嗙郴缁熸彁渚涚粰鐢ㄦ埛鐨勪互澶氱瑙掑害瑙傚療鏁版嵁搴撲腑鏁版嵁鐨勪竴绉嶉噸瑕佹満鍒躲傚畠瀵瑰簲涓夌妯″紡涓殑澶栨ā寮忋 + +鍦⊿QL涓紝璇曞浘鏄熀浜嶴QL璇彞鐨勭粨鏋滈泦鐨勫彲瑙嗗寲鐨勮〃銆 + +瑙嗗浘鍖呭惈琛屽拰鍒楋紝灏卞儚涓涓湡瀹炵殑琛ㄣ傝鍥句腑鐨勫瓧娈靛氨鏄潵鑷竴涓垨澶氫釜鏁版嵁搴撲腑鐨勭湡瀹炵殑琛ㄤ腑鐨勫瓧娈点備絾瑙嗗浘涓庡熀鏈〃涓嶅悓锛**璇曞浘鏄竴涓櫄琛**銆傛暟鎹簱涓彧瀛樺偍瑙嗗浘鐨勫畾涔夛紝鑰屼笉瀛樺偍瑙嗗浘鎵鍖呭惈鐨勬暟鎹紝杩欎簺鏁版嵁浠嶅瓨鏀惧湪鍘熸潵鐨勫熀鏈〃涓傝繖绉嶆ā寮忔湁涓や釜濂藉锛 + +瑙嗗浘鏁版嵁濮嬬粓涓庡熀鏈〃鏁版嵁淇濇寔涓鑷达紝褰撳熀鏈〃鍙戠敓鍙樺寲鏃讹紝浠庤鍥句腑鏌ヨ鍑虹殑鏁版嵁涔熶細闅忎箣鍙樺寲銆 +鑺傜渷瀛樺偍绌洪棿銆傚綋鏁版嵁閲忛潪甯稿ぇ鏃讹紝閲嶅瀛樺偍鏁版嵁闈炲父鑰楄垂绌洪棿銆 +瑙嗗浘鍙互浠庝竴涓熀鏈〃涓彁鍙栨暟鎹紝涔熷彲浠ヤ粠澶氫釜鍩烘湰琛ㄤ腑鎻愬彇鏁版嵁锛岀敋鑷宠繕鍙互浠庡叾浠栬鍥句腑鎻愬彇鏁版嵁锛屽悎鐞嗗埄鐢ㄥ彲浠ュ甫鏉ュ緢澶氬ソ澶勶細 + +**绠鍖栨暟鎹煡璇€** +**浣跨敤鎴疯兘澶熶粠澶氳搴︾湅寰呭悓涓鏁版嵁銆** +**鎻愰珮鏁版嵁鐨勫畨鍏ㄦс** +**鎻愪緵浜嗕竴瀹氱▼搴︾殑閫昏緫鐙珛鎬с** + +```SQL +CREATE VIEW <瑙嗗浘鍚> [(鍒楀悕 [, ...n])] +AS + SELECT璇彞 +``` + +1. SELECT璇彞涓氬父涓嶅寘鍚玂RDER BY 鍜孌ISTINCT瀛愬彞 +2. 鍦ㄥ畾涔夎鍥炬椂瑕佷箞鍒跺畾瑙嗗浘鐨勫叏閮ㄥ垪鍚嶏紝瑕佷箞鍏ㄩ儴鐪佺暐涓嶅啓锛屼笉鑳藉彧鍐欒鍥剧殑閮ㄥ垎鍒楀悕銆 + +# 2.琛岃浆鍒楀拰鍒楄浆琛 + +## 2.1琛岃浆鍒 + +### 2.1.1 case when 浠ュ強 sum/max + +```sql +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 + +```sql +select * from [mod] --1 +pivot +( +sum(amount) --3 +for [month] in ([1],[2],[3],[4]) --2 +) as amount + + +--1鏁版嵁鏉ユ簮 +--2閭d簺鍊煎仛鏂板垪鐨勫悕瀛 +--3瀵规柊鍒楃殑鍊肩殑瑕佹眰 +``` + +## 2.2鍒楄浆琛 + +### 2.2.1union + +```sql +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 + +```sql + +select * from Sales +unpivot (Amount for people in (A1,A2,A3,A4)) as a +``` +