https://www.growthbook.io/ logo
#ask-questions
Title
# ask-questions
w

wide-jordan-14758

11/28/2023, 12:39 AM
Hey there, we are looking to dynamically choose a column in the table used to define our metrics based on the value in another table. We could hard code which column to choose based on the value like in the example below, but I’m wondering if there is a way to do this where we don’t have to specify
user_id_1
explicitly, e.g. whatever the value is for
test_meta_data.test_user_id
it will be used as the column name to select in
test_clickstream
. Has anyone encountered this type of dynamic column selection? (cc @ambitious-apartment-58735 @orange-magician-36994)
Copy code
select
case when test_meta_data.test_user_id = 'user_id_1' then USER_ID_1 
else USER_ID_2 end as USER_ID,
timestamp,
pageview as value
from test_clickstream
join 
(select test_user_id
from test_meta_data
where experiment_id = '{{ experimentId }}') test_meta_data
b

brief-honey-45610

11/28/2023, 2:07 AM
Hi Juliana, thanks for reaching out! It's likely you won't hear from us until we're back within business hours tomorrow. We'll follow up soon
h

helpful-application-7107

11/28/2023, 2:47 AM
Hmmm, I don't think so. I'm sure there's some way to do this in SQL but I doubt it can be used in the context of a CTE (which is how we inject your metric definition code into the final analysis SQL). Would definitely be curious if you found a way to do this that didn't rely on multiple statements.
w

wide-jordan-14758

11/28/2023, 5:48 PM
Got it, that’s more or less what we concluded too. I’ll follow up if we find another solution!
👍🏻 1
thankyou 1
3 Views