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!

Query to exclude/include based on availability of date

TheManWhoSoldTheWorldJun 3 2019 — edited Jun 3 2019

Hi,

I have the following table DDL :

                **CREATE TABLE KNDTSCM**

                **(**

                **"TSCM\_CNCONTRA"   NUMBER(8, 0) NOT NULL ENABLE,**

                **"TSCM\_CNSERVIC"   CHAR(4 BYTE) NOT NULL ENABLE,**

                **"TSCM\_CNSUBSER"   CHAR(2 BYTE) NOT NULL ENABLE,**

                **"TSCM\_CNMAPCO"    CHAR(4 BYTE) NOT NULL ENABLE,**

                **"TSCM\_CNCNLIDA"   CHAR(4 BYTE) DEFAULT '0030' NOT NULL ENABLE,**

                **"TSCM\_CNCENTID"   CHAR(4 BYTE) DEFAULT 'H2H1' NOT NULL ENABLE,**

                **"TSCM\_CNESTADO"   CHAR(2 BYTE),**

                **"TSCM\_CNFHALTA"   DATE DEFAULT SYSDATE,**

                **"TSCM\_CNFHULMO"   TIMESTAMP(6) DEFAULT SYSDATE,**

                **"TSCM\_CNUSUALT"   VARCHAR2(8 BYTE) NOT NULL ENABLE,**

                **"TSCM\_CNUSUMOD"   VARCHAR2(8 BYTE),**

                **"TSCM\_CNYACURE"   CHAR(1 BYTE),**

                **"TSCM\_CNYCONAR"   CHAR(1 BYTE),**

                **"TSCM\_CNACTIVS"   DATE**

                **)**

And the below sample data with the relevant columns highlighted

pastedImage_78.png

My problem is as below :

When tscm_cnfhulmo has anything less than SYSDATE, i don't want the query to select anything.If any date is equal to SYDATE, i do want the query to return me the data, but if i run the same query tomorrow and there is one record with the SYSDATE and the existing records from today I do not want it to give me an output. Makes sense? The query i have below is still returning meaning my HAVING clause is not working.

           SELECT DISTINCT tscm\_cncontra, COUNT(\*)

           FROM kndtscm

           WHERE tscm\_cnservic = '3102'

           AND tscm\_cnsubser IN ('I1','I2','R1','R2')

           AND tscm\_cnfhulmo > trunc(SYSDATE)

           GROUP BY tscm\_cncontra

           HAVING COUNT(tscm\_cnestado) NOT IN (SELECT COUNT(tscm\_cnestado)

                                                                                 FROM kndtscm

                                                                                 WHERE tscm\_cnservic = '3102'

                                                                                 AND tscm\_cnsubser IN ('I1','I2','R1','R2')

                                                                                 AND tscm\_cnfhulmo  \< trunc(SYSDATE)

                                                                                )
This post has been answered by Solomon Yakobson on Jun 3 2019
Jump to Answer
Comments
Post Details
Added on Jun 3 2019
6 comments
648 views