SQL 統計的用法(Union All)

2021-06-04 00:32:18 字數 1606 閱讀 6927

1、按客戶統計 

select 0 aa,t_customer.customerid,t_customer.customername a1 ,  customername, ' ' modelname,sum(borrownumber)  as borrownumber

from t_borrowrecord inner join t_customer on t_borrowrecord.customerid = t_customer.customerid

inner join t_model on t_borrowrecord.modelid = t_model.modelid

group by t_customer.customerid,customername

union all

select 1 aa,t_customer.customerid a1 ,t_customer.customername,' ' customername, modelname,sum(borrownumber) as borrownumber

from t_borrowrecord inner join t_customer on t_borrowrecord.customerid = t_customer.customerid

inner join t_model on t_borrowrecord.modelid = t_model.modelid

group by t_customer.customerid,t_customer.customername,modelname

order by t_customer.customerid,aa

2、按樣機統計

select 0 aa,t_model.modelid,modelname a1, modelname,'' customername,sum(borrownumber) as borrownumber

from t_borrowrecord inner join t_customer on t_borrowrecord.customerid = t_customer.customerid

inner join t_model on t_borrowrecord.modelid = t_model.modelid

group by t_model.modelid,modelname

union all

select 1 aa,t_model.modelid,modelname a1, '' modelname, customername,sum(borrownumber) as borrownumber

from t_borrowrecord inner join t_customer on t_borrowrecord.customerid = t_customer.customerid

inner join t_model on t_borrowrecord.modelid = t_model.modelid

group by t_model.modelid,modelname,customername

order by t_model.modelid,aa

sql 統計常用的sql

統計常用的sql 統計常用的sql語句 今天的所有資料 select from 表名 where datediff dd,datetime型別字段,getdate 0昨天的所有資料 select from 表名 where datediff dd,datetime型別字段,getdate 17天內的...

Mysql中的Union和Union All查詢

union 用於合併兩個或多個 select 語句的結果集,並消去表中任何重複行。union 內部的 select 語句必須擁有相同數量的列,列也必須擁有相似的資料型別。同時,每條 select 語句中的列的順序必須相同。select name from a union select name fr...

統計報表 sql統計語句

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