1

只是想知道任何人都可以看到编写此查询的更简洁的方法。这对我来说似乎有很多重复,但我看不到更简单的方法来编写它。它正在撤回年初至今的数据。

输出如下所示:

name  Total New Drives  Total New Sales  Total Used Drives Total Used Sales    
Alan    41                31                 15               93    
Pascal  45                51                 35               33   

查询是:

select sp.name, x.ttlNewTestDrives, x.ttlNewSales, y.ttlUsedTestDrives, y.TtlUsedSales
from
(
    select ts.SalesPersonID, ttd.TotalTestDrives as TtlNewTestDrives ,ts.TotalSales as TtlNewSales
    from
    (       
            select  sp.[SalesPersonID], count([TestDriveID]) as TotalTestDrives
            from SalesPeople sp
            join TestDrives td 
            on sp.[SalesPersonID] = td.[SalesPerson_SalesPersonID]
            join cars c on
            c.[CarID] = td.[Car_CarID]
            where c.CarType = 'New'
            group by sp.[SalesPersonID]
        ) as ttd
    full outer join
        (
            select sp.[SalesPersonID], count(SaleID) as TotalSales
            from Sales s
            join SalesPeople sp
            on s.[Salesperson_SalesPersonID] = sp.[SalesPersonID]
            join cars c on
            s.[Car_CarID] = c.[CarID]
            where c.CarType = 'New'
            group by sp.[SalesPersonID]
        ) as ts
    on ts.[SalesPersonID] = ttd.[SalesPersonID]
    ) as x
full outer join
(   
        select ttd.SalesPersonID, ttd.TotalTestDrives as TtlUsedTestDrives ,ts.TotalSales as TtlUsedSales
        from
        (       
                select  sp.[SalesPersonID], count([TestDriveID]) as TotalTestDrives
                from SalesPeople sp
                join TestDrives td 
                on sp.[SalesPersonID] = td.[SalesPerson_SalesPersonID]
                join cars c on
                c.[CarID] = td.[Car_CarID]
                where c.CarType = 'Used'
                group by sp.[SalesPersonID]
            ) as ttd
        full outer join
            (
                select sp.[SalesPersonID], count(SaleID) as TotalSales
                from Sales s
                join SalesPeople sp
                on s.[Salesperson_SalesPersonID] = sp.[SalesPersonID]
                join cars c on
                s.[Car_CarID] = c.[CarID]
                where c.CarType = 'Used'
                group by sp.[SalesPersonID]
            ) as ts
        on ts.[SalesPersonID] = ttd.[SalesPersonID]
    ) y
on x.[SalesPersonID] = y.[SalesPersonID]
join SalesPeople sp
on x.[SalesPersonID] = sp.[SalesPersonID]
4

1 回答 1

3

您当然可以使用以下方法减少查询中的代码量:

select sp.name, td.NewTestDrives, s.NewSales, td.UsedTestDrives, s.NewSales
from SalesPeople sp
  left join
  (
    select td.[SalesPerson_SalesPersonID]
      , NewTestDrives = sum(case when c.CarType = 'New' then 1 else 0 end)
      , UsedTestDrives = sum(case when c.CarType = 'Used' then 1 else 0 end)
    from TestDrives td 
      inner join cars c on c.[CarID] = td.[Car_CarID]
  ) td on sp.SalesPersonID = td.[SalesPerson_SalesPersonID]
  left join
  (
    select s.[SalesPerson_SalesPersonID]
      , NewSales = sum(case when c.CarType = 'New' then 1 else 0 end)
      , UsedSales = sum(case when c.CarType = 'Used' then 1 else 0 end)
    from Sales s 
      inner join cars c on c.[CarID] = s.[Car_CarID]
  ) s on sp.SalesPersonID = s.[SalesPerson_SalesPersonID]

我已经消除了很多重复的联接,并从完全联接转移到将两个汇总结果集(试驾和销售)加入到销售人员表中。

您没有提供表或任何示例数据的详细信息,因此无法测试上述查询,但希望它能给您一些关于如何更简洁地编写它的想法。

就实际性能而言,我想简化代码可能会对优化器有所帮助,但这是您需要在自己的环境中确认的事情。

于 2013-05-22T10:40:07.310 回答