问题
How would one go about gathering information from one column in an SQL database(specific cell) that has double or triple values stored in it, like:
row_number column1 1 A,B 2 B,D *Note: the above table is not true, just an example
and place it in a column with the "AS" attribute according to a rank over to another column after you have split column1?
The situation I have is as follows:
I am able to split and do the above with case statements, but I am only able to get value "B" after I split it and placed it in the AS Score column according to the rank over of column called ElIndex. Code as follows:
SQLStr1 = "DECLARE @ScoreA VARCHAR(100), @ScoreB VARCHAR(100), @ScoreC VARCHAR (100), @ScoreD VARCHAR (100), @ScoreE VARCHAR (100), @ScoreF VARCHAR (100), @ScoreG VARCHAR (100) SET @ScoreA = 1 SET @ScoreB = 2 SET @ScoreC = 3 SET @ScoreD = 4 SET @ScoreE = 5 SET @ScoreF = 6 SET @ScoreG = 7"
SQLStr1 = SQLStr1 & " SELECT DISTINCT cs.ModuleName, cs.ScreenName, se.AnswerScore, cs.CourseName, tf.FileText, tf.VoiceFile, se.ElIndex"
if rsUserModules1("LinkType") = "Submit Answer Multi" then
readyList = Split(rsUserModules1("LinkAction"),",")
For i = 0 TO UBound(readyList) -1
if readyList(i) = "A" then
SQLStr1 = SQLStr1 & " ,(CASE WHEN se.LinkAction = 'A' THEN @ScoreA "
SQLStr1 = SQLStr1 & " WHEN @ScoreA = RANK()OVER(ORDER BY se.ElIndex) THEN '-1' ELSE '0' END) AS Score"
end if
if readyList(i) = "B" then
SQLStr1 = SQLStr1 & " ,(CASE WHEN se.LinkAction = 'B' THEN @ScoreB"
SQLStr1 = SQLStr1 & " WHEN @ScoreB = RANK()OVER(ORDER BY se.ElIndex) THEN '-1' ELSE '0' END) AS Score"
end if
if readyList(i) = "C" then
SQLStr1 = SQLStr1 & " ,(CASE WHEN se.LinkAction = 'C' THEN @ScoreC"
SQLStr1 = SQLStr1 & " WHEN @ScoreC = RANK()OVER(ORDER BY se.ElIndex) THEN '-1' ELSE '0' END) AS Score"
end if
if readyList(i) = "D" then
SQLStr1 = SQLStr1 & " ,(CASE WHEN se.LinkAction = 'D' THEN @ScoreD"
SQLStr1 = SQLStr1 & " WHEN @ScoreD = RANK()OVER(ORDER BY se.ElIndex) THEN '-1' ELSE '0' END) AS Score"
end if
if readyList(i) = "E" then
SQLStr1 = SQLStr1 & " ,(CASE WHEN se.LinkAction = 'E' THEN @ScoreE"
SQLStr1 = SQLStr1 & " WHEN @ScoreE = RANK()OVER(ORDER BY se.ElIndex) THEN '-1' ELSE '0' END) AS Score"
end if
next
end if
SQLStr1 = SQLStr1 & " FROM GroupScreens cs WITH (NOLOCK)"
SQLStr1 = SQLStr1 & " INNER JOIN TextFiles tf WITH (NOLOCK)"
SQLStr1 = SQLStr1 & " ON cs.ScreenName = tf.ScreenName"
SQLStr1 = SQLStr1 & " INNER JOIN Screens s WITH (NOLOCK)"
SQLStr1 = SQLStr1 & " ON s.ScreenName = tf.ScreenName"
SQLStr1 = SQLStr1 & " INNER JOIN ScreenElements se WITH (NOLOCK)"
SQLStr1 = SQLStr1 & " ON se.CourseName = cs.CourseName AND tf.SEUID = se.UID"
SQLStr1 = SQLStr1 & " WHERE tf.CourseName= '" & Name1 & "' AND cs.ModuleName= '" & Name & "' AND (s.ScreenType = 'A' AND ( [FileText] NOT LIKE '%?%' AND [FileText] NOT LIKE '%...%' AND [FileText] NOT LIKE '%True or False%' AND [FileText] NOT LIKE '%Name%' AND [FileText] NOT LIKE '%There may be%' AND [FileText] NOT LIKE '%Select%' AND [FileText] NOT LIKE '%[(]%' AND [FileText] NOT LIKE ''))AND cs.ScreenName ='" & ScreenName1 & "'"
SQLStr1 = SQLStr1 & " ORDER BY se.ElIndex"
The problem I am having is in the image below:
来源:https://stackoverflow.com/questions/27374501/how-would-one-go-about-gathering-information-from-one-column-in-an-sql-database