Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to get the total number of occurrences based on the value of a column.

1004278Apr 19 2013 — edited Apr 22 2013
Hello everyone,

This is the first time that I will ask question here on your forum but has been following several threads ever since. I guess that now is my turn to ask a question. So anyway here is the thing, I have a query that should return count the number of rows depending on the value of SLOT. Something like this:

-----
WIPDATAVALUE          SLOT             N            M
1-2                   TRALTEST43S1     1            3
1-2                   TRALTEST43S1     2            3
3                     TRALTEST43S1     3            3
4-6                   TRALTEST43S2     1            4
4-6                   TRALTEST43S2     2            4
4-6                   TRALTEST43S2     3            4
7                     TRALTEST43S2     4            4
-----

As you can see above, on the SLOT TRALTEST43S1, there are three occurrences so M (Total number of occurrences) should be three and that column N should count it. Same goes with the SLOT TRALTEST43S2. This is the query that I have so far:
SELECT DISTINCT
WIPDATAVALUE, SLOT
, LEVEL AS n
, m 
FROM
(
  SELECT
    WIPDATAVALUE
    , SLOT
    , (dulo - una) + 1 AS m 
  FROM
  (
    SELECT 
      WIPDATAVALUE
      , SLOT
      , CASE WHEN INSTR(wipdatavalue, '-') = 0 THEN wipdatavalue ELSE SUBSTR(wipdatavalue, 1, INSTR(wipdatavalue, '-')-1) END AS una
      , CASE WHEN INSTR(wipdatavalue, '-') = 0 THEN wipdatavalue ELSE SUBSTR(wipdatavalue, INSTR(wipdatavalue, '-') + 1) END AS dulo
    FROM trprinting
    WHERE (containername = :lotID OR SLOT= :lotID) AND WIPDATAVALUE LIKE :wip
  )
) CONNECT BY LEVEL <= m
ORDER BY wipdatavalue;
And that it results to something like this:
-----
WIPDATAVALUE          SLOT             N            M
1-2                   TRALTEST43S1     1            2
1-2                   TRALTEST43S1     2            2
3                     TRALTEST43S1     1            1
4-6                   TRALTEST43S2     1            3
4-6                   TRALTEST43S2     2            3
4-6                   TRALTEST43S2     3            3
7                     TRALTEST43S2     1            1
-----

I think that my current query is basing its M and N results on WIPDATAVALUE and not the SLOT that is why I get the wrong output. I have also tried to use the WITH Statement and it works well but unfortunately, our system cant accept subquery factoring.

I know you guys will be helping out because you are all awesome. Thanks everyone

Edited by: 1001275 on Apr 19, 2013 8:07 PM

Edited by: 1001275 on Apr 19, 2013 8:18 PM
This post has been answered by Frank Kulash on Apr 20 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 20 2013
Added on Apr 19 2013
23 comments
3,075 views