Quote (carteblanche @ Feb 26 2015 09:27pm)
im not entirely sure what you're asking for, so i took a guess. i think your problem is that after you union everything together, you no longer know which SELECT statement it came from, right? that's all it's doing.
when you say 20 columns...you're not really creating 20 columns right? it's just 2 columns (3 with my suggestion) and you will have 20 SELECT statements, right?
the order by is just to combine all the rows with the corresponding select. eg:
select * from (
SELECT
'response_1_c' as key
,response_1__c
, SUM(1)
FROM
sf_survey__c a
WHERE
survey_id__c = 1658015
GROUP BY 1, 2
UNION
SELECT
'response_2_c' as key
,response_2__c
, SUM(1)
FROM
sf_survey__c a
WHERE
survey_id__c = 1658015
GROUP BY 1, 2
) order by key
personally, i would consider calling this from non-sql instead of all the unions. but if it has to be done via sql, that's the way to do it.
that makes perfect sense. that's a great explanation and I completely understand. The main issue is the data and schema within the table that makes this such a pain in the ass. But with your query, it's 20 queries with easy to read data that i can export and read right away. I'm trying to build a query template because all surveys are going to come in under the same template. As I venture further and further in sql, i'd love to be able to reach out to you from time to time for advice. Would that be ok?
I'm trying to think what the best layout for this would be as my next step is to join in another table with customer properties and attributes to actually analyze the data.. if i were to do that I would just add on to that existing query ?
This post was edited by noflexzone on Feb 26 2015 07:43pm