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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:37:40+00:00 2026-06-03T07:37:40+00:00

I have a table Register with columns: class_id bigint NOT NULL, disciple text, datelesson

  • 0

I have a table "Register"

with columns:

  class_id bigint NOT NULL,
  disciple text,
  datelesson date NOT NULL,
  student_id integer NOT NULL,
  note character varying(2)

now I want to calculate the average score for each student_id and the number of absent

Select * from "Register" as m

Join

(SELECT AVG(average), COUNT(abs) FROM (SELECT
  CASE
      WHEN "note" ~ '[0-9]' THEN CAST("note" AS decimal) 
  END AS average,
  CASE
      WHEN "note" ='a' THEN "note"
  END AS abs
FROM "Register" ) AS average)n 
on class_id=0001 
And datelesson between '01.01.2012' And  '06.06.2012' 
And discipline='music' order by student_id

Result is this:

0001;"music";"2012-05-02";101;"6";7.6666666666666667;1
0001;"music";"2012-05-03";101;"a";7.6666666666666667;1
0001;"music";"2012-05-01";101;"10";7.6666666666666667;1
0001;"music";"2012-05-02";102;"7";7.6666666666666667;1
0001;"music";"2012-05-03";102;"";7.6666666666666667;1
0001;"music";"2012-05-01";102;"";7.6666666666666667;1

The result I receive is for the whole column but how do I calculate average marks for each student?

  • 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-03T07:37:42+00:00Added an answer on June 3, 2026 at 7:37 am

    Could look like this:

    SELECT student_id
         , AVG(CASE WHEN note ~ '^[0-9]*$' THEN note::numeric
                                           ELSE NULL END) AS average
         , COUNT(CASE WHEN note = 'a' THEN 1 ELSE NULL END) AS absent
    FROM   "Register"
    WHERE  class_id = 1 
    AND    datelesson BETWEEN '2012-01-01' AND  '2012-06-06' 
    AND    discipline = 'music'
    GROUP  BY student_id
    ORDER  BY student_id;
    

    I added a couple of improvements.

    • You don’t need to double-quote lower case identifiers.
    • If you want to make sure, there are only digits in note, your regular expression must be something like note ~ '^[0-9]*$'. What you have only checks if there is any digit in the string.
    • It’s best to use the ISO format for dates, which works the same with any locale: YYYY-MM-DD.
    • The count for absence works, because NULL values do not count. Ypu could also use sum for that.
    • As class_id is a number type, bigint to be precise, leading zeros are just noise.
      Use class_id = 1 instead of class_id = 0001.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In iOS apps, we have to register nib files with our table view before
I have table like below ID User Date Location 1 Tom 6-Mar-2012 US 2
I have a table 'TYPE' in this table i have 2 columns as 'SKEY'
I have a table SIGNUPS, where I register all signups to a specific event.
I have a table which PolygonMapping whose register contains a polygon id and other
I have table Owner in my database where one of the field (System) saved
I have table of over 4 million rows and accidentally in one column there
I have table of products, and there are 2 fields: price, currency. Possible values
I have table tree. I have query: SELECT * FROM tree WHERE pid =10
I have table v_c_country with column table_name I can make an query and, as

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.