1

I'm using an Oracle SQL database. I'm trying to combine this 2 queries into one from same table:

select count(CustomerID) from Customers
where (City = 'Berlin' or City = 'London')
from Customers;

select count(CustomerID) from Customers
where (City = 'Mannheim' or City = 'Strasbourg')
from Customers;

And my output would like to be something like exported on 2 columns:

| Berlin & London | Mannheim & Strasbourg |
|-----------------------------------------|
|        5        |         10            |

example of db here, https://www.w3schools.com/sql/trysql.asp?filename=trysql_op_in

I think the answer will be:

(select count(CustomerID) from Customers
where (City = 'Berlin' or City = 'London'))
union
(select count(CustomerID) from Customers
where (City = 'Marseille' or City = 'Tsawassen'));
union
(select count(CustomerID) from Customers
where (City = 'Strasbourg' or City = 'Madrid'));

Still testing...

Finally got the job done by using this case. Thanks to everyone involved !

SELECT
  COUNT(CASE
    WHEN  work_zone = 'AMBGI01' OR
          work_zone = 'AMBGI02' OR
          work_zone = 'AMBGI03' AND
          task_type = 'P' THEN work_zone
  END) "HighBay Putaways",
  COUNT(CASE
    WHEN  work_zone = 'AMBGI04' OR
          work_zone = 'AMBGI05' OR
          work_zone = 'AMBGI06' OR
          work_zone = 'AMBGI07' AND
          task_type = 'P' THEN work_zone
  END) "LowBay Putaways",
  COUNT(CASE
    WHEN  from_loc_id = 'THEWALL' AND
          task_type = 'P' THEN from_loc_id
    END) "THE WALL",
    COUNT(CASE
    WHEN  tag_id like '%' AND
          from_loc_id like 'EXPANS%' AND
           task_type = 'P' THEN tag_id
     END) "EXPANSION",
   COUNT(CASE
    WHEN  final_loc_id like '_______' AND
          (status = 'In Progress'        OR
          status = 'Released')          AND
          task_type = 'R' THEN final_loc_id
  END) "Replens"
FROM move_task;
3
  • is there any relational key between country and customer table? Commented Oct 18, 2018 at 8:20
  • Please edit the question to show the structure of the Country and Customers tables. Commented Oct 18, 2018 at 9:45
  • 1
    i think the asnwer will be (select count(CustomerID) from Customers where (City = 'Berlin' or City = 'London')) union (select count(CustomerID) from Customers where (City = 'Marseille' or City = 'Tsawassen')); union (select count(CustomerID) from Customers where (City = 'Strasbourg' or City = 'Madrid')); but not sure if working 100% Commented Oct 18, 2018 at 9:53

2 Answers 2

2

Easiest way is the following:

select count(CASE WHEN City = 'Berlin' or City = 'London' THEN City END) "Berlin & London"
     , count(CASE WHEN City = 'Mannheim' or City = 'Strasbourg' THEN City END) "Mannheim & Strasbourg"
  from Customers
Sign up to request clarification or add additional context in comments.

3 Comments

trying it on the link I provided, I got an error .. try it as well, maybe I am doing something wrong.. ?
The database in your link doesn't support Oracle syntax. Which database type do you use?
Oracle at work, should try this at work and then come with a feedback ;) will get back asap
2

Its not clear if you have two tables (customers, country) or one. If there are two you need to show us structure. w3schools.com says there is no such table like country and customers does not contain store column. Anyway you need conditional count, something like here:

select count(case when city in ('Berlin', 'London')       
                  then 1 end) as "Berlin & London",
       count(case when city in ('Mannheim', 'Strasbourg') 
                  then 1 end) as "Mannheim & Strasbourg"
  from Customers
  where city in ('Berlin', 'London', 'Mannheim', 'Strasbourg')

You could also use pivot, but this is simpler, more readable and works in older Oracle versions.

1 Comment

Customers table only sorry

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.