Solved

Can you do a count() in a table without grouping other columns

Posted on 2014-09-11
3
396 Views
Last Modified: 2014-09-11
Basically what I am trying to do is get a table with all my cables, the number of conductors the cable is supposed to have, and the count of the cables in the table... To further illustrate here is the query I have

  SELECT CONDUCTOR.CABLE, TYPE_CODE.CONDUCTORS
    FROM CONDUCTOR, CABLE, TYPE_CODE
   WHERE CONDUCTOR.CABLE_ID = CABLE.CABLE_ID AND CABLE.ID = TYPE_CODE.ID
ORDER BY CABLE ASC;

The resulting table:
Cable      Conductors
1000A      3
1000A      3
1000B      3
1000B      3
1000B      3
1000C      3

what I want is this:
Cable      Conductors      Count(Cable)
1000A            3                     2
1000A            3                     2
1000B            3                     3
1000B            3                     3
1000B            3                     3
1000C            3                     1

Is this possible to do?
0
Comment
Question by:imonfireDAMMIT
3 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 40317588
Try this (untested, just typed in):
 SELECT CONDUCTOR.CABLE, TYPE_CODE.CONDUCTORS,count(CONDUCTOR.CABLE) over(partition by CONDUCTOR.CABLE order by CONDUCTOR.CABLE) cable_count
    FROM CONDUCTOR, CABLE, TYPE_CODE
   WHERE CONDUCTOR.CABLE_ID = CABLE.CABLE_ID AND CABLE.ID = TYPE_CODE.ID
ORDER BY CABLE ASC;
0
 
LVL 1

Author Closing Comment

by:imonfireDAMMIT
ID: 40317605
Thanks. This worked perfectly.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 40317667
the order by in the OVER() clause isn't necessary unless you are doing a rolling count
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question