SQL一次性查詢一個字段不同條件下的統計結果(另一張表的統計數量)


 

做另一張表的統計,比如本部門有多少在職人員。本崗位有多少女生。

有兩個表,分別存放了【操作員】和【單據】,要根據單據的不同類型來分類匯總(銷售單、銷售退貨單,筆數和金額),並且顯示在同一張表里,不想用做兩次查詢再合並的方法,研究了一下,終於搞定:

d_employee表

d_bilndx表

 

代碼如下:

復制代碼
select  b.inputid as 開單員編號, 
        e.fullname as 開單員, 

        isnull( ( select count(*)
          from d_bilndx
          where draft=3 and biltype=12 and d_bilndx.inputid=e.id
        ), 0) as '銷售開單筆數',

        isnull( ( select sum(d_bilndx.amount)
          from d_bilndx
          where draft=3 and biltype=12 and d_bilndx.inputid=e.id
        ), 0) as '銷售開單金額',

        isnull( ( select count(*)
          from d_bilndx
          where draft=3 and biltype=13 and d_bilndx.inputid=e.id
        ), 0) as '銷售退單筆數',

        isnull( ( select sum(d_bilndx.amount)
          from d_bilndx
          where draft=3 and biltype=13 and d_bilndx.inputid=e.id
        ), 0) as '銷售退單金額',

        count(b.biltype) as 開單總筆數,
        sum(b.Amount) as 開單金額

from d_bilndx as b
left join d_employee as e 
on b.inputid=e.id 
where b.draft=3 and ( b.biltype=12 or b.biltype=13 )
group by b.inputid, e.fullname, e.id
復制代碼

 

得到結果:

 

 

補記:以上代碼有一個問題,就是如果沒有符合條件的單據,查到的結果為空,而我們可能希望,查不到符合條件的記錄,相關字段要顯示為0(並且按天來統計),改寫代碼如下:

 

復制代碼
select  e1.id as ePersonCode, e1.FullName as eFullName, 
        isnull(Bill_Sale_NUm, 0) as Bill_Sale_Num, 
        isnull(Bill_Sale_Amount, 0) as Bill_Sale_Amount, 
        isnull(Bill_SaleReturn_Num, 0) as Bill_SaleReturn_Num, 
        isnull(Bill_SaleReturn_Amount, 0) as Bill_SaleReturn_Amount
from d_employee as e1
left join (

        select  b.inputid as ePersonCode, 
                e.fullname as eFullName, 
        
                isnull( ( select count(*)
                  from d_bilndx
                  where biltype=12 and d_bilndx.inputid=e.id and d_bilndx.date>='2018-06-03' and d_bilndx.date<='2018-06-03'
                ), 0) as Bill_Sale_Num,
        
                isnull( ( select sum(d_bilndx.amount)
                  from d_bilndx
                  where biltype=12 and d_bilndx.inputid=e.id and d_bilndx.date>='2018-06-03' and d_bilndx.date<='2018-06-03'
                ), 0) as Bill_Sale_Amount,
        
                isnull( ( select count(*)
                  from d_bilndx
                  where biltype=13 and d_bilndx.inputid=e.id and d_bilndx.date>='2018-06-03' and d_bilndx.date<='2018-06-03'
                ), 0) as Bill_SaleReturn_Num,
        
                isnull( ( select sum(d_bilndx.amount)
                  from d_bilndx
                  where biltype=13 and d_bilndx.inputid=e.id and d_bilndx.date>='2018-06-03' and d_bilndx.date<='2018-06-03'
                ), 0) as Bill_SaleReturn_Amount,
        
                count(b.biltype) as Bill_Total_Num

        from d_employee as e 
        left join d_bilndx as b
        on b.inputid=e.id 
        where (b.draft=3 or b.draft=2) and ( b.biltype=12 or b.biltype=13 ) and  b.date>='2018-06-03' and b.date<='2018-06-03'
        group by b.inputid, e.fullname, e.id


) as t1
on e1.id = t1.ePersonCode
where e1.id<>'00000'
order by ePersonCode asc
復制代碼

 

得到結果如下:

 

轉自:https://www.cnblogs.com/skysowe/p/9117099.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM