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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:05:38+00:00 2026-06-10T01:05:38+00:00

I have written a query in which one column is a month. From that

  • 0

I have written a query in which one column is a month. From that I have to get min month, max month, and median month. Below is my query.

select ext.employee,
       pl.fromdate,
       ext.FULL_INC as full_inc,
       prevExt.FULL_INC as prevInc,
       (extract(year from age (pl.fromdate))*12 +extract(month from age (pl.fromdate))) as month,
       case
         when prevExt.FULL_INC is not null then (ext.FULL_INC -coalesce(prevExt.FULL_INC,0))
         else 0
       end as difference,
       (case when prevExt.FULL_INC is not null then (ext.FULL_INC - prevExt.FULL_INC) / prevExt.FULL_INC*100 else 0 end) as percent
from pl_payroll pl
  inner join pl_extpayfile ext
          on pl.cid = ext.payrollid
         and ext.FULL_INC is not null
  left outer join pl_extpayfile prevExt
               on prevExt.employee = ext.employee
              and prevExt.cid = (select max (cid) from pl_extpayfile
                                 where employee = prevExt.employee
                                 and   payrollid = (
                                   select max(p.cid)
                                   from pl_extpayfile,
                                        pl_payroll p
                                   where p.cid = payrollid
                                   and   pl_extpayfile.employee = prevExt.employee
                                   and   p.fromdate < pl.fromdate
                                 )) 
              and coalesce(prevExt.FULL_INC, 0) > 0 
where ext.employee = 17 
and (exists (
    select employee
    from pl_extpayfile preext
    where preext.employee = ext.employee
    and   preext.FULL_INC <> ext.FULL_INC
    and   payrollid in (
      select cid
      from pl_payroll
      where cid = (
        select max(p.cid)
        from pl_extpayfile,
             pl_payroll p
        where p.cid = payrollid
        and   pl_extpayfile.employee = preext.employee
        and   p.fromdate < pl.fromdate
      )
    )
  )
  or not exists (
    select employee
    from pl_extpayfile fext,
         pl_payroll p
    where fext.employee = ext.employee
    and   p.cid = fext.payrollid
    and   p.fromdate < pl.fromdate
    and   fext.FULL_INC > 0
  )
)
order by employee,
         ext.payrollid desc

If it is not possible, than is it possible to get max month and min month?

  • 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-10T01:05:39+00:00Added an answer on June 10, 2026 at 1:05 am

    You want the aggregate functions named min and max. See the PostgreSQL documentation and tutorial:

    • http://www.postgresql.org/docs/current/static/tutorial-agg.html
    • http://www.postgresql.org/docs/current/static/functions-aggregate.html

    There’s no built-in median in PostgreSQL, however one has been implemented and contributed to the wiki:

    http://wiki.postgresql.org/wiki/Aggregate_Median

    It’s used the same way as min and max once you’ve loaded it. Being written in PL/PgSQL it’ll be a fair bit slower, but there’s even a C version there that you could adapt if speed was vital.

    UPDATE After comment:

    It sounds like you want to show the statistical aggregates alongside the individual results. You can’t do this with a plain aggregate function because you can’t reference columns not in the GROUP BY in the result list.

    You will need to fetch the stats from subqueries, or use your aggregates as window functions.

    Given dummy data:

    CREATE TABLE dummystats ( depname text, empno integer, salary integer );
    INSERT INTO dummystats(depname,empno,salary) VALUES
    ('develop',11,5200),
    ('develop',7,4200),
    ('personell',2,5555),
    ('mgmt',1,9999999);
    

    … and after adding the median aggregate from the PG wiki:

    You can do this with an ordinary aggregate:

    regress=# SELECT min(salary), max(salary), median(salary) FROM dummystats;
     min  |   max   |         median          
    ------+---------+----------------------
     4200 | 9999999 | 5377.5000000000000000
    (1 row)
    

    but not this:

    regress=# SELECT depname, empno, min(salary), max(salary), median(salary)
    regress-# FROM dummystats;
    ERROR:  column "dummystats.depname" must appear in the GROUP BY clause or be used in an aggregate function
    

    because it doesn’t make sense in the aggregation model to show the averages alongside individual values. You can show groups:

    regress=# SELECT depname, min(salary), max(salary), median(salary) 
    regress-# FROM dummystats GROUP BY depname;
      depname  |   min   |   max   |          median          
    -----------+---------+---------+-----------------------
     personell |    5555 |    5555 | 5555.0000000000000000
     develop   |    4200 |    5200 | 4700.0000000000000000
     mgmt      | 9999999 | 9999999 |  9999999.000000000000
    (3 rows)
    

    … but it sounds like you want the individual values. For that, you must use a window, a feature new in PostgreSQL 8.4.

    regress=# SELECT depname, empno, 
                     min(salary) OVER (), 
                     max(salary) OVER (), 
                     median(salary) OVER () 
              FROM dummystats;
    
      depname  | empno | min  |   max   |        median         
    -----------+-------+------+---------+-----------------------
     develop   |    11 | 4200 | 9999999 | 5377.5000000000000000
     develop   |     7 | 4200 | 9999999 | 5377.5000000000000000
     personell |     2 | 4200 | 9999999 | 5377.5000000000000000
     mgmt      |     1 | 4200 | 9999999 | 5377.5000000000000000
    (4 rows)
    

    See also:

    • http://www.postgresql.org/docs/current/static/tutorial-window.html
    • http://www.postgresql.org/docs/current/static/functions-window.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this query for retrieving data from mysql as below select FeedbackCode,EMailID,FeedbackDetail,
I have Written a query which returns multiple column.Out of which One Column contains
I have written a query in Sql server 2008. select select * from program
I have written a query which is a composite query like one update query
The query I have written is: delete from featured where featuredID IN( select top(@removeAmnt)
This one has me rather confused. I've written a query which runs fine from
I have a table which has one TEXT column that looks like this: 0-4
I have written a query to find similar ids based on tags from a
I have written a SQL query that works just fine, but am having a
I have one jsp which accepts avlue from textbox and passes it to another

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.