0

When I am run the below query, I get the following result.

I don't understand why I am not getting the expected result from the GROUP BY.

How can I fix this so I get the expected result?

SELECT
  status AS "ROW LABELS",
  (case when order = 'INTERNET' THEN COUNT(*) end) AS "INTERNET"
FROM
    order
    ,status
WHERE  order       =   status
GROUP BY status, order_source
order by 1;

Data is here https://drive.google.com/file/d/0BzWMxMDCgXFaSk5TTWV2SEEzTFU/edit?usp=sharing I couldn't format the data here well.

Following Dnoeth's answer, I am getting the below result: enter image description here See, I need the row labels to be grouped up, but they're not being grouped. Putting count outside the case helped a little. I have edited the query to get the idea across, the query is more complicated than what I am presenting here

3
  • What are you trying to get? Commented Jul 16, 2014 at 20:33
  • What is the expected result? Your query works so we need to know what you are after to be able to help. Commented Jul 16, 2014 at 20:49
  • That can't be your real query, because WHERE order = status will generate an error as order is a reserved word. Commented Jul 16, 2014 at 21:10

1 Answer 1

1

Assuming you have some typo (order instead of order_source) you need to move the CASE inside the COUNT:

SELECT
  status AS "ROW LABELS",
  COUNT(case when order_source = 'INTERNET' THEN 1 end) AS "INTERNET"
FROM
    order
    ,status
WHERE  order       =   status
GROUP BY status
order by 1;
Sign up to request clarification or add additional context in comments.

1 Comment

Hey thx, I've updated. I didn't notice the format until after posting. Yea just typo here, I'm not getting any errors. I've uploaded an image showing what I mean.

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.