SQL-作为附加列的多个计数

SQL-作为附加列的多个计数,sql,Sql,我有一个问题: select e.Owner as 'Owner', COUNT(l.EnquiryID) as 'Total Sales Lines' from DayBookQuoteLines l, DayBookEnquiries e where l.EnquiryID = e.EnquiryID and MONTH(e.enquirydate) = 8 and YEAR(e.enquirydate) = 2012 group by e.Owner 这将返回包含名称和总计列的所有者列

我有一个问题:

select e.Owner as 'Owner', COUNT(l.EnquiryID) as 'Total Sales Lines'
from DayBookQuoteLines l, DayBookEnquiries e
where l.EnquiryID = e.EnquiryID
and MONTH(e.enquirydate) = 8 and YEAR(e.enquirydate) = 2012 
group by e.Owner
这将返回包含名称和总计列的所有者列,但我希望还有两列,在这两列中,我将应用额外的筛选器并再次计数,并添加:

l.LostStatusId =2 

剩下的结果集如下所示:

Owner      Total Lines    Total Sold    Total Lost    

Person1    124            112           12

我似乎无法回答正确的问题。我正在尝试使用子选项,但明显缺少一些内容,如有任何帮助,将不胜感激。

这将为您提供各种总计

select e.owner, LostStatusID , COUNT(l.EnquiryID)
from DayBookQuoteLines l
    inner join DayBookEnquiries e  
    on l.EnquiryID = e.EnquiryID    
group by owner, LostStatusID with rollup
如果你想水平排列,你需要一个枢轴。这取决于SQL的种类

select owner, [2]+[3] as total, [2],[3]
from 
(
    select e.owner, LostStatusID , l.EnquiryID
    from DayBookQuoteLines l
        inner join DayBookEnquiries e  
        on l.EnquiryID = e.EnquiryID    

) v
    pivot
(count(enquiryid) for LostStatusID in ([2],[3])) p

这将为您提供各种总计

select e.owner, LostStatusID , COUNT(l.EnquiryID)
from DayBookQuoteLines l
    inner join DayBookEnquiries e  
    on l.EnquiryID = e.EnquiryID    
group by owner, LostStatusID with rollup
如果你想水平排列,你需要一个枢轴。这取决于SQL的种类

select owner, [2]+[3] as total, [2],[3]
from 
(
    select e.owner, LostStatusID , l.EnquiryID
    from DayBookQuoteLines l
        inner join DayBookEnquiries e  
        on l.EnquiryID = e.EnquiryID    

) v
    pivot
(count(enquiryid) for LostStatusID in ([2],[3])) p

当条件满足时,您可以通过添加一条记录来有条件地对记录进行计数

select e.Owner as 'Owner', 
       COUNT(l.EnquiryID) as 'Total Sales Lines',
       sum(case when l.LostStatusId = 2 then 1 end) TotalSold,
       sum(case when l.LostStatusId = 3 then 1 end) TotalLost
  from DayBookQuoteLines l
 inner join DayBookEnquiries e
    on l.EnquiryID = e.EnquiryID
 where MONTH(e.enquirydate) = 8 
   and YEAR(e.enquirydate) = 2012 
 group by e.Owner

当条件满足时,您可以通过添加一条记录来有条件地对记录进行计数

select e.Owner as 'Owner', 
       COUNT(l.EnquiryID) as 'Total Sales Lines',
       sum(case when l.LostStatusId = 2 then 1 end) TotalSold,
       sum(case when l.LostStatusId = 3 then 1 end) TotalLost
  from DayBookQuoteLines l
 inner join DayBookEnquiries e
    on l.EnquiryID = e.EnquiryID
 where MONTH(e.enquirydate) = 8 
   and YEAR(e.enquirydate) = 2012 
 group by e.Owner

在没有看到模式的情况下很难编写,但您是否尝试过以下方法:

select 
    e.Owner as 'Owner', 
    COUNT(l.EnquiryID) as 'Total Sales Lines'
    count(select count(a.salesMade) from DayBookQuoteLines where month=MONTH(e.enquirydate)) as totalSold,
    count(select count(a.lostSales) from DayBookQuoteLines where month=MONTH(e.enquirydate)) as totalLost
from 
    DayBookQuoteLines l, 
    DayBookEnquiries e
where 
    l.EnquiryID = e.EnquiryID
    and MONTH(e.enquirydate) = 8 
    and YEAR(e.enquirydate) = 2012 
group by e.Owner

在没有看到模式的情况下很难编写,但您是否尝试过以下方法:

select 
    e.Owner as 'Owner', 
    COUNT(l.EnquiryID) as 'Total Sales Lines'
    count(select count(a.salesMade) from DayBookQuoteLines where month=MONTH(e.enquirydate)) as totalSold,
    count(select count(a.lostSales) from DayBookQuoteLines where month=MONTH(e.enquirydate)) as totalLost
from 
    DayBookQuoteLines l, 
    DayBookEnquiries e
where 
    l.EnquiryID = e.EnquiryID
    and MONTH(e.enquirydate) = 8 
    and YEAR(e.enquirydate) = 2012 
group by e.Owner