-1

I'm trying to work out the best way to perform the following:

In the database I have two fields - q1, q2.

I want to first count, order and group response for q1 - easy enough.

1. SELECT q1, COUNT(q1) FROM xyz GROUP BY q1 ORDER BY q1

Now, for arguments sake, let's assume that this brings back the following:

A = 5
B = 12
C = 3

The I need the following information via this query:

2. SELECT q2, COUNT(q2) FROM xyz WHERE q1 = A GROUP BY q2 ORDER by q2 

Which is fine but is there a way to iterate through the above in a Loop for each time the first query brings back a result, so in this example the code for 2. is iterated and looped for q1 = A, q1 = B and q1 = C?

Make sense? I'm not sure how well I have described that, so I apologise.

1 Answer 1

1

You can achieve this using single query as:

SELECT q2, COUNT(q2)
FROM xyz
GROUP BY q1, q2
ORDER by q1, q2;
Sign up to request clarification or add additional context in comments.

2 Comments

do you want separate result sets for 'A', 'B', ...? in that case you may have to write a stored procedure with CURSOR.
Yes, need the results back for A, B, C etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.