SELECT QUERY help

Im in SQL Developer and I want to return the top 1 of each record with the same PolNbr, CVG_ID and Cov_Eff_Dt but they have different INSURED_NAME so I need the Top1 Cov_Eff_Dt desc

 Select  i.INSURED_NAME,  c.POLNBR, c.CVG_ID , c.COV_EFFDT ,c.COV_EXPDT , c.LCF_IND, c.CARRID
  FROM SPECTRUM.COVERAGE c
  INNER JOIN SPECTRUM.INSURED_ENTITY i on c.CVG_ID = i.CVG_ID
  AND c.PolNBR = i.PolNBR
  AND c.COV_EFFDT = i.COV_EFFDT  
 WHERE c.COV_EXPDT > '19-MAY-09' AND (c.STATUS != 'V') AND (c.LCF_IND = 'Y')
GROUP BY i.INSURED_NAME,   c.POLNBR, c.CVG_ID , c.COV_EFFDT ,c.COV_EXPDT , c.LCF_IND, c.CARRID
 ORDER BY c.COV_EFFDT desc

Im getting back the following
InsuredCompany1      xxx150091       xxx0376              01-MAR-15      01-JAN-16
InsuredCompany2      xxx150091       xxx0376        01-MAR-15      01-JAN-16

I want the 2nd company only

Thanks
jknj72Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vikas GargAssociate Principal EngineerCommented:
Hello,

You can get it by just removing i.INSURED_NAME from the group by clause and replace i.INSURED_NAME with max/min in the select statement..

Select  min(i.INSURED_NAME),  c.POLNBR, c.CVG_ID , c.COV_EFFDT ,c.COV_EXPDT , c.LCF_IND, c.CARRID
  FROM SPECTRUM.COVERAGE c
  INNER JOIN SPECTRUM.INSURED_ENTITY i on c.CVG_ID = i.CVG_ID
  AND c.PolNBR = i.PolNBR
  AND c.COV_EFFDT = i.COV_EFFDT  
 WHERE c.COV_EXPDT > '19-MAY-09' AND (c.STATUS != 'V') AND (c.LCF_IND = 'Y')
GROUP BY c.POLNBR, c.CVG_ID , c.COV_EFFDT ,c.COV_EXPDT , c.LCF_IND, c.CARRID
 ORDER BY c.COV_EFFDT desc

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jknj72Author Commented:
Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.