0

I have the following two ORACLE SQL queries which work well:

SELECT COUNT(SEG_ID) INTO totalUniqueSegments FROM CAR_RENTAL_SERVICES;

SELECT DISTINCT SEG_ID FROM CAR_RENTAL_SERVICES;

But I need to combine them. I'd like my count to consider only unique seg_id.

This is what I've tried:

SELECT COUNT(SEG_ID)INTO totalUniqueSegments FROM CAR_RENTAL_SERVICES WHERE SEG_ID IN (SELECT DISTINCT SEG_ID FROM CAR_RENTAL_SERVICES);

But I'm getting the error 'missing expression'. It should be fairly simple but I'm not very experienced. Thanks.

EDIT: this COUNT(SEG_ID)INTO was changed to this COUNT(SEG_ID) INTO (with space). Now the error is 'missing expression'

1
  • Maybe you're missing a column name for COUNT(SEG_ID) since you're putting it into another table. Commented Apr 15, 2014 at 18:17

1 Answer 1

4

Try like this,

SELECT COUNT(DISTINCT SEG_ID) FROM CAR_RENTAL_SERVICES;

Use the below query, If You want to demonstrate the same using a sub-query.

SELECT COUNT(*)
FROM(
     SELECT DISTINCT seg_id 
     FROM   car_rental_services
     );
Sign up to request clarification or add additional context in comments.

2 Comments

Your solution is great and it works. However I needed to demonstrate using a sub-query for a school exercise. Is it possible in this case or should I abandon the idea ? Thanks ! (I can mark your solution in any case)
@PrincessLilly, Please find the updated answer with sub-query.

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.