We return all required data from this, but the business logic requires it be in order of the string declared in our parameter.
Previously when I asked about the business rules, you said, The business rule doesn't matter as the colors need to be hardcoded in the query and return with result set. So I decided that I could use any rule I ilked, and I preferred to randomise.
But I can see there is no correlation between the status and and the Colour in our parameter.
Correct. There is not supposed to be any.
Now you are saying it be in order of the string declared in our parameter. I take this to mean that if the string is
Purple, Blue, Brown, Black, the output should be sorted so that Purple comes first. To achieve, you need a split function that returns the list position. I have one here
http://www.sommarskog.se/arrays-in-sql-2005.html#iter-list-of-strings
Change:
Create Table #Colours(ID int NOT NULL PRIMARY KEY,
Colour Varchar(50) NOT NULL)
Insert into #colours(id, colour)
Select ROW_NUMBER() over(Order By (Select newid()))-1, Value
From dbo.fn_udfSplit(@ColourSQL, ',')
to:
Create Table #Colours(pos int NOT NULL PRIMARY KEY,
Colour Varchar(50) NOT NULL)
Insert into #colours(id, colour)
Select listpos, str
From dbo.iter_charlist_to_tbl(@ColourSQL, ',')
The final query can then be changed to:
;WITH CTE AS (
Select Number, Status, ROW_NUMBER() OVER(ORDER BY newid()) as rownum
from #tempCountRichard
)
SELECT CTE.Number, CTE.Status, c.Colour
from CTE
JOIN #Colours c on CTE.rownum % (SELECT MAX(pos) FROM #colours) = c.listpos
ORDER BY c.listpos
Now all statuses that happen to be connected to the colour purple will be listed first. There is still no correlation between colours and status, but now you are saying
How can I set it so
Pass = Purple, Pass With Observation = Blue, Fail = Brown, None = Black and it doesn't change each time I excute.Thanks.
CASE Status
WHEN 'Pass' THEN 'Purple'
WHEN 'Pass with observation' THEN 'Blue'
WHEN 'Fail' THEN 'Brown'
WHEN 'None' THEN' Black'
END
Or if you want the colours to be determined by the flux of the moment, add a column for status to the Colours table, and put the statuses in the desired position.
Now, what should happen if there are unforseen status values, or few colours than statuses, I don't know.