Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to put a column header on case statement in an Oracle SQL

Below is the Syntax used

WITH total_orders_1996 AS (SELECT
    customers.customer_id,
    customers.company_name,
    SUM(order_details.unit_price *  order_details.quantity) AS totlal_orders
FROM
    customers
JOIN orders ON 
    customers.customer_id = orders.customer_id
JOIN order_details ON
    orders.order_id = order_details.order_id
WHERE
    orders.order_date >= '1996=7-01-01' and orders.order_date < '1998-01-01'
    
GROUP BY
    customers.customer_id,
    customers.company_name)


Select  
customer_id ,
company_name, 
totlal_orders, 

Case 
            when totlal_orders between 0 and 1000 then 'Low'             
            when totlal_orders between 1001 and 5000 then 'Medium'             
            when totlal_orders between 5001 and 10000 then 'High' 
            when totlal_orders > 10000 then 'Very High'         
       
End  from total_orders_1996
Order by customer_id ;

And I get an output Header for the case statement as shown below

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How could I get the case header as shown below
enter image description here

>Solution :

All you need to do is add a column alias at the end of your case statement

  SELECT customer_id,
         company_name,
         totlal_orders,
         CASE
             WHEN totlal_orders BETWEEN 0 AND 1000 THEN 'Low'
             WHEN totlal_orders BETWEEN 1001 AND 5000 THEN 'Medium'
             WHEN totlal_orders BETWEEN 5001 AND 10000 THEN 'High'
             WHEN totlal_orders > 10000 THEN 'Very High'
         END as customer_group
    FROM total_orders_1996
ORDER BY customer_id;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading