Count (*) that is a conditional statement and then if the result

HI: This is table_1:
with Table_1
as
(
      select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') incident_date, 2005 Policy_no  from dual
      union all
      select to_date('04-MAR-2009 12:12','DD-MON-YYYY HH24:MI') incident_date, 2009 Policy_no  from dual
      union all
      select to_date('25-JUL-2010 23:30','DD-MON-YYYY HH24:MI') incident_date, 2013 Policy_no  from dual
)
select * from Table_1
with Table_2
as
(
      select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 100 agr_line_no, 'BIKE' object  from dual
      union all
      select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 200 agr_line_no, 'BUCKS'  from dual
      union all
      select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 300 agr_line_no, 'CELL'  from dual
      union all
      select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 400 agr_line_no, 'CAR'  from dual
      union all
      select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 500 agr_line_no, 'HOUSE'  from dual
      union all
      select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 100 agr_line_no, 'BIKE' object  from dual
      union all
      select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 200 agr_line_no, 'BUCKS'  from dual
      union all
      select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 300 agr_line_no, 'BEACH'  from dual
      union all
      select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 400 agr_line_no, 'CAR'  from dual
      union all
      select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 500 agr_line_no, 'HOUSE'  from dual
      union all
      select to_date('20-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('29-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2013 Policy_no, 676 agr_line_no, 'CAR'  from dual
)
select * from Table_2
I need to perform a count (*) on agr_line_no on the condition that there is other other objects 'CAR' that is to say if forpolicy_no = 2005 the count should be 4.

Be Table_1 to table_2 on Policy_no and all columns in table_1 must be selected as well as an additional column called Object_many.

Object_many is based on the count (*) from table_2 it is if the number is > 1, 'Y' must be returned to another "n".

Also, there must be a clause where clause that is to say when table_1.incident_date between .cover_start_date table_2 and table_2 .cover_end_date

Final result should look like this
 with Table_1
as
(
      select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') incident_date, 2005 Policy_no, 'Y' Object_many from dual
      union all
      select to_date('04-MAR-2009 12:12','DD-MON-YYYY HH24:MI') incident_date, 2009 Policy_no, 'Y' Object_many from dual
      union all
      select to_date('25-JUL-2010 23:30','DD-MON-YYYY HH24:MI') incident_date, 2013 Policy_no, 'N' Object_many from dual
)
select * from Table_1
Thanks in advance!

Banner:
Oracle Database 11 g Release 11.2.0.2.0 - 64 bit Production
PL/SQL Release 11.2.0.2.0 - Production
"CORE 11.2.0.2.0 Production."
AMT for Linux: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production
with Table_1
as
(
      select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') incident_date, 2005 Policy_no  from dual
      union all
      select to_date('04-MAR-2009 12:12','DD-MON-YYYY HH24:MI') incident_date, 2009 Policy_no  from dual
      union all
      select to_date('25-JUL-2010 23:30','DD-MON-YYYY HH24:MI') incident_date, 2013 Policy_no  from dual
), Table_2
as
(
      select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 100 agr_line_no, 'BIKE' object  from dual
      union all
      select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 200 agr_line_no, 'BUCKS'  from dual
      union all
      select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 300 agr_line_no, 'CELL'  from dual
      union all
      select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 400 agr_line_no, 'CAR'  from dual
      union all
      select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 500 agr_line_no, 'HOUSE'  from dual
      union all
      select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 100 agr_line_no, 'BIKE' object  from dual
      union all
      select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 200 agr_line_no, 'BUCKS'  from dual
      union all
      select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 300 agr_line_no, 'BEACH'  from dual
      union all
      select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 400 agr_line_no, 'CAR'  from dual
      union all
      select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 500 agr_line_no, 'HOUSE'  from dual
      union all
      select to_date('20-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('29-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2013 Policy_no, 676 agr_line_no, 'CAR'  from dual
)
--
-- end-of-test-data
--
select
table_1.policy_no,
max(table_1.incident_date) incident_date,
case when count(table_2.agr_line_no) > 1 then 'Y' else 'N' end object_many
from table_1
left outer join table_2
   on table_2.policy_no = table_1.policy_no
   and table_2.cover_start_date <= table_1.incident_date
   and table_2.cover_end_date >= table_1.incident_date
   and table_2.object != 'CAR'
group by
table_1.policy_no
order by
table_1.policy_no;

I assumed policy_no either the primary key in Table_1 - so max (table_1.incident_date) will work.

If you have an index on Table_2 on coverage dates, then the construction above could be better than one between. Otherwise between will be quite OK ;-)

Tags: Database

Similar Questions

Maybe you are looking for

  • Did fix my phone

    I'm just a matter

  • Carpet * a/Panasonic UJ-830 s satellite L10

    Hello I was put in with the carpet * a reader of DVD-RAM UJ-830 s mounted on my Satellite L10 for a few years, because I have used in a extent the laptop somewhere and have had my external USB DVD - RAM drive still available. However; now I am in the

  • New Lenovo Ideapad Z710 friend refuses to install the touchpad driver.

    A few weeks ago my friend bought new a laptop Lenovo Ideapad Z710 pre-installed with Win8. I "upgraded" to Windows 7 for him, as none of us can handle the new OS. Usually, I have no trouble to reinstall the drivers after installing a new OS, but for

  • MD3220i - virtual drives Windows 2008 offline

    After installation of the MD3220i and the initiator configuration iSCSI, I see the LUNS from storage but they are offline and I can do something with them. Any help?

  • Problems with my window, system windows photo viewer 7. ____

    Error message: the Windows Photo Viewer cannot display images because there may not be enough memory available on your computer.  Close some programs that you do not use or free up hard disk space and then try again. I got rid of some programs and so