?
Solved

oracle query

Posted on 2015-01-05
3
Medium Priority
?
122 Views
Last Modified: 2015-01-06
CREATE TABLE TAB1
(
  DRIVE_DATE      DATE                          NOT NULL,
  AREA_REP_NO     NUMBER(4)                     NOT NULL,
  PROCEDURE_CODE  VARCHAR2(5 BYTE)
)




Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'DR');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 15, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 15, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 15, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 15, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'DR');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'DR');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 8, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 12, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 12, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 12, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'DR');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
Insert into TAB1
   (DRIVE_DATE, AREA_REP_NO, PROCEDURE_CODE)
 Values
   (TO_DATE('01/02/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 10, 'WB');
COMMIT;

Open in new window


select drive_date,area_rep_no,
       procedure_code,count(*) as cnt
  from tab1
 group by drive_date,area_rep_no,
       procedure_code 

Open in new window


Need help in the count

If procedure_code is DR (double red)  then I need count multiplied by 2. For WB (whole blood) its 1
0
Comment
Question by:anumoses
3 Comments
 
LVL 35

Accepted Solution

by:
johnsone earned 1000 total points
ID: 40531733
I didn't test it, but this should do it:

SELECT drive_date, 
       area_rep_no, 
       procedure_code, 
       SUM(CASE 
             WHEN procedure_code = 'DR' THEN 2 
             ELSE 1 
           END) AS cnt 
FROM   tab1 
GROUP  BY drive_date, 
          area_rep_no, 
          procedure_code 

Open in new window

0
 
LVL 29

Assisted Solution

by:MikeOM_DBA
MikeOM_DBA earned 1000 total points
ID: 40531742
This should work!
  SELECT Drive_Date
       , Area_Rep_No
       , Procedure_Code
       , DECODE ( Procedure_Code, 'DR', 2, 1 ) * COUNT ( * ) AS Cnt
    FROM Tab1
GROUP BY Drive_Date, Area_Rep_No, Procedure_Code
/

Open in new window

0
 
LVL 6

Author Closing Comment

by:anumoses
ID: 40533313
thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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 setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

809 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