Question

In the following I need to somehow join the fields within the case statement with the outer tables so that the fldNumb is selected for the current PK and CIA values. I am really stuck, can anyone help?

INSERT INTO #CTE (sMedNum)
        SELECT T1.sMedNum
        FROM #CTE INNER JOIN
            (SELECT
                (CASE WHEN (Charindex('.', CAST(rInd AS NVARCHAR(30))) > 0) 
                    THEN CAST(( SELECT fldNumb 
                                FROM #CTE 
                                WHERE sCtr = FLOOR(rInd)) AS FLOAT) + 
                         CAST(( SELECT fldNumb
                                FROM #CTE 
                                WHERE Ind = FLOOR(rInd+1)) AS FLOAT) / 2                     
                    ELSE CAST(( SELECT fldNumb 
                                FROM #CTE 
                                WHERE sCtr = rInd) AS FLOAT)
                END) AS sMedNum, fldPK, fldCIA
            ) T1
            ON 
            #CTE.fldPK = T2.fldPK AND
            #CTE.fldCIA = T2.fldCIA
Was it helpful?

Solution

Not having your database I cannot help you terribly much, but it looks like you need to be aliasing your tables. If you are trying to get a value from a table outside a subquery, you have to call it something different.

here is an example

  select case 
    when a1.value = 1 then (select a2.id from tableA a2 where a1.val2 = a2.val2)
    when a1.value = 2 then (select a3.id from tableA a3 where a1.val2 = a3.val2)
    when a1.value = 3 then (select a4.id from tableA a4 where a1.val2 = a4.val2)
  end as new_id
  from tableA a1

hope this makes sense, if it doesn't, then that is what questions are for ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top