I have a query:
Which only returns values that fit one of the above groups. If there is no data, the group doesn't show. I then modified the query to include a temp table:
I am having trouble creating a proper join to show all 5 groups and the associated data.
Can anyone help?
Thanks!
Ed
Code:
SELECT O.Range, COUNT(*) AS NumOfOpportunities, CAST (SUM(O.ExpectedAmount) AS MONEY) AS ExpectedAmount
FROM (
SELECT CASE
WHEN DATEDIFF(D, EstimatedCloseDate, GETDATE()) BETWEEN 0 AND 30 THEN '0-30 Days'
WHEN DATEDIFF(D, EstimatedCloseDate, GETDATE()) BETWEEN 31 AND 60 THEN '31-60 Days'
WHEN DATEDIFF(D, EstimatedCloseDate, GETDATE()) BETWEEN 61 AND 90 THEN '61-90 Days'
WHEN DATEDIFF(D, EstimatedCloseDate, GETDATE()) > 91 THEN 'Over 90 Days'
ELSE 'No Expected Close Date'
END AS Range, ExpectedAmount FROM tblOpportunities) O
GROUP BY O.Range
Which only returns values that fit one of the above groups. If there is no data, the group doesn't show. I then modified the query to include a temp table:
Code:
CREATE TABLE #TT_TOD_OPPTEMP ( Range Char(50))
INSERT #TT_TOD_OPPTEMP
VALUES ('No Expected Close Date'), ('0-30 Days'), ('31-60 Days'), ('61-90 Days'), ('Over 90 Days')
SELECT O.Range, COUNT(*) AS NumOfOpportunities, CAST (SUM(O.ExpectedAmount) AS MONEY) AS ExpectedAmount
FROM (
SELECT CASE
WHEN DATEDIFF(D, EstimatedCloseDate, GETDATE()) BETWEEN 0 AND 30 THEN '0-30 Days'
WHEN DATEDIFF(D, EstimatedCloseDate, GETDATE()) BETWEEN 31 AND 60 THEN '31-60 Days'
WHEN DATEDIFF(D, EstimatedCloseDate, GETDATE()) BETWEEN 61 AND 90 THEN '61-90 Days'
WHEN DATEDIFF(D, EstimatedCloseDate, GETDATE()) > 91 THEN 'Over 90 Days'
ELSE 'No Expected Close Date'
END AS Range, ExpectedAmount FROM tblOpportunities) O
GROUP BY O.Range
DROP TABLE #TT_TOD_OPPTEMP
I am having trouble creating a proper join to show all 5 groups and the associated data.
Can anyone help?
Thanks!
Ed