Question

I'm a big confused on this one. I'm trying to use a SQL statement that sums up two different records but displays both 'Makes' of each row. I currently have this statement..

 SELECT  
     tDealerships.CapShortName,
     SUM(tObjective.CommitObj) AS CommitObj, 
     SUM(tObjective.ActualMTD) AS MTD, 
     SUM(tObjective.DelToday) AS DelT,
     SUM(tObjective.ActualMTD)/Sum(tObjective.CommitObj) AS CommitUnit,
     SUM(tObjective.CommitGrossObj) AS CommitGrossObj, 
     SUM(tObjective.GrossActual) AS GrossActual,
     SUM(tObjective.GrossActual)/Sum(tObjective.CommitGrossObj) as CommitGross,
     Sum(tObjective.ActualMTD)/Sum(tObjective.GrossActual) AS MTDPRU 
 FROM 
     tObjective, tMake, tDealerships
 WHERE 
     tObjective.DealershipID = 10
     AND NewUsed = 'New'
     AND tObjective.MakeID = tMake.MakeID
     AND tObjective.DealershipID = tDealerships.DealershipID
     AND (tMake.Make LIKE '%BUICK%' OR tMake.Make LIKE '%GMC%')
 GROUP BY 
     tDealerships.CapShortName

which returns this..

enter image description here

However, I need to display the two makes that it is summing up which is Buick and GMC. If I add the make in the statement, I must group by the make which then separates them into two row sums.. one for Buick and then one for GMC. Is there a better way or doing this? I am able to make it do what I need it to do? I have been stuck on this one for a bit now. Any suggestions/help is greatly appreciated!

EDIT: The ideal result is having 1 result with an additional column named Make that displays both Buick/GMC together.

Was it helpful?

Solution

You had to hard-code the makes for the LIKE strings. Why not just hard code them in a literal for the make column?

 SELECT  
    tDealerships.CapShortName,
    SUM(tObjective.CommitObj) AS CommitObj, 
    SUM(tObjective.ActualMTD) AS MTD, 
    SUM(tObjective.DelToday) AS DelT,
    SUM(tObjective.ActualMTD)/Sum(tObjective.CommitObj) AS CommitUnit,
    SUM(tObjective.CommitGrossObj) AS CommitGrossObj, 
    SUM(tObjective.GrossActual) AS GrossActual,
    SUM(tObjective.GrossActual)/Sum(tObjective.CommitGrossObj) as CommitGross,
    Sum(tObjective.ActualMTD)/Sum(tObjective.GrossActual) AS MTDPRU 
    ------
    'GMC/Buick' As Make
    ------
 FROM tObjective, tMake, tDealerships
 WHERE tObjective.DealershipID = 10
    AND NewUsed = 'New'
    AND tObjective.MakeID = tMake.MakeID
    AND tObjective.DealershipID = tDealerships.DealershipID
    AND (tMake.Make LIKE '%BUICK%'
         OR tMake.Make LIKE '%GMC%')
 GROUP BY tDealerships.CapShortName

OTHER TIPS

I think that something as simple as grouping on the make as well would solve your problem, if I am reading it correctly?

SELECT  
 tDealerships.CapShortName,
 SUM(tObjective.CommitObj) AS CommitObj, 
 SUM(tObjective.ActualMTD) AS MTD, 
 SUM(tObjective.DelToday) AS DelT,
 SUM(tObjective.ActualMTD)/Sum(tObjective.CommitObj) AS CommitUnit,
 SUM(tObjective.CommitGrossObj) AS CommitGrossObj, 
 SUM(tObjective.GrossActual) AS GrossActual,
 SUM(tObjective.GrossActual)/Sum(tObjective.CommitGrossObj) as CommitGross,
 Sum(tObjective.ActualMTD)/Sum(tObjective.GrossActual) AS MTDPRU 
 FROM tObjective, tMake, tDealerships
 WHERE tObjective.DealershipID = 10
 AND NewUsed = 'New'
 AND tObjective.MakeID = tMake.MakeID
 AND tObjective.DealershipID = tDealerships.DealershipID
 AND (tMake.Make LIKE '%BUICK%'
 OR tMake.Make LIKE '%GMC%')
 GROUP BY tMake.Make, tDealerships.CapShortName
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top