here
I am trying to divide the value of one column by another column and have the results in a new column, rounded to the the thousands place. TIA!
Here is the code I tried:
%%sql
--#WHICH 5 CASHIERS RANG UP THE MOST CUSTOMERS ON AVERAGE PER SHIFT?
WITH customers_seen AS (SELECT E.NAME,
E.EMPLOYEE_ID,
E.POSITION,
COUNT(DISTINCT T.TRANSACTION_ID) AS CUSTOMERS_SERVICED,
S.STORE_BRANCH
FROM TRANSACTIONS T
JOIN EMPLOYEES E
ON E.EMPLOYEE_ID = T.CASHIER_ID
JOIN STORES S
ON S.STORE_ID = T.STORE_ID
GROUP BY E.NAME
ORDER BY CUSTOMERS_SERVICED DESC),
number_of_shifts AS (SELECT E.NAME,
E.EMPLOYEE_ID,
COUNT(DISTINCT T.DATE) AS NUMBER_OF_SHIFTS,
E.SHIFT,
E.POSITION
FROM TRANSACTIONS T
JOIN EMPLOYEES E
ON E.EMPLOYEE_ID = T.CASHIER_ID
GROUP BY E.NAME
HAVING E.POSITION = 'cashier'
ORDER BY NUMBER_OF_SHIFTS DESC)
SELECT n.NAME,
c.CUSTOMERS_SERVICED,
n.NUMBER_OF_SHIFTS,
ROUND(printf("%.1f",c.CUSTOMERS_SERVICED/n.NUMBER_OF_SHIFTS),4) AS CUSTOMERS_SERVICED_PER_SHIFT
FROM number_of_shifts n
JOIN customers_seen c
ON n.EMPLOYEE_ID = c.EMPLOYEE_ID
ORDER BY CUSTOMERS_SERVICED_PER_SHIFT DESC
LIMIT 5;
and I get this as a result where the value was rounded: enter image description here
I believe your issue is that you are rounding the formatted (printf'ed) output and thus losing the trailing 0's.
Try just
printf('%.3f',c.CUSTOMERS_SERVICED/n.NUMBER_OF_SHIFTS))Perhaps consider the following example:-
This results in:-
and:-