具有主select语句的多个复杂查询

时间:2016-02-02 18:48:12

标签: sql sql-server tsql case-when

所以,我试图找到这个查询的例子,看看它是否优雅或是否有更好的方法,但我没有在任何地方找到任何这样的例子。它工作正常,并且在中小型数据库上运行得相当快,但我不知道是否有更好/更快的方法来完成此查询。

有关改进此事的任何建议吗?

select 
(select count(b.bookingnbr) from Customer as cu
inner join Sales as b on b.CugFK=cu.NameID
inner join Gender as g on cu.SexID=g.SexCodeID
inner join SalesOrderHistory as ssh on ssh.SalesFK=b.SalesPK
inner join OrderStatus as ss on ss.OrderStatusID=ssh.OrderStatusFK
where b.SalesDtTm between '01/01/2015 00:00:00' and '12/31/2015 23:59:00'
and ssh.DeletedDtTm is null
and g.sexcode='F'
and ss.IsHold=1
group by g.SexCode) 'Female Hold',

(select count(b.bookingnbr) from Customer as cu
inner join Sales as b on b.cugFK=cu.NameID
inner join Gender as g on cu.SexID=g.SexCodeID
inner join SalesOrderHistory as ssh on ssh.SalesFK=b.SalesPK
inner join OrderStatus as ss on ss.OrderStatusID=ssh.OrderStatusFK
where b.SalesDtTm between '01/01/2015 00:00:00' and '12/31/2015 23:59:00'
and ssh.DeletedDtTm is null
and g.sexcode='M'
and ss.IsHold=1
group by g.SexCode) 'Male Hold',

(select count(b.bookingnbr) from Customer as cu
inner join Sales as b on b.CugFK=cu.NameID
inner join Gender as g on cu.SexID=g.SexCodeID
inner join SalesOrderHistory as ssh on ssh.SalesFK=b.SalesPK
inner join OrderStatus as ss on ss.OrderStatusID=ssh.OrderStatusFK
where b.SalesDtTm between '01/01/2015 00:00:00' and '12/31/2015 23:59:00'
and ssh.DeletedDtTm is null
and g.sexcode='F'
and (ss.IsFinal=1 and ss.code<>'PC')
group by g.SexCode) 'Female Finalized',

(select count(b.bookingnbr) from Customer as cu
inner join Sales as b on b.CugFK=cu.NameID
inner join Gender as g on cu.SexID=g.SexCodeID
inner join SalesOrderHistory as ssh on ssh.SalesFK=b.SalesPK
inner join OrderStatus as ss on ss.OrderStatusID=ssh.OrderStatusFK
where b.SalesDtTm between '01/01/2015 00:00:00' and '12/31/2015 23:59:00'
and ssh.DeletedDtTm is null
and g.sexcode='M'
and (ss.IsFinal=1 and ss.code<>'PC')
group by g.SexCode) 'Male Finalized',

(select count(b.bookingnbr) from Customer as cu
inner join Sales as b on b.CugFK=cu.NameID
inner join Gender as g on cu.SexID=g.SexCodeID
inner join SalesOrderHistory as ssh on ssh.SalesFK=b.SalesPK
inner join OrderStatus as ss on ss.OrderStatusID=ssh.OrderStatusFK
where b.SalesDtTm between '01/01/2015 00:00:00' and '12/31/2015 23:59:00'
and ssh.DeletedDtTm is null
and g.sexcode='F'
and (ss.IsOrdered=1 and ss.code='PC')
group by g.SexCode) 'Female Ordered',

(select count(b.bookingnbr) from Customer as cu
inner join Sales as b on b.CugFK=cu.NameID
inner join Gender as g on cu.SexID=g.SexCodeID
inner join SalesOrderHistory as ssh on ssh.SalesFK=b.SalesPK
inner join OrderStatus as ss on ss.OrderStatusID=ssh.OrderStatusFK
where b.SalesDtTm between '01/01/2015 00:00:00' and '12/31/2015 23:59:00'
and ssh.DeletedDtTm is null
and g.sexcode='M'
and (ss.IsOrdered=1 and ss.code='PC')
group by g.SexCode) 'Male Ordered',

(select count(b.bookingnbr)  from Customer as cu
inner join Sales as b on b.CugFK=cu.NameID
inner join Gender as g on cu.SexID=g.SexCodeID
inner join SalesOrderHistory as ssh on ssh.SalesFK=b.SalesPK
inner join OrderStatus as ss on ss.OrderStatusID=ssh.OrderStatusFK
where b.SalesDtTm between '01/01/2015 00:00:00' and '12/31/2015 23:59:00'
and ssh.DeletedDtTm is null
and g.sexcode='F'
and ss.IsApproved=1
group by g.SexCode) 'Female Approved',

(select count(b.bookingnbr)  from Customer as cu
inner join Sales as b on b.CugFK=cu.NameID
inner join Gender as g on cu.SexID=g.SexCodeID
inner join SalesOrderHistory as ssh on ssh.SalesFK=b.SalesPK
inner join OrderStatus as ss on ss.OrderStatusID=ssh.OrderStatusFK
where b.SalesDtTm between '01/01/2015 00:00:00' and '12/31/2015 23:59:00'
and ssh.DeletedDtTm is null
and g.sexcode='M'
and ss.IsApproved=1
group by g.SexCode) 'Male Approved'

1 个答案:

答案 0 :(得分:1)

不完整的答案,只是解释我的评论

像这样的东西

    select sum(case when g.sexcode='F'and and ss.IsHold=1 then 1 else 0 end ) 'Female Hold',
    sum(case when g.sexcode='M'and ss.IsHold=1 then 1 else 0 end ) 'Male Hold',
   sum(case when g.sexcode='F'and and ss.IsFinal=1 and ss.code<>'PC' then 1 else 0 end ) 'Female Finalized'
--,....
    from Customer as cu
    inner join Sales as b on b.CugFK=cu.NameID
    inner join Gender as g on cu.SexID=g.SexCodeID
    inner join SalesOrderHistory as ssh on ssh.SalesFK=b.SalesPK
    inner join OrderStatus as ss on ss.OrderStatusID=ssh.OrderStatusFK
    where b.SalesDtTm between '01/01/2015 00:00:00' and '12/31/2015 23:59:00'
    and ssh.DeletedDtTm is null