常用SQL



關於數據統計ratio_to_report over()這個不適合在用having操作

select pct from (

select a.gw_status, count(*), ratio_to_report(count(*)) over() pct

  from pgw_orderinfo a

where a.gw_requesttime > sysdate - 6 / 1440

  and a.gw_requesttime < sysdate - 1 / 1440

  and a.gw_pcs_id = 'pu'

group by a.gw_status

where gw_status=0;

結果:

GW_STATUS  COUNT(*)      PCT

0     2385       0.933463796477495

1     170  0.0665362035225049

select a.order_status, count(*), ratio_to_report(count(*)) over() pct

from eop_order_info a where a.order_time > sysdate - 6 / 1440

and a.order_time <  sysdate - 1 / 1440

group by a.order_status;

 

 

一段時間內

select * from pgw_orderinfo a where a.gw_requesttime between

to_date(to_char(sysdate,'YYYY-MM-DD')||'00:00:00','YYYY-MM-DD hh24:mi:ss') and

to_date(to_char(sysdate,'YYYY-MM-DD')||'09:10:59','YYYY-MM-DD hh24:mi:ss')

and a.gw_pcs_id='pu' and a.gw_status='3'  order by a.gw_serialid

and a.gw_requesttime>sysdate -1/12 and a.gw_status='3' ;

 

between

to_date('2014-07-01 00:00:00','YYYY-MM-DD hh24:mi:ss'and

to_date('2014-07-01 23:59:59','YYYY-MM-DD hh24:mi:ss')

 

 

mysql

between

str_to_date('2014-05-01 00:00:00','%Y-%m-%d') and

str_to_date('2014-08-06 11:50:00','%Y-%m-%d')

 

 

 

按時段統計

select trunc(a.gw_requesttime,'dd'),count(1from pgw_orderinfo a  where a.gw_requesttime between

       to_date('2013-05-12 00:00:00''yyyy-mm-dd hh24:mi:ss'and

       to_date('2013-05-15 10:05:00''yyyy-mm-dd hh24:mi:ss')

group by trunc(a.gw_requesttime,'dd')

 

 

select trunc(a.gw_requesttime,'hh24'),count(1from pgw_orderinfo a  where a.gw_requesttime between

       to_date('2013-05-14 00:00:00''yyyy-mm-dd hh24:mi:ss'and

       to_date('2013-05-14 23:59:59''yyyy-mm-dd hh24:mi:ss')

group by trunc(a.gw_requesttime,'hh24')

order by trunc(a.gw_requesttime,'hh24')

 

 

select trunc(a.gw_requesttime, 'dd'),

       decode(a.gw_status, 0'成功'1'失敗'3'處理中'),

      count(*)

  from pgw_orderinfo a

where a.gw_requesttime between

       to_date('2013-08-26 00:00:00''yyyy-mm-dd hh24:mi:ss'and

       to_date('2013-08-28 23:59:59''yyyy-mm-dd hh24:mi:ss')

group by trunc(a.gw_requesttime, 'dd'),

          decode(a.gw_status, 0'成功'1'失敗'3'處理中')

order by trunc(a.gw_requesttime, 'dd');




sql之left join、right join、inner join的區別

left join(左聯接) 返回包括左表中的所有記錄和右表中聯結字段相等的記錄 
right join(右聯接) 返回包括右表中的所有記錄和左表中聯結字段相等的記錄
inner join(等值連接) 只返回兩個表中聯結字段相等的行

舉例如下: 
--------------------------------------------
表A記錄如下:
aID     aNum
1     a20050111
2     a20050112
3     a20050113
4     a20050114
5     a20050115

表B記錄如下:
bID     bName
1     2006032401
2     2006032402
3     2006032403
4     2006032404
8     2006032408

--------------------------------------------
1.left join
sql語句如下: 
select * from A
left join B 
on A.aID = B.bID

結果如下:
aID     aNum     bID     bName
1     a20050111    1     2006032401
2     a20050112    2     2006032402
3     a20050113    3     2006032403
4     a20050114    4     2006032404
5     a20050115    NULL     NULL

(所影響的行數爲 5 行)
結果說明:
left join是以A表的記錄爲基礎的,A可以看成左表,B可以看成右表,left join是以左表爲準的.
換句話說,左表(A)的記錄將會全部表示出來,而右表(B)只會顯示符合搜索條件的記錄(例子中爲: A.aID = B.bID).
B表記錄不足的地方均爲NULL.
--------------------------------------------
2.right join
sql語句如下: 
select * from A
right join B 
on A.aID = B.bID

結果如下:
aID     aNum     bID     bName
1     a20050111    1     2006032401
2     a20050112    2     2006032402
3     a20050113    3     2006032403
4     a20050114    4     2006032404
NULL     NULL     8     2006032408

(所影響的行數爲 5 行)
結果說明:
仔細觀察一下,就會發現,和left join的結果剛好相反,這次是以右表(B)爲基礎的,A表不足的地方用NULL填充.
--------------------------------------------
3.inner join
sql語句如下: 
select * from A
innerjoin B 
on A.aID = B.bID

結果如下:
aID     aNum     bID     bName
1     a20050111    1     2006032401
2     a20050112    2     2006032402
3     a20050113    3     2006032403
4     a20050114    4     2006032404

結果說明:
很明顯,這裏只顯示出了 A.aID = B.bID的記錄.這說明inner join並不以誰爲基礎,它只顯示符合條件的記錄.
--------------------------------------------
注: 
LEFT JOIN操作用於在任何的 FROM 子句中,組合來源表的記錄。使用 LEFT JOIN 運算來創建一個左邊外部聯接。左邊外部聯接將包含了從第一個(左邊)開始的兩個表中的全部記錄,即使在第二個(右邊)表中並沒有相符值的記錄。

語法:FROM table1 LEFT JOIN table2 ON table1.field1 compopr table2.field2

說明:table1, table2參數用於指定要將記錄組合的表的名稱。
field1, field2參數指定被聯接的字段的名稱。且這些字段必須有相同的數據類型及包含相同類型的數據,但它們不需要有相同的名稱。
compopr參數指定關係比較運算符:"=", "<", ">", "<=", ">=" 或 "<>"。
如果在INNER JOIN操作中要聯接包含Memo 數據類型或 OLE Object 數據類型數據的字段,將會發生錯誤. 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章