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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:23:30+00:00 2026-06-17T11:23:30+00:00

I query the Postgres database to get statistics for the 4 last years. However

  • 0

I query the Postgres database to get statistics for the 4 last years.
However I cannot figure out the correct syntax to replace the hard-coded date
in the AS part of the query with a function.

Say: date_part('year',current_date)-1 instead of "2012"

SELECT * FROM crosstab('
SELECT client,date_part (''year'',date) as SalesDate
       ,Sum(total)::integer AS Synthese
FROM statistic
WHERE date_part(''year'',date)>date_part(''year'',current_date)-5
AND date_part(''year'',date)<date_part(''year'',current_date)
GROUP BY client,SalesDate
ORDER BY 1,2',
$$VALUES 
(date_part('year',current_date)-4),
(date_part('year',current_date)-3),
(date_part('year',current_date)-2),
(date_part('year',current_date)-1)$$)
AS ( client text, "2009" text, "2010" text , "2011" text, "2012" text);
  • 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-17T11:23:32+00:00Added an answer on June 17, 2026 at 11:23 am

    Answer

    A principle of SQL: You can provide values dynamically, but identifiers are static.

    The outer call is a basic SQL statement, the column definition list consists of identifiers, not values. (That’s why you are forced to double-quote your otherwise illegal identifiers starting with a digit.) Therefore, you cannot use a function call in this place. Just not possible.

    Possible workaround

    There are tricky ways around this ..
    You could create a plpgsql function that returns a polymorphic composite type and hand in a parameter of a well defined composite type, which also carries column names. But that’s highly advanced stuff. You need to be up to speed with plpgsql for this.

    Consider my comprehensive answer under this related question:
    Refactor a PL/pgSQL function to return the output of various SELECT queries
    The second half of the answer is for you.

    Since you need a well-defined composite type you’d have to create a type before calling the function this way. Or just create a temporary table (that provides a type automatically). You could automate this with a DO statement that uses dynamic SQL to derive column names from the current date … I never said it was simple, but it’s possible.

    Base query

    In the meanwhile, your crosstab() query could be improved:

    SELECT * FROM crosstab(
        $$
        SELECT client
              ,date_part ('year', date) AS salesdate
              ,sum(total)::int AS synthese
        FROM   statistic
        WHERE  date >= date_trunc('year', now()) - interval '5y'
        AND    date <  date_trunc('year', now())
        GROUP  BY 1,2
        ORDER  BY 1,2
        $$
    
        ,$$VALUES 
             (date_part('year', now()) - 4)
            ,(date_part('year', now()) - 3)
            ,(date_part('year', now()) - 2)
            ,(date_part('year', now()) - 1)
        $$
        )
    AS  (client text
         ,"2009" text
         ,"2010" text
         ,"2011" text
         ,"2012" text);
    
    • Use dollar-quoting to avoid quoting hell.
    • Rewrite your WHERE conditions so they can use indexes.
      Detailed explanation in this closely related answer.

    The rest is a matter of taste and style.

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

Sidebar

Related Questions

I have some tables that I'm joining in a query with Postgres (9). However,
I'm having trouble regarding speed in a SELECT query on a Postgres database. I
I have an SQL query thats runs on the Postgres database of my Django
I currently have to implement a query on a postgres database using a prepared
My Postgres query calculates statistical aggregate from a bunch of sensor readings: SELECT to_char(ipstimestamp,
How can I do such query in Postgres? IF (select count(*) from orders) >
I've got a SQL query in Postgres that works fine from a SQL console/tool,
I want to remove duplicate rows return from a SELECT Query in Postgres I
The following Postgres SQL query will list all tables from all schemas and their
I have a problem with the query below in postgres SELECT u.username,l.description,l.ip,SUBSTRING(l.createdate,0,11) as createdate,l.action

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.