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 8691143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:03:56+00:00 2026-06-13T00:03:56+00:00

I have a database table containing two costs. I want to find the distinct

  • 0

I have a database table containing two costs. I want to find the distinct costs over these two columns. I also want to find the count that these costs appear. The table may look like

|id|cost1|cost2|
|1 |50   |60   |
|2 |20   |50   |
|3 |50   |70   |
|4 |20   |30   |
|5 |50   |60   |

In this case I want a result that is distinct over both columns and count the number of times that appears. So the result I would like is

|distinctCost|count|
|20          |2    |
|30          |1    |
|50          |4    |
|60          |2    |
|70          |1    |

and ideally ordered

|disctinCost1|count|
|50          |4    |
|60          |2    |
|20          |2    |
|70          |1    |
|30          |1    |

I can get the distinct over two columns by doing something like

select DISTINCT c FROM (SELECT cost1 AS c FROM my_costs UNION SELECT cost2 AS c FROM my_costs);

and I can get the count for each column by doing

select cost1, count(*)
from my_costs 
group by cost1
order by count(*) desc;

My problem is how can I get the count for both columns? I am stuck on how to do the count over each individual column and then add it up.

Any pointers would be appreciated.

I am using Oracle DB.

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-06-13T00:03:57+00:00Added an answer on June 13, 2026 at 12:03 am

    By combining your two queries..

    select cost, count(*)
    from
    (
        SELECT id, cost1 AS cost FROM my_costs 
        UNION ALL
        SELECT id, cost2 AS c FROM my_costs
    ) v
    group by cost
    order by count(*) desc;
    

    (If when a row has cost1 and cost2 equal, you want to count it once not twice, change the union all to a union)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in an Openoffice Database, containing two date columns. I'm trying
I have a java class containing all the columns of a database table as
I have a database table with some rows that I want to fetch using
I have a database with a table, containing two integer fields. When I try
If I have a database table containing a flag that can have multiple states,
I have a staff database table containing staff members, with user_no and user_name columns.
I have a database table containing dates (`date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00').
I have a database table containing the news on my site. It's divided into
In my database I have table AnimalDetails containing animalNumber(primary key),name,breed dateofbirth and an image
I have a database containing a single huge table. At the moment a query

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.