Noman Arain
Noman Arain

Reputation: 1162

Oracle SQL - GROUP BY not working

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

Upvotes: 0

Views: 141

Answers (1)

dnoeth
dnoeth

Reputation: 60462

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;

Upvotes: 1

Related Questions