简体   繁体   中英

SQL query optimisation / refactoring

Just wondering can anyone see a neater way to write this query. It seems like a lot of repetition to me but I can't see an easier way to write it. It is pulling back Year-to-date data.

Output looks like this:

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

The query is:

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]

You could certainly reduce the amount of code in the query by using something like:

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]

I've eliminated a lot of the duplicated joins and moved from full joins to just left joining the two summary result sets (test drives and sales) to the sales person table.

You haven't given details of your tables or any sample data so it's impossible to test the above query, but hopefully it will give you some ideas on how to write it a bit more concisely.

In terms of actual performance, I would imagine simplifying the code will likely help the optimiser, but this is something you would need to confirm in your own environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM