You're economicla with words, with description of your issue... So, i can only guess that you need to replace
ROW_NUMBER()
with another
Ranking Functions (T-SQL)[
^], for example
DENSE_RANK()[
^] function.
DENSE_RANK()
returns the rank of rows within the partition of a result set, without any gaps in the ranking; the rank of a row is one plus the number of distinct ranks that come before the row in question.
( CASE WHEN (DENSE_RANK() OVER (PARTITION BY LIstAllStages_Backup3.productserial ORDER BY LIstAllStages_Backup3.SymptomWeight ASC)) = 1 THEN 'Y' ELSE 'N' END 'Primary'
[EDIT #1]
I have done something this:
DECLARE @Receiving Table (id INT, serial INT, Workcompleted DATETIME, symptom VARCHAR(30), SymptomWeight INT)
INSERT INTO @Receiving (id, serial, Workcompleted, symptom, SymptomWeight)
VALUES(1, 100, '2013-03-01', NULL, 999)
INSERT INTO @Receiving (id, serial, Workcompleted, symptom, SymptomWeight)
VALUES(2, 100, '2013-04-11', NULL, 999)
DECLARE @Dispatch TABLE (id INT, serial INT, Workcompleted DATETIME, symptom VARCHAR(30), SymptomWeight INT)
INSERT INTO @Dispatch (id, serial, Workcompleted, symptom, SymptomWeight)
VALUES(1, 100, '2013-03-12', NULL, 999)
INSERT INTO @Dispatch(id, serial, Workcompleted, symptom, SymptomWeight)
VALUES(2, 100, '2013-04-15' ,NULL, 999)
DECLARE @Table1 TABLE (id INT, serial INT, Workcompleted DATETIME, symptom VARCHAR(30), SymptomWeight INT)
INSERT INTO @Table1 (id, serial, Workcompleted, symptom, SymptomWeight)
VALUES(1, 100, '2013-03-03', 'NFF', 900)
INSERT INTO @Table1(id, serial, Workcompleted, symptom, SymptomWeight)
VALUES(3, 100, '2013-04-11', 'NFD', 850)
DECLARE @Table2 TABLE (id INT, serial INT, Workcompleted DATETIME, symptom VARCHAR(30), SymptomWeight INT)
INSERT INTO @Table2 (id, serial, Workcompleted, symptom, SymptomWeight)
VALUES(1, 100, '2013-03-04', 'NFD', 850)
INSERT INTO @Table2(id, serial, Workcompleted, symptom, SymptomWeight)
VALUES(5, 100, '2013-04-12', 'NFC', 800)
DECLARE @VwListallstages TABLE (id INT, serial INT, Workcompleted DATETIME, symptom VARCHAR(30), SymptomWeight INT, stage INT)
INSERT INTO @VwListallstages (id, serial, Workcompleted, symptom, SymptomWeight, stage)
SELECT *
FROM (
SELECT *, 1 AS stage
FROM @Receiving
UNION ALL
SELECT *, 4 AS stage
FROM @Dispatch
UNION ALL
SELECT *, 2 AS stage
FROM @Table1
UNION ALL
SELECT *, 3 AS stage
FROM @Table2
) AS T
SELECT *
FROM (
SELECT t1.*, CASE WHEN t2.[Primary] IS NULL THEN 'N' ELSE 'Y' END AS [Primary]
FROM @VwListallstages AS t1 LEFT JOIN (
SELECT serial, MONTH(WorkCompleted) AS MonthOfWC, MAX(WorkCompleted) AS WorkCompleted, MAX(stage) AS [Primary]
FROM @VwListallstages
WHERE symptom IS NOT NULL
GROUP BY serial, MONTH(WorkCompleted)
) AS t2 ON t1.serial = t2.serial AND t1.WorkCompleted = t2.WorkCompleted
) AS T
ORDER BY id, stage
My results:
1 100 2013-03-01 00:00:00.000 NULL 999 1 N
1 100 2013-03-03 00:00:00.000 NFF 900 2 N
1 100 2013-03-04 00:00:00.000 NFD 850 3 Y
1 100 2013-03-12 00:00:00.000 NULL 999 4 N
2 100 2013-04-11 00:00:00.000 NULL 999 1 N
2 100 2013-04-15 00:00:00.000 NULL 999 4 N
3 100 2013-04-11 00:00:00.000 NFD 850 2 N
5 100 2013-04-12 00:00:00.000 NFC 800 3 Y
[/EDIT]