Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6242567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:58:44+00:00 2026-05-24T11:58:44+00:00

I have a table structure that looks like this: Columns: Account Number Service Code

  • 0

I have a table structure that looks like this:

Columns:
Account Number
Service Code
Quantity

Accounts can have multiple service codes on separate rows. For example, for this account ‘1868603’, their service codes are stored like this:

ACCOUNT_NUMBER  SERVICE_CODE    SERVICE_QUANTITY
1868603     101     1
1868603     101     1
1868603     101     1
1868603     101     1
1868603     101     1
1868603     101     1
1868603     326     1
1868603     326     1
1868603     327     1
1868603     327     1
1868603     328     1
1868603     328     1

I am writing a query which need to see if specific service codes are on their account. I am not concerned about the quantity. I just want to see if they exist for an account.

This is what I have so far:

SELECT ACCOUNT_NUMBER, COUNT(SERVICE_CODE) HOW_MANY
FROM (
    SELECT DISTINCT ACCOUNT_NUMBER, SERVICE_CODE 
    FROM CUSTOMER_SERVICES
    WHERE   ACCOUNT_NUMBER = 1868603 
        AND     SERVICE_CODE IN('323','326','327','328')
)
GROUP BY ACCOUNT_NUMBER

Which gives me:

ACCOUNT_NUMBER  HOW_MANY
1868603 3

3 is the correct number, because there are three instances of IN(‘323′,’326′,’327′,’328’), which is what I’m trying to determine.

Is there a way to simplify this query without subsetting the data first?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:58:46+00:00Added an answer on May 24, 2026 at 11:58 am

    Can ORACLE do COUNT(DISTINCT <field>)?

    SELECT
      ACCOUNT_NUMBER, COUNT(DISTINCT SERVICE_CODE) HOW_MANY
    FROM
      CUSTOMER_SERVICES
    WHERE
      ACCOUNT_NUMBER = 1868603 
      AND SERVICE_CODE IN('323','326','327','328')
    GROUP BY
      ACCOUNT_NUMBER
    

    Also, note that having sub-querys does not always imply poor performance. SQL is a language for expressing the logic, but it is compiled and executed in a way you may not feel resembles the SQL very closely.

    As such windowing functions (like DENSE_RANK) may also have different performance charactertics worthy of a try…

    SELECT
      account_number,
      MAX(rank_id)
    FROM
    (
      SELECT
        account_number,
        DENSE_RANK() OVER (PARTITION BY account_number ORDER BY service_code) AS rank_id
      FROM
        customer_services
      WHERE
        account_number = 1868603 
        AND service_code IN('323','326','327','328')
    )
      AS Data
    GROUP BY
      account_number
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML Table that looks like this: The user can update any
I have a table structure that looks like: <table> <tr id=row1> <td> <div>row 1
I have a bag whose structure looks like this: <bag name=foo fetch=select table=table_of_foos> <key
This is the MySQL table structure that I have: itemID (BIGINT) userID (BIGINT) wasAdded
How would one structure a table for an entity that can have a one
Let's say my table structure looks something like this: CREATE TABLE [dbo].[table1] ( [id]
I have a database structure that has a Person table which contains fields such
I have a few classes, such as those that outline database table structure or
I have a table in SQL server that has the normal tree structure of
I have a table with a structure like the following: LocationID AccountNumber long-guid-here 12345

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.