I have 7 metric names and all these metrics should ideally be updated every month but sometimes that doesn't happen. In that case I'm required to carry forward my previous month metric value, red threshold and yellow threshold. All the data comes from Excel.
The have created the following query on sql-server-
select
withnull.[Metric Name],
ISNULL(withnull.[Metric Value], withnullx.[Metric Value]) MetricValue,
ISNULL(withnull.[Red Threshold], withnullx.[Red Threshold]) Red,
ISNULL(withnull.[Yellow Threshold], withnullx.[Yellow Threshold]) Yellow,
withnull.[Date]
from
(
SELECT [Metric Value], [Red Threshold], [Yellow Threshold], Mon.[Date], Mon.[Metric Name]
FROM
(
SELECT [Metric Name], [Last Updated Date], [Metric Value], [Red Threshold], [Yellow Threshold]
FROM [QMS Scorecard].[dbo].['#1 Effectiveness (SPC)$']
UNION ALL
SELECT [Metric Name], [Last Updated Date], [Metric Value], [Red Threshold], [Yellow Threshold]
FROM [QMS Scorecard].[dbo].['#1 MRB Effectiveness (Non-Conf)$']
UNION ALL
SELECT [Metric Name], [Last Updated Date], [Metric Value], [Red Threshold], [Yellow Threshold]
FROM [QMS Scorecard].[dbo].['#1 Effectiveness(Problem Solvi)$']
UNION ALL
SELECT [Metric Name], [Last Updated Date], [Metric Value], [Red Threshold], [Yellow Threshold]
FROM [QMS Scorecard].[dbo].['Calibration Passing "As Found" $']
UNION ALL
SELECT [Metric Name], [Last Updated Date], [Metric Value], [Red Threshold], [Yellow Threshold]
FROM [QMS Scorecard].[dbo].['Change Control Malfunction Rate$']
UNION ALL
SELECT [Metric Name], [Last Updated Date], [Metric Value], [Red Threshold], [Yellow Threshold]
FROM [QMS Scorecard].[dbo].['MSA passing rate$']
UNION ALL
SELECT [Metric Name], [Last Updated Date], [Metric Value], [Red Threshold], [Yellow Threshold]
FROM [QMS Scorecard].[dbo].['Unknown Failures (FMEA & Ctrl)$']
) Data
RIGHT JOIN
(
SELECT [Date], [Metric Name]
FROM [Dates$]
--ORDER BY [Metric Name], [Date]
) Mon
ON datepart(mm, Data.[Last Updated Date]) = datepart(mm, Mon.[Date])
AND
datepart(yyyy, Data.[Last Updated Date]) = datepart(yyyy, Mon.[Date])
AND
Data.[Metric Name] = Mon.[Metric Name]
--ORDER BY [Metric Name], [Date]
) withnull
outer apply
(SELECT [Metric Value], [Red Threshold], [Yellow Threshold]
FROM (SELECT * from (SELECT *, row_number() OVER (PARTITION BY [Metric Name] ORDER BY [Date] DESC) rn from
(
SELECT [Metric Value], [Red Threshold], [Yellow Threshold], Mon.[Date], Mon.[Metric Name]
FROM
(
SELECT [Metric Name], [Last Updated Date], [Metric Value], [Red Threshold], [Yellow Threshold]
FROM [QMS Scorecard].[dbo].['#1 Effectiveness (SPC)$']
UNION ALL
SELECT [Metric Name], [Last Updated Date], [Metric Value], [Red Threshold], [Yellow Threshold]
FROM [QMS Scorecard].[dbo].['#1 MRB Effectiveness (Non-Conf)$']
UNION ALL
SELECT [Metric Name], [Last Updated Date], [Metric Value], [Red Threshold], [Yellow Threshold]
FROM [QMS Scorecard].[dbo].['#1 Effectiveness(Problem Solvi)$']
UNION ALL
SELECT [Metric Name], [Last Updated Date], [Metric Value], [Red Threshold], [Yellow Threshold]
FROM [QMS Scorecard].[dbo].['Calibration Passing "As Found" $']
UNION ALL
SELECT [Metric Name], [Last Updated Date], [Metric Value], [Red Threshold], [Yellow Threshold]
FROM [QMS Scorecard].[dbo].['Change Control Malfunction Rate$']
UNION ALL
SELECT [Metric Name], [Last Updated Date], [Metric Value], [Red Threshold], [Yellow Threshold]
FROM [QMS Scorecard].[dbo].['MSA passing rate$']
UNION ALL
SELECT [Metric Name], [Last Updated Date], [Metric Value], [Red Threshold], [Yellow Threshold]
FROM [QMS Scorecard].[dbo].['Unknown Failures (FMEA & Ctrl)$']
) Data
RIGHT JOIN
(
SELECT [Date], [Metric Name]
FROM [Dates$]
) Mon
ON datepart(mm, Data.[Last Updated Date]) = datepart(mm, Mon.[Date])
AND
datepart(yyyy, Data.[Last Updated Date]) = datepart(yyyy, Mon.[Date])
AND
Data.[Metric Name] = Mon.[Metric Name]
) b ) d
WHERE rn = 1) c
WHERE
c.[Date]<withnull.[Date] and
withnull.[Metric Value] is null and
c.[Metric Value] is not null and
c.[Red Threshold] is not null and
c.[Yellow Threshold] is not null
ORDER BY [Metric Name], [Date] DESC
) withnullx
I have joined worksheet for all the Metrics using UNION ALL, and to have all the months for each Metric Name I did a right join.
Now I have Null values (Metric Value, Red Threshold, Yellow Threshold) for months with no records for those specific metric names.
With Outer Apply, I'm replacing these NULLS with previous month values. This requires the ORDER BY in second last line of the above code.
There's another Order BY in above code. "SELECT * from (SELECT *, row_number() OVER (PARTITION BY [Metric Name] ORDER BY [Date] DESC) rn" This helps me get not just top for the complete data but top for each Metric Name.
I'm hoping if I could use this code, I will be able to replace NULL values with previous month values for each Metric Name. But I get error saying "The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified." Thank you in advance.