Math Operators with SQL Statements

Hello Pros -

Hope you are doing well.

I am running into an issue with writing a query that will do current year comparison to previous year. In my code below it stores current year month for beginning to end of month in separate variables. Than it also stores the same but for previous year. As you proceed, you will see the t-sql query that will display all stores, sales data, volume of transactions in cars, GPM, and ARO.


However, my conumdrum is how do I display an additional column that will do the operation of previous year for the month of January and than subtract, add, multiply or divide if need be and display only the sales data, car count, GPM and ARO? Is this possible?

I have attached a picture of the data results when I run the 2016 query below. Let know if you have any questions or you need me to verify.

Sales Data


DECLARE @begJan DATETIME, @endJan DATETIME, @begJanPrev DATETIME, @endJanPrev DATETIME
SET @begJan = '01/01/2016'
SET @endJan = '01/31/2016'
SET @begJanPrev = '01/01/2015'
SET @endJanPrev = '01/31/2015'


select 

B.store_Num as [Store Name],
A.store_UIN as [Store Code],
B.store_Name as [Store Name],
FORMAT(SUM(total-tax), 'C', 'en-us') AS [Sales],
COUNT(RO_NO) as [Car Count],
FORMAT((((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS)) - (SUM(P_COST + T_COST))) / (SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS))), 'P', 'EN-US') as [GPM],
FORMAT(((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET + SUPPLIES - DISP - DISL -DISS)) / (COUNT(RO_NO))), 'C', 'EN-US') as [ARO],


from HRO as A
left JOIN StoreInfo as B
  ON A.store_UIN = B.store_UIN

where
( PAY_DATE >= @begJan) And (PAY_DATE <= @endJan) and Status IN ('C','Q')  


group by B.store_Num, A.store_UIN, B.store_Name, B.store_OpeningDate, B.store_UIN

order by B.store_Num, A.store_UIN asc

Open in new window

sj77Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pcelbaCommented:
You should start from the list of stores and then join annual results twice, e.g.:
SELECT 
  st.store_Num AS [Store Num],
  st.store_UIN as [Store Code],
  st.store_Name as [Store Name],
  r1.Sales AS [Sales1], 
  r1.CarCnt AS [Car Count1],
  r1.GPM AS GPM1,
  r1.ARO AS ARO1,
  r2.Sales AS Sales2, 
  r2.CarCnt AS [Car Count2],
  r2.GPM AS GPM2,
  r2.ARO AS ARO2
FROM StoreInfo st
LEFT JOIN (
   SELECT
     A.store_UIN,
     FORMAT(SUM(total-tax), 'C', 'en-us') AS [Sales],
     COUNT(RO_NO) as [CarCnt],
     FORMAT((((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS)) - (SUM(P_COST + T_COST))) / (SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS))), 'P', 'EN-US') as [GPM],
     FORMAT(((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET + SUPPLIES - DISP - DISL -DISS)) / (COUNT(RO_NO))), 'C', 'EN-US') as [ARO]
  FROM HRO as A
 WHERE ( PAY_DATE >= @begJanPrev) And (PAY_DATE <= @endJanPrev) and Status IN ('C','Q')  
 GROUP BY A.store_UIN
  ) r1 OR r1.store_UIN = st.store_UIN
LEFT JOIN (
   SELECT
     A.store_UIN,
     FORMAT(SUM(total-tax), 'C', 'en-us') AS [Sales],
     COUNT(RO_NO) as [CarCnt],
     FORMAT((((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS)) - (SUM(P_COST + T_COST))) / (SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS))), 'P', 'EN-US') as [GPM],
     FORMAT(((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET + SUPPLIES - DISP - DISL -DISS)) / (COUNT(RO_NO))), 'C', 'EN-US') as [ARO]
  FROM HRO as A
 WHERE ( PAY_DATE >= @begJan) And (PAY_DATE <= @endJan) and Status IN ('C','Q')  
 GROUP BY A.store_UIN
  ) r2 OR r2.store_UIN = st.store_UIN

order by st.store_Num, st.store_UIN asc

Open in new window

Remember the fact NULL values can appear on the output so you have to possibly convert them before further calculations.
You may filter out inactive stores by appropriate WHERE clause.
Optional way of inner subselects is to have just one groupped by YEAR + MONTH.
1
sj77Author Commented:
Pcelba -

