Oracle Sql Server相關查詢語句

2021-10-01 08:18:23 字數 4249 閱讀 2850

上週處理過 oracle、sql server 資料庫相關資料,發現其實它們的 sql 查詢語句有些是不太一樣的,比如行列轉置和將查詢結果插入新錶。本人還是比較願意寫 sql 語句的,網際網路的技術日新月異,前端後端各種框架、新技術每擱幾年就得更新一遍,根本學不過來,而 sql 語句這些多卻少有變化,值得好好學習。

此篇部落格將會根據後續的工作持續更新。

一、sql server

1、查詢表資料

select * from hzwater
2、擷取字段

擷取欄位的前 6 位

select substring(t.cbmonth,0,7) as cbmonths from hzwater as t
3、增加幾列(行列倒置)

原表字段 mainid、user_name、user_addr、ysxz、cbmonth、biaodi、meter_no、pianqu

首先將 cbmonth 字段擷取前 6 位改為 cbmonths 字段,然後再增加幾行,增加幾列其實是按月份增加幾個字段,如需要增加 201601、201602、201603、201604、201605、201606、201607、201608、201609、201610......201812

使用 case......when......語句實現:

select meter_no as meter_no,mainid as mainid,user_name as user_name,user_addr as user_addr,ysxz as ysxz,pianqu as pianqu,

max(case cbmonths when '201601' then cbsl else 0 end) as '201601',

max(case cbmonths when '201602' then cbsl else 0 end) as '201602',

max(case cbmonths when '201603' then cbsl else 0 end) as '201603',

max(case cbmonths when '201604' then cbsl else 0 end) as '201604',

max(case cbmonths when '201605' then cbsl else 0 end) as '201605',

max(case cbmonths when '201606' then cbsl else 0 end) as '201606',

max(case cbmonths when '201607' then cbsl else 0 end) as '201607'

from [hzwater_guotu].[dbo].middle

group by meter_no,mainid,user_name,user_addr,ysxz,pianqu

order by meter_no,user_name desc

注意:group by 後面的字段要跟前面 select 字段一樣(除了 max(case...when)裡面的字段),否則容易出錯;

得到的結果是:

4、將查詢結果放入建立的新錶

使用

select * into newtablename from ...
如:

select * into middle from

(select t.mainid,t.user_name,t.user_addr,t.ysxz,substring(t.cbmonth,0,7)

as cbmonths,t.cbsl,t.biaodi,t.meter_no,t.pianqu from hzcbdata as t)b

二、oracle

1、查詢資料

select * from temp
2、增加幾列(行列倒置)

使用 decode 語句實現:

select * from (select 用電類別,使用者名稱,用電位址,

sum (decode(dfny, '201701' , 計費電量, 0 )) "201701",

sum (decode(dfny, '201702' , 計費電量, 0 )) "201702",

sum (decode(dfny, '201703' , 計費電量, 0 )) "201703",

sum (decode(dfny, '201704' , 計費電量, 0 )) "201704",

sum (decode(dfny, '201705' , 計費電量, 0 )) "201705",

sum (decode(dfny, '201706' , 計費電量, 0 )) "201706",

sum (decode(dfny, '201707' , 計費電量, 0 )) "201707",

sum (decode(dfny, '201708' , 計費電量, 0 )) "201708",

sum (decode(dfny, '201709' , 計費電量, 0 )) "201709",

sum (decode(dfny, '201710' , 計費電量, 0 )) "201710",

sum (decode(dfny, '201711' , 計費電量, 0 )) "201711",

sum (decode(dfny, '201712' , 計費電量, 0 )) "201712",

sum (decode(dfny, '201801' , 計費電量, 0 )) "201801",

sum (decode(dfny, '201802' , 計費電量, 0 )) "201802",

sum (decode(dfny, '201803' , 計費電量, 0 )) "201803",

sum (decode(dfny, '201804' , 計費電量, 0 )) "201804",

sum (decode(dfny, '201805' , 計費電量, 0 )) "201805",

sum (decode(dfny, '201806' , 計費電量, 0 )) "201806",

sum (decode(dfny, '201807' , 計費電量, 0 )) "201807",

sum (decode(dfny, '201808' , 計費電量, 0 )) "201808",

sum (decode(dfny, '201809' , 計費電量, 0 )) "201809",

sum (decode(dfny, '201810' , 計費電量, 0 )) "201810",

sum (decode(dfny, '201811' , 計費電量, 0 )) "201811",

sum (decode(dfny, '201812' , 計費電量, 0 )) "201812"

from tempdlt20191203 group by 用電類別,使用者名稱,用電位址)t

處理完之後的結果為:

3、將表轉置並查詢

select * from (select 使用者名稱,用電位址,sum (decode(dfny, '201701' , 計費電量, 0 )) "201701",

sum (decode(dfny, '201702' , 計費電量, 0 )) "201702",

sum (decode(dfny, '201703' , 計費電量, 0 )) "201703",

sum (decode(dfny, '201704' , 計費電量, 0 )) "201704",

sum (decode(dfny, '201705' , 計費電量, 0 )) "201705"

from tempdlt20191203 group by 使用者名稱,用電位址)t where t.使用者名稱='向陽';

4、將查詢結果放入建立的新錶

create table newtablename as select * from b;

除錯oracle, sql server儲存過程

關於儲存過程的除錯,知道方法以後很簡單,但在不知道的時候,為了測試乙個儲存過程的正確性,print,插入臨時表等可謂是使出了渾身解數,煩不勝煩 下面就把我工作中除錯儲存過程的方法,簡單的說明一下 除錯oracle儲存過程 環境 win2003 server oracle9i pl sql devel...

Oracle SQLServer中實現跨庫查詢

oracle sqlserver中實現跨庫查詢 一 在sqlserver中連線另乙個sqlserver庫資料 在sql中,要想在本地庫中查詢另乙個資料庫中的資料表時,可以建立乙個鏈結伺服器 exec master.dbo.sp addlinkedserver server n 別名 srvprodu...

Oracle SQLServer中實現跨庫查詢

一 在sqlserver中連線另乙個sqlserver庫資料 exec master.dbo.sp addlinkedserver server n 別名 srvproduct n 庫名 provider n sqloledb datasrc n 伺服器位址 exec master.dbo.sp a...