Hi everyone, This isn't strictly a CFML question.....
# cfml-general
g
Hi everyone, This isn't strictly a CFML question... It might be "JUST" an SQL problem - but since I can't solve it for myself in SQL, I'm open to solutions that may need some massaging in CFML, to get it working. Basically I need to create a join between two tables, where one side of the join is a column name, not a value in a column. I have created a dbFiddle to better explain. (I don't think I need a "coded" solution. A psuedo-code / steps required - will most likely be all I need) As always - thanks in advance for any ideas you might have.
m
@gavinbaumanis just an FYI there is a SQL channel #sql for these type of questions.
👍🏼 1
l
@gavinbaumanis I gave it a shot transposing the column and taking some help from information_schema (i think this only works in sql8+)
Copy code
SELECT 
v.survey_id,
concat(
  (CASE WHEN internalName='PvFf9Bzd_response' THEN questionText ELSE '' END),
  (CASE WHEN internalName='yW3AEXOT_response' THEN questionText ELSE '' END),
  (CASE WHEN internalName='IES35zCj_response' THEN questionText ELSE '' END),
  (CASE WHEN internalName='IES35zCj_response_reason' THEN questionText ELSE '' END)
) AS question,
v.IES35zCj_response_reason

from vxml_xxxxxx v inner join ws_question ws on v.id = ws.id
where ws.internalName in (
select COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = "vxml_xxxxxx" AND COLUMN_NAME like "%response%") 
;
produces output like so:
Not sure how you want the output to become, but you could always transpose the other table instead of you're looking for another way to show the data
t
I was going to suggest using
PIVOT
, but it looks like mysql doesn't have that...
m
If you are open for it not being pure SQL ... and since I don't know your desired result: I would query for your recordset that you want to output somehow... then in your recordset i would find the columns whose names are _response then i would query for the labels for those columns and store those in a Struct _Columns["PvF...response"]="ON a scale..." then you could loop over the rows and then the columns that you want output.... i don't know how you want the output....
g
@Michael Schmidt The existing report has survey_id, date, recipientName, recipientTHIS, recipientTHAT, answer_XX1 answer_XX2, ... I have been asked to include the text of the question before the answer in the existing report. do I would have; survey_id, ..., recipientTHAT, question_XX1_text, answer_XX1, question_XX2_text, answer_XX2, ...
@Lukas Thanks for your help - but it isn't right sorry. My example was poor - sorry. I don't need to add 1 column. I need to add 1 extra column for every existing answer column. from
id, survey_id, PvFf9Bzd_response, yW3AEXOT_response, IES35zCj_response, IES35zCj_response_reason
to
id, survey_id, questionText_for_PvFf9Bzd_response, PvFf9Bzd_response, questionText_for_yW3AEXOT_response, yW3AEXOT_response, questionText_for_IES35zCj_response, IES35zCj_response, questionText_for_IES35zCj_response_reason, IES35zCj_response_reason
or a little more clearly; id, survey_id, ..., Q1_TEXT, Q1_answer, Q2_TEXT, Q2_answer Where Q1_TEXT / Q2_TEXT / etc - are the new columns I need to add.
I have recreated this question in the #sql channel - with a better / clearer explanation of the problem and what I am after. Can a moderator "close / delete" this message, please?
👍🏻 1