Thank you for the information. Never would have thought that logic. I added in the declare variables and ran the query and I did get an error. Any idea why the OR or maybe the r2 would throw it off?

ERROR
Msg 156, Level 15, State 1.
Incorrect syntax near the keyword 'OR'.
Msg 102, Level 15, State 1.
Incorrect syntax near 'r2'. (Line 41)
---------------------------------------------------------

DECLARE @begJan DATETIME, @endJan DATETIME, @begJanPrev DATETIME, @endJanPrev DATETIME
SET @begJan = '01/01/2016'
SET @endJan = '01/31/2016'
SET @begJanPrev = '01/01/2015'
SET @endJanPrev = '01/31/2015'

SELECT 
  st.store_Num AS [Store Num],
  st.store_UIN as [Store Code],
  st.store_Name as [Store Name],
  r1.Sales AS [Sales1], 
  r1.CarCnt AS [Car Count1],
  r1.GPM AS GPM1,
  r1.ARO AS ARO1,
  r2.Sales AS Sales2, 
  r2.CarCnt AS [Car Count2],
  r2.GPM AS GPM2,
  r2.ARO AS ARO2
FROM StoreInfo st
LEFT JOIN (
   SELECT
     A.store_UIN,
     FORMAT(SUM(total-tax), 'C', 'en-us') AS [Sales],
     COUNT(RO_NO) as [CarCnt],
     FORMAT((((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS)) - (SUM(P_COST + T_COST))) / (SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS))), 'P', 'EN-US') as [GPM],
     FORMAT(((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET + SUPPLIES - DISP - DISL -DISS)) / (COUNT(RO_NO))), 'C', 'EN-US') as [ARO]
  FROM HRO as A
 WHERE ( PAY_DATE >= @begJanPrev) And (PAY_DATE <= @endJanPrev) and Status IN ('C','Q')  
 GROUP BY A.store_UIN
  ) r1 OR r1.store_UIN = st.store_UIN
LEFT JOIN (
   SELECT
     A.store_UIN,
     FORMAT(SUM(total-tax), 'C', 'en-us') AS [Sales],
     COUNT(RO_NO) as [CarCnt],
     FORMAT((((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS)) - (SUM(P_COST + T_COST))) / (SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS))), 'P', 'EN-US') as [GPM],
     FORMAT(((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET + SUPPLIES - DISP - DISL -DISS)) / (COUNT(RO_NO))), 'C', 'EN-US') as [ARO]
  FROM HRO as A
 WHERE ( PAY_DATE >= @begJan) And (PAY_DATE <= @endJan) and Status IN ('C','Q')  
 GROUP BY A.store_UIN
  ) r2 OR r2.store_UIN = st.store_UIN

order by st.store_Num, st.store_UIN asc

Open in new window

0
pcelbaCommented:
Sorry, it should have been ON, I didn't test the command so some tuning is expected...
DECLARE @begJan DATETIME, @endJan DATETIME, @begJanPrev DATETIME, @endJanPrev DATETIME
SET @begJan = '01/01/2016'
SET @endJan = '01/31/2016'
SET @begJanPrev = '01/01/2015'
SET @endJanPrev = '01/31/2015'

SELECT 
  st.store_Num AS [Store Num],
  st.store_UIN as [Store Code],
  st.store_Name as [Store Name],
  r1.Sales AS [Sales1], 
  r1.CarCnt AS [Car Count1],
  r1.GPM AS GPM1,
  r1.ARO AS ARO1,
  r2.Sales AS Sales2, 
  r2.CarCnt AS [Car Count2],
  r2.GPM AS GPM2,
  r2.ARO AS ARO2
FROM StoreInfo st
LEFT JOIN (
   SELECT
     A.store_UIN,
     FORMAT(SUM(total-tax), 'C', 'en-us') AS [Sales],
     COUNT(RO_NO) as [CarCnt],
     FORMAT((((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS)) - (SUM(P_COST + T_COST))) / (SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS))), 'P', 'EN-US') as [GPM],
     FORMAT(((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET + SUPPLIES - DISP - DISL -DISS)) / (COUNT(RO_NO))), 'C', 'EN-US') as [ARO]
  FROM HRO as A
 WHERE ( PAY_DATE >= @begJanPrev) And (PAY_DATE <= @endJanPrev) and Status IN ('C','Q')  
 GROUP BY A.store_UIN
  ) r1 ON r1.store_UIN = st.store_UIN
LEFT JOIN (
   SELECT
     A.store_UIN,
     FORMAT(SUM(total-tax), 'C', 'en-us') AS [Sales],
     COUNT(RO_NO) as [CarCnt],
     FORMAT((((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS)) - (SUM(P_COST + T_COST))) / (SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS))), 'P', 'EN-US') as [GPM],
     FORMAT(((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET + SUPPLIES - DISP - DISL -DISS)) / (COUNT(RO_NO))), 'C', 'EN-US') as [ARO]
  FROM HRO as A
 WHERE ( PAY_DATE >= @begJan) And (PAY_DATE <= @endJan) and Status IN ('C','Q')  
 GROUP BY A.store_UIN
  ) r2 ON r2.store_UIN = st.store_UIN

order by st.store_Num, st.store_UIN asc

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

pcelbaCommented:
BTW, what data type is the PAY_DATE column?  If it is DateTime then you should rather test if it is less than the first day of the next month:

@endJan 01/31/2016  converted to DateTime gives 01/31/2016 00:00:00 and if the PAY_DATE contains some non-zero time part then the results could be incorrect.
0
sj77Author Commented:
Pcelba!

It worked! Thank you so much. Now a question I had. Assume I display that data like I just did but how would I do an operation on it such as adding, subtracting, etc in my select statement?

like this?

r1.Sales + r2.Sales
0
sj77Author Commented:
AWESOME JOB! Was able to assist me and took the time to explain the solution.
0
pcelbaCommented:
Note to calculations:
Inner queries contain FORMAT() function which returns (formatted) character string. You cannot use such values in arithmetic operations. So to e.g. sum two years results just remove the FORMAT() and then you may calculate whatever you need:
DECLARE @begJan DATETIME, @endJan DATETIME, @begJanPrev DATETIME, @endJanPrev DATETIME
SET @begJan = '01/01/2016'
SET @endJan = '01/31/2016'
SET @begJanPrev = '01/01/2015'
SET @endJanPrev = '01/31/2015'

SELECT 
  st.store_Num AS [Store Num],
  st.store_UIN as [Store Code],
  st.store_Name as [Store Name],
  r1.Sales AS [Sales1], 
  r1.CarCnt AS [Car Count1],
  r1.GPM AS GPM1,
  r1.ARO AS ARO1,
  r2.Sales AS Sales2, 
  r2.CarCnt AS [Car Count2],
  r2.GPM AS GPM2,
  r2.ARO AS ARO2,
  ISNULL(r1.CarCnt,0) + ISNULL(r2.CarCnt,0) AS CarsTotal,
  (r2.CarCnt/r1.CarCnt-1)*100 AS [y/y change in %]
FROM StoreInfo st
LEFT JOIN (
   SELECT
     A.store_UIN,
     SUM(total-tax) AS [Sales],
     COUNT(RO_NO) as [CarCnt],
     (((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS)) - (SUM(P_COST + T_COST))) / (SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS))) as [GPM],
     ((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET + SUPPLIES - DISP - DISL -DISS)) / (COUNT(RO_NO))) as [ARO]
  FROM HRO as A
 WHERE ( PAY_DATE >= @begJanPrev) And (PAY_DATE <= @endJanPrev) and Status IN ('C','Q')  
 GROUP BY A.store_UIN
  ) r1 ON r1.store_UIN = st.store_UIN
LEFT JOIN (
   SELECT
     A.store_UIN,
     SUM(total-tax) AS [Sales],
     COUNT(RO_NO) as [CarCnt],
     (((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS)) - (SUM(P_COST + T_COST))) / (SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS))) as [GPM],
     ((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET + SUPPLIES - DISP - DISL -DISS)) / (COUNT(RO_NO))) as [ARO]
  FROM HRO as A
 WHERE ( PAY_DATE >= @begJan) And (PAY_DATE <= @endJan) and Status IN ('C','Q')  
 GROUP BY A.store_UIN
  ) r2 ON r2.store_UIN = st.store_UIN
order by st.store_Num, st.store_UIN asc

Open in new window

1
sj77Author Commented:
Thank you so much!
0
sj77Author Commented:
Hello Pcelba -

Thank you for updating the code and assisting. I wanted to ask why this is happening. I cannot seem to figure out what causes it.

I have played around with what you told me to do to get math operations to work and specifically get percentages. For instance y/y change for sales. However, when I do y/y change it gives me a zero for all results. To test I subtracted r2.CarCnt - r1.CarCnt as [Increase] and it displayed correctly, but, when I did the y/y formula which is (r2.CarCnt - r1.CarCnt) / (r1.CarCnt) I get all zeros. Any ideas why? I have pasted the code below.

DECLARE @begJan DATETIME, @endJan DATETIME, @begJanPrev DATETIME, @endJanPrev DATETIME
SET @begJan = '01/01/2016'
SET @endJan = '01/31/2016'
SET @begJanPrev = '01/01/2015'
SET @endJanPrev = '01/31/2015'

SELECT 
  st.store_Num AS [Store Num],
  st.store_UIN as [Store Code],
  st.store_Name as [Store Name],
  r1.Sales AS [Sales PY], 
  r1.CarCnt AS [Car Count PY],
  r1.GPM AS GPM1,
  r1.ARO AS ARO1,
  r2.Sales AS [Sales CYTD], 
  r2.CarCnt AS [Car Count CYTD],
  r2.GPM AS GPM2,
  r2.ARO AS ARO2,
  ISNULL(r2.Sales,0) - ISNULL(r1.Sales,0) AS [SalesTotal],
  FORMAT(((r2.Sales - r1.Sales)/ (r1.Sales)), 'P', 'en-us') AS [y/y change in % Sales],
  ISNULL(r2.CarCnt,0) - ISNULL(r1.CarCnt,0) AS [CarsTotal],
  FORMAT(((r2.CarCnt - r1.CarCnt)/ (r1.CarCnt)), 'P', 'en-us') AS [y/y change in % Cars]
FROM StoreInfo st
LEFT JOIN (
   SELECT
     A.store_UIN,
     SUM(total-tax) AS [Sales],
     COUNT(RO_NO) as [CarCnt],
     (((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS)) - (SUM(P_COST + T_COST))) / (SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS))) as [GPM],
     ((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET + SUPPLIES - DISP - DISL -DISS)) / (COUNT(RO_NO))) as [ARO]
  FROM HRO as A
 WHERE ( PAY_DATE >= @begJanPrev) And (PAY_DATE <= @endJanPrev) and Status IN ('C','Q')  
 GROUP BY A.store_UIN
  ) r1 ON r1.store_UIN = st.store_UIN
LEFT JOIN (
   SELECT
     A.store_UIN,
     SUM(total-tax) AS [Sales],
     COUNT(RO_NO) as [CarCnt],
     (((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS)) - (SUM(P_COST + T_COST))) / (SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET - DISP - DISL -DISS))) as [GPM],
     ((SUM(TAX_PARTS + NTX_PARTS + TAX_LABOR + NTX_LABOR + TAX_SUBLET + NTX_SUBLET + SUPPLIES - DISP - DISL -DISS)) / (COUNT(RO_NO))) as [ARO]
  FROM HRO as A
 WHERE ( PAY_DATE >= @begJan) And (PAY_DATE <= @endJan) and Status IN ('C','Q')  
 GROUP BY A.store_UIN
  ) r2 ON r2.store_UIN = st.store_UIN
order by st.store_Num, st.store_UIN asc

Open in new window

0
pcelbaCommented:
Sorry, mi mistake. I should tell something about data types.
When you divide two integer numbers (and the car count is integer) the result is also integer and fraction parts are stripped out.

To get what you need you have to convert integer numbers into some numeric data type which can contain decimal places, e.g. NUMERIC.

So instead of:
(r2.CarCnt - r1.CarCnt) / (r1.CarCnt)
convert one of the operands to NUMERIC:
(r2.CarCnt - r1.CarCnt) / CAST(r1.CarCnt AS NUMERIC(10))
and it should provide decimal places in the result.

Note the NUMERIC does not need to be defined with decimal places but you may specify NUMERIC(15,5) etc.

You may also test various expressions in SQL Server Management studio. Simply execute

SELECT 3/2, 2/3, 2/CAST(3 as NUMERIC(5))

in the query window
1
sj77Author Commented:
You my friend are a GENIUS!

Thank you so much for the teaching point. Once again, I greatly appreciate it!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.