SQL統計語句技巧

2021-08-27 12:22:32 字數 1307 閱讀 2800

select t.*, t.rowid from ts_orders t;

select * from ts_cust;

-- 查詢 type=0 ,1 的訂單數

select

count (o.oid) as 總訂單數 ,

count (o1.oid) as type1_訂單總數 ,

count (o2.oid) as type0_訂單總數

from ts_orders o

left join ts_orders o1 on o1.oid =o.oid and o1.type = 1

left join ts_orders o2 on o2.oid =o.oid and o2.type = 0

where 1=1

-- 統計 下單人數 (乙個人有可能下多個訂單,但也只算1個)

select

count ( distinct o.cid) as 總下單人數 ,

count ( distinct o1.cid) as type1_下單人數 ,

count ( distinct o2.cid) as type0_下單人數

from ts_orders o

left join ts_orders o1 on o1.oid =o.oid and o1.type = 1

left join ts_orders o2 on o2.oid =o.oid and o2.type = 0

where 1=1

;-- 統計每個使用者不同型別的訂單數量

select

--c.cid as 客戶編號,

c.name as 客戶名稱,

count(o.oid) as 訂單總數 ,

count(o1.oid) as type1_訂單總數,

count(o2.oid) as type0_訂單總數

from  ts_cust c

left join ts_orders o on c.cid =o.cid

left join ts_orders o1 on c.cid =o1.cid and o.oid =o1.oid and o1.type =1

left join ts_orders o2 on c.cid =o2.cid and o.oid =o2.oid and o2.type =0

where 1=1

group by c.name

order by 訂單總數 desc

;

SQL語句 統計

統計某年每個月的資料資訊 如何時間欄位為int型別的秒數,mysql用 例如 select hour from unixtime posttime u as hourtime,year from unixtime posttime u as yeartime,count businessid as ...

統計報表 sql統計語句

需要資料統計頁面,肯定需要匯出資料,於是,邊學邊寫,完成了一段sql 最早的版本是這樣的 分三條sql查出三種不同的狀態的記錄數 總記錄,未支付,已支付 select count as recordcount from t record where createtime 2019 01 01 00 ...

SQL語句 分組統計

一 教師號 星期號 是否有課 有 有 有 有 有 寫一條sql語句讓你變為這樣的表 教師號 星期一 星期二 星期三 各星期下的數字表示 對應的教師在星期幾已經排的課數 答案是 select 教師號 sum case when 星期號 1then 是否有課 else 0end as 星期一 sum c...