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 8542061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:58:24+00:00 2026-06-11T11:58:24+00:00

I made up this weird example trying to illustrate what I want to do

  • 0

I made up this weird example trying to illustrate what I want to do (it’s kind of stupid, but bear with me):

Consider the following table:

EMPLOYEES
Employees table data

married, certified and religious are just boolean fields (in case of Oracle, they are of type NUMBER(1,0)).

I need to come up with SQL that displays for each hire_year, count of married, certified and religious employees within the following salary categories:

  • A SALARY > 2000
  • B SALARY BETWEEN 1000 AND 2000
  • C SALARY < 1000

Based on the above dataset, here is what I expect to get:

Expected result

So far, I’ve only come up with the following SQL:

SELECT
COUNT(CASE WHEN married = 1 THEN 1 END) as MARRIED,
COUNT(CASE WHEN certified = 1 THEN 1 END) as certified,
COUNT(CASE WHEN religious = 1 THEN 1 END) as religious,
hire_year
FROM employees
GROUP BY hire_year;

The result of executing this SQL is:

Actual result

Which is almost what I need, but I also need to divide these counters further down into the groups based on a salary range.

I guess that some analytic function, that divides groups into the buckets based on some SQL expression would help, but I can’t figure out which one. I tried with NTILE, but it expects a positive constant as a parameter, rather than an SQL expression (such as SALARY BETWEEN X and Y).

  • 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-06-11T11:58:25+00:00Added an answer on June 11, 2026 at 11:58 am

    Nope, no need for analytic functions; they’re difficult to have in the same query as an aggregate function anyway.

    You’re looking for the case statement again, you just have to put it in the GROUP BY.

    select hire_year
         , sum(married) as married
         , sum(certified) as certified
         , sum(religious) as religious
         , case when salary > 2000 then 'A'
                when salary >= 1000 then 'B'
                else 'C' end as salary_class
      from employees
     group by hire_year
         , case when salary > 2000 then 'A'
                when salary >= 1000 then 'B'
                else 'C' end
    

    Note that I’ve changed your count(case when...) to sum(). This is because you’re using a boolean 1/0 so this’ll work in the same manner but it’s a lot cleaner.

    For the same reason I’ve ignored your between in your salary calculation; there’s no particular need for it as if the salary is greater than 2000 the first CASE has already been fulfilled.

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

Sidebar

Related Questions

I made this Greasemonkey script: var maxpi = 250; var p1 = /html/body/div/div[2]/div/div[2]/table[2]/tbody/tr[1]/td[11]; var
I'm trying to update a table in my AccessDB and i'm having a weird
I got this weird error when I was trying to compile matlab to C++
This is just a why is something weird happening question. I made a mistake
I'm sure this is going to sound very weird, but I'll ask anyway. I
Example #1 bschaeffer's answer to this question - in his last example: $this->load->model('table'); $data
This is a tricky one to explain (and very weird), so bear with me.
I made this topic here , but wasn't sure how to reply to it
Made this nice little loop for hiding and showing div's, works as a charm
I made this function to verify a user's twitter credentials. Its running on two

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.