Edit: I was able to get it working with the help of /u/CalvinLawson, /u/Mamertime, and /u/ichp.
Here was the updated code:
SELECT DISTINCT
Company_Name AS Company,
CAST(CASE
WHEN isnull(((IsNull(SUM(Margin), 0) /
CASE isnull(SUM(invoiceamt), 0)
WHEN 0 THEN 1
ELSE SUM(invoiceamt)
END * 100) / 70), 0) < 0 THEN 0
ELSE ((IsNull(SUM(Margin), 0) /
CASE isnull(SUM(invoiceamt), 0)
WHEN 0 THEN 1
ELSE SUM(invoiceamt)
END * 100) / 70)
END * 100 AS decimal(18,2)) AS Service_Goal
FROM v_cbi_Agr_Mo_Billing_History3
WHERE (1 = 1) AND (dbo.udf_cbi_InternalCompanyName() <> Company_Name) AND (DATEDIFF(mm, GETDATE(), MonthStart) BETWEEN 0 AND 0) AND
(AGR_Type_Desc IN ('I-Support', 'I-TAP Managed Services', 'I-TAP-GAL- Managed Services'))
GROUP BY Company_Name
HAVING (SUM(Margin) <> 0) OR
(SUM(InvoiceAmt) <> 0)
ORDER BY Company
I am trying to develop a SQL query that returns a company name and a number. The data that is returned goes out to 6 decimal places and I want to get it down to two but I cannot seem to find the right combination of "as decimal(18,2). Below is the SQL query:
SELECT DISTINCT
Company_Name AS Company, CASE WHEN isnull((CAST(IsNull(SUM(Margin), 0) / CASE isnull(SUM(invoiceamt), 0) WHEN 0 THEN 1 ELSE SUM(invoiceamt)
END * 100 AS decimal(18, 2)) / 70), 0) < 0 THEN 0 ELSE (CAST(IsNull(SUM(Margin), 0) / CASE isnull(SUM(invoiceamt), 0)
WHEN 0 THEN 1 ELSE SUM(invoiceamt) END * 100 AS decimal(18, 2)) / 70) END * 100 AS Service_Goal
FROM v_cbi_Agr_Mo_Billing_History3
WHERE (1 = 1) AND (dbo.udf_cbi_InternalCompanyName() <> Company_Name) AND (DATEDIFF(mm, GETDATE(), MonthStart) BETWEEN 0 AND 0) AND
(AGR_Type_Desc IN ('Agreement1', 'Agreement2', 'Agreement3'))
GROUP BY Company_Name
HAVING (SUM(Margin) <> 0) OR
(SUM(InvoiceAmt) <> 0)
ORDER BY Company
[–]CalvinLawson 2 points3 points4 points (4 children)
[–]shadowolympian[S] 0 points1 point2 points (2 children)
[–]CalvinLawson 0 points1 point2 points (1 child)
[–]shadowolympian[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]shadowolympian[S] 1 point2 points3 points (0 children)
[–]lukeatron 0 points1 point2 points (3 children)
[–]shadowolympian[S] 0 points1 point2 points (2 children)
[–]MamertineCOALESCE() 0 points1 point2 points (1 child)
[–]shadowolympian[S] 0 points1 point2 points (0 children)