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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:19:55+00:00 2026-05-19T00:19:55+00:00

After searching and reading a little bit I came up with the following SQL

  • 0

After searching and reading a little bit I came up with the following SQL query for my application:

SELECT
  ROUND(AVG(CASE WHEN gender = 'M' THEN rating END), 1) avgAllM,
  COUNT(CASE WHEN gender = 'M' THEN rating END) countAllM,
  ROUND(AVG(CASE WHEN gender = 'F' THEN rating END), 1) avgAllF,
  COUNT(CASE WHEN gender = 'F' THEN rating END) countAllF,
  ROUND(AVG(CASE WHEN gender = 'M' AND UserAge(birth_date) <= 18 THEN rating END), 1) avgU18M,
  COUNT(CASE WHEN gender = 'M' AND UserAge(birth_date) <= 18 THEN rating END) countU18M,
  ROUND(AVG(CASE WHEN gender = 'F' AND UserAge(birth_date) <= 18 THEN rating END), 1) avgU18F,
  COUNT(CASE WHEN gender = 'F' AND UserAge(birth_date) <= 18 THEN rating END) countU18F
FROM movie_ratings mr INNER JOIN accounts a
  ON mr.aid = a.aid
WHERE mid = 5;

And I’m wondering how can I simplify this, if possible. The birth_date field is of type DATE and UserAge is a function to calculate the age from that date field.

The table structures are as follows:

[ACCOUNTS]
aid(PK), birth_date, gender

[MOVIE_RATINGS]
mid(PK), aid(PK,FK), rating

I’m looking for two things:

  • General simplifications to the code above that more experienced users know about that I don’t.
  • I’m doing this in PHP and for each record I’ll have an associative array with all those variables. I’m looking for a way to group them into a multidimensional array, so the PHP code is easier to read. Of course I don’t want to do this in PHP itself, it would be pointless.

For instance, something like this:

$info[0]['avgAllM']
$info[0]['countAllM']
$info[1]['avgAllF']
$info[1]['countAllF']
$info[2]['avgU18M']
$info[2]['countU18M']
$info[3]['avgU18F']
$info[3]['countU18F']

Instead of:

$info['avgAllM']
$info['countAllM']
$info['avgAllF']
$info['countAllF']
$info['avgU18M']
$info['countU18M']
$info['avgU18F']
$info['countU18F']

I don’t even know if this is possible, so I’m really wondering if it is and how it can be done.

Why I want all this? Well, the SQL query above is just a fragment of the complete SQL I need to do. I haven’t done it yet because before doing all the work, I want to know if there’s a more compact SQL query to achieve the same result. Basically I’ll add a few more lines like the ones above but with different conditions, specially on the date.

  • 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-19T00:19:55+00:00Added an answer on May 19, 2026 at 12:19 am

    You could create a VIEW with the following definition

    SELECT
          CASE WHEN gender = 'M' THEN rating END AS AllM,
          CASE WHEN gender = 'F' THEN rating END AS AllF,
          CASE WHEN gender = 'M' AND UserAge(birth_date) <= 18 THEN rating END AS U18M,
          CASE WHEN gender = 'F' AND UserAge(birth_date) <= 18 THEN rating END AS U18F
          FROM movie_ratings mr INNER JOIN accounts a
            ON mr.aid = a.aid
          WHERE mid = 5
    

    Then SELECT from that

    SELECT ROUND(AVG(AllM), 1) avgAllM,
           COUNT(AllM)         countAllM,
           ROUND(AVG(AllF), 1) avg,
           COUNT(AllF)         countAllF,
           ROUND(AVG(U18M), 1) avgU18M,
           COUNT(U18M)         countU18M,
           ROUND(AVG(U18F), 1) avgU18F,
           COUNT(U18F)         countU18F
    FROM  yourview
    

    Might simplify things slightly?

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

Sidebar

Related Questions

After a bit of searching and reading the documentation, it's clear that you can
After searching online and reading the Oracle documentation for this, I need some help
After searching, I dont find the appropriate way to resolve following problem. After activating
UPDATE: After searching a bit I don't seem to be alone about this problem:
After searching and reading about Modern OpenGL in order to upgrade my existing project,
After searching a lot, reading every tutorials I've found and asking some questions here,
After many hours spent on reading documentations and searching for some tips on the
It's my first time writting there but after reading and searching a lot about
Whilst reading through the DirectWrite source code I came across the following struct: ///
After a lot of searching and reading, I am not really sure what I

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.