I'm not sure that the query you provided is doing what you think, unless the Start Date and End Date passed in are based on the data frequency meaning when you pass in Weekly as DateFrequency the start date and end date passed in is one week.
It definitely would be easier if you provided some sample data and the results you'd expect from the sample data.
Based on the information we have I'd think something along the lines of making the summing query a CTE, then creating a 2nd CTE based on that data that adds a row_number() to determine which is the last value and then a final select that selects where rowNo = 1. Something like this:
WITH summedData
AS (
SELECT
@DateFrequency AS [Date],
SUM(CONVERT(INT, [SATX Closed on First Contact (COFC) Footprints])) AS "SATX Closed on First Contact (COFC) Footprints",
SUM(CONVERT(INT, [SATX Total New Tickets])) AS "SATX Total New Tickets",
SUM(CONVERT(INT, [SATX Phone])) AS "SATX Phone",
SUM(CONVERT(INT, [SATX Email])) AS "SATX Email",
SUM(CONVERT(INT, [SATX Chat])) AS "SATX Chat",
SUM(CONVERT(INT, [SATX Customer Portal])) AS "SATX Customer Portal",
SUM(CONVERT(INT, [SATX New Tickets (H9)])) AS "SATX New Tickets (H9)",
SUM(CONVERT(INT, [SATX New Tickets (Non-H9)])) AS "SATX New Tickets (Non-H9)",
SUM(CONVERT(INT, [SATX Total Tickets Closed])) AS "SATX Total Tickets Closed",
SUM(CONVERT(INT, [SATX Closed Tickets (H9)])) AS "SATX Closed Tickets (H9)",
SUM(CONVERT(INT, [SATX Closed Tickets (Non-H9)])) AS "SATX Closed Tickets (Non-H9)",
MIN(dbo.ufnSATXGetLastRecord([SATX Total Incident Tickets Outstanding])) AS "SATX Total Incident Tickets Outstanding",
SUM(CONVERT(INT, [SATX Incident Tickets Outstanding (H9)])) AS "SATX Incident Tickets Outstanding (H9)"
FROM
SATXFPMetrics
WHERE
[Date] >= @StartDate AND
[Date] <= @EndDate
),
addRowNo
AS (
SELECT
ROW_NUMBER() OVER (ORDER BY [Date] DESC) AS rowNo,
*
FROM
summedData
)
SELECT
*
FROM
addRowNo
WHERE
rowNo = 1
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question