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

  • Home
  • SEARCH
  • 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 1080345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:00:57+00:00 2026-05-16T22:00:57+00:00

Suppose I have three tables: user , group and xref , a table that

  • 0

Suppose I have three tables: user, group and xref, a table that gives them many-to-many RI.

I might want to see how groups each user belongs to:

select
    user.user_id,
    user.user_name,
    count(*) as group_count
from
    user
        inner join xref on user.user_id = xref.user_id
        inner join group on group.group_id = xref.group_id
group by user.user_id, user.user_name

Everything’s OK so far. But, what if I want some extra information? I’m reporting, and I want to know if each user is a Developer or a Content Manager. Now, an anti-pattern emerges:

select
    user.user_id,
    user.user_name,
    count(*) as group_count,
    max( case group.group_name when 'Developers' then 'Y' else null end )
        as is_dev
    max( case group.group_name when 'Content Management' then 'Y' else null end )
        as is_cm
from
    user
        inner join xref on user.user_id = xref.user_id
        inner join group on group.group_id = xref.group_id
group by user.user_id, user.user_name

This works, and produces expected results, but it feels very wrong. What I want to ask Oracle is this:

“For each user, show me how many groups they’re in. Also, for all group names per user, show me if ‘Developers’ is one of the values.”

What I’m actually asking is this:

“For each user, show me how many groups they’re in. Also, for all group names per user, show me the highest value produced by this case expression.”

The reason that this is an anti-pattern is that I’m basically relying on the fact that Y happens to “bubble up” above null when evaluated with max(). If someone wanted to copy or augment this query, they could easily forget about the anti-pattern and accidentally change the return values to something that doesn’t use the same unintuitive coincidence.

Basically, the query I wish I could write is this:

select
    user.user_id,
    user.user_name,
    count(*) as group_count,
    any(group.group_name, 'Developers', 'Y', null) as is_dev,
    any(group.group_name, 'Content Management', 'Y', null) as is_cm
from
    user
        inner join xref on user.user_id = xref.user_id
        inner join group on group.group_id = xref.group_id
group by user.user_id, user.user_name

I’ve been sifting around for options, and it seems like there are a few potentials:

  • first_value could work, but I can’t figure out how to limit the corresponding partition window to the right rows.
  • Analytic functions with an over clause might work, but I do want to collapse the columns I’m grouping by, so it doesn’t seem to be a perfect fit.
  • Infuriatingly, there seems to be an any function documented here, but it only exists in a mysterious dialect called the Oracle OLAP DML, which I don’t think I can access using only SQL on 10g. But, it seems to do exactly what I want.

That’s all I got. Any ideas?

I recognize that there are two very simple ideas, “Do it in code” or “Do it in PL/SQL,” but that’s cheating. 🙂

  • 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-16T22:00:57+00:00Added an answer on May 16, 2026 at 10:00 pm

    I’d switch from MAX to SUM (with 1 rather than Y) so you are saying “Count the number of groups this person is in where the group name is Developers”.

    Then the pattern is similar to a “count the number of sales where the purchase value was more than $30”.

    You can, if desired, then add another expression to say “If the count is greater than zero then ‘yes’ this person is a developer”. Very explicit and probably unnecessary though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use: SELECT p.id as a, p.url as b, t.id as… May 17, 2026 at 3:22 am
  • Editorial Team
    Editorial Team added an answer There are two parts to the problem First Issue You… May 17, 2026 at 3:19 am
  • Editorial Team
    Editorial Team added an answer I thought I'd show the regex approach, too. It doesn't… May 17, 2026 at 3:18 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Suppose I have a table called Companies that has a DepartmentID column. There's also
Suppose that I have a test server with a large group of test accounts.
Suppose I have three classes. It is valid to instantiate A, but there are
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose I have a SELECT statement that returns some set of results. Is there
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose we have a table A: itemid mark 1 5 2 3 and table
Suppose I have a directory /dir inside which there are 3 symlinks to other
There's no strongly typed View() method to return an ActionResult. So, suppose I have
I have a simple question. Is there a way ( using reflections I suppose

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.