Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6073705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:18:02+00:00 2026-05-23T10:18:02+00:00

Intro I have a complex query I need to write (for an Oracle DB).

  • 0

Intro

I have a complex query I need to write (for an Oracle DB). I’ve tried to make an analogy to avoid using company data, so let me know if something seems weird.

Tables

CAT

  • CAT_ID (PK)

KITTEN

  • KITTEN_ID (PK)

CAT_KITTEN_PLAY

  • PLAY_ID (PK)
  • CAT_ID (FK to CAT table)
  • KITTEN_ID (FK to KITTEN table)
  • KITTEN_GENDER (String – M or F)
  • PLAY_TIME (Date)

KITTEN_INFO

  • KITTEN_ID (FK to KITTEN table)
  • NAME (String, e.g. “Name”)
  • VALUE (String, e.g. “Duchess”)

Description

So basically, the CAT and KITTEN tables just hold IDs and some other random information. KITTEN_INFO holds the name of the kittens (among other things). And CAT_KITTEN_PLAY logs all of the times a cat plays with a kitten.

What I need, is to write a query that returns all of the CATs whose most recent play time with a male KITTEN was in a certain date range. Let’s call it Jan 1 2011 to Jan 31 2011. I also need to include the play date and name of the most recent male and female kitten the cat played with.

So Far

Below is what I have so far:

SELECT cat.*, lastMale.PLAY_TIME maleTime, lastFemale.PLAY_TIME femaleTime, maleName.VALUE male, femaleName.VALUE female
FROM CAT cat
LEFT JOIN CAT_KITTEN_PLAY lastMale ON lastMale.CAT_ID = cat.CAT_ID 
                                  AND lastMale.GENDER = 'M'
LEFT JOIN CAT_KITTEN_PLAY lastFemale ON lastFemale.CAT_ID = cat.CAT_ID 
                                    AND lastFemale.GENDER = 'F'
LEFT JOIN KITTEN_INFO femaleName ON femaleName.KITTEN_ID = lastFemale.KITTEN_ID 
                                AND maleName.NAME = 'Name'
LEFT JOIN KITTEN_INFO maleName ON maleName.KITTEN_ID = lastMale.KITTEN_ID 
                              AND femaleName.NAME = 'Name'
    WHERE lastMale.PLAY_TIME BETWEEN '01-JAN-2011 12:00:00 AM'
                                 AND '31-JAN-2011 11:59:59 PM'

Problems

This does not account for the fact that a cat may have played with a make/female kitten more than once. So, I’d want to add “AND lastMale.PLAY_TIME = (SELECT MAX(PLAY_TIME) FROM CAT_KITTEN_PLAY WHERE CAT_ID = cat.ID AND KITTEN_GENDER = ‘M’)” onto the first join, and a similar one to the second join. But subqueries are not allowed on joins.

Any thoughts? Note that a cat may have never played with a female kitten. But the cat should still be included if they meet the criteria (hence the left joins).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:18:02+00:00Added an answer on May 23, 2026 at 10:18 am

    Analytic functions are probably the best answer to your problem:

    SELECT DISTINCT
           cat.*, 
           first_value(lastMale.PLAY_TIME) 
              over (partition by cat.id 
                    order by lastMale.PLAY_TIME desc nulls last) maleTime,
           first_value(lastFemale.PLAY_TIME) 
              over (partition by cat.id 
                    order by lastFemale.PLAY_TIME desc nulls last) femaleTime,
           first_value(maleName.VALUE)  
              over (partition by cat.id 
                    order by lastMale.PLAY_TIME desc nulls last) male,
           first_value(femaleName.VALUE)  
              over (partition by cat.id 
                    order by lastFemale.PLAY_TIME desc nulls last) female
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In our organization we have the need to let employees filter data in our
I have a complex query using many joins (8 in fact). I was thinking
Has anyone tried the ActiveRecord Intro Sample with C# 3.5? I somehow have the
I have a relatively complex query, with several self joins, which works on a
i have some complex select and result need to place in the table. I
Our users have created MS-Excel spreadsheets which over time have evolved into fairly complex
I have a select from (nothing to complex) Select * from VIEW This view
You can also write hibernate hql or criteria queries. I have Teacher entity and
I have a case where I need to translate (lookup) several values from the
I recently started to use LINQ to SQL and i have a minor complex

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.