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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:31:18+00:00 2026-05-24T03:31:18+00:00

I have a need to create sales reports by day, week, month, etc. in

  • 0

I have a need to create sales reports by day, week, month, etc. in PostgreSQL. I have the following tables setup:

tbl_products:
    id INT
    name VARCHAR

tbl_purchase_order:
    id INT
    order_timestamp TIMESTAMP

tbl_purchase_order_items:
    id INT
    product_id INT (FK to tbl_products.id)
    order_id (FK to tbl_purchase_order.id)

I need to create a SQL query that returns the number of times a given product has been purchased within a given time frame. That is, I need to query the number of times a given product ID appears in a purchase order item in a specific month, day, year, etc. In an earlier question I learned how to use date_trunc() to truncate my TIMESTAMP column to the period of time I’m concerned about. Now I’m faced with how to perform the COUNT and GROUP BY properly.

I’ve tried several queries using various combinations of COUNT(XXX) and GROUP BY XXX but never seem to come up with what I’m expecting. Can someone give me guidance as to how to construct this query? I’m more of a Java developer, so I’m still getting up to speed on SQL queries. Thanks for any help you can provide.

  • 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-24T03:31:19+00:00Added an answer on May 24, 2026 at 3:31 am

    Count per year:

    SELECT oi.product_id, 
           extract(year from po.order_timestamp) as order_year
           count(*)
    FROM purchase_order_items oi
       JOIN purchase_order po ON po.id = oi.order_id
    GROUP BY extract(year from po.order_timestamp)
    

    Counter per month:

    SELECT oi.product_id, 
           extract(month from po.order_timestamp) as order_month
           extract(year from po.order_timestamp) as order_year
           count(*)
    FROM purchase_order_items oi
       JOIN purchase_order po ON po.id = oi.order_id
    GROUP BY extract(year from po.order_timestamp), 
             extract(month from po.order_timestamp)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello Everything is right? My problem is the following: I have 3 tables: create
I have two tables in MySQL sales database: Orders table: CREATE TABLE salestest.`orders` (
I have a need to create a HttpSession (via cookie) whenever a client invokes
I have a need to create a small pop up information box when a
I have a need to create a wrapper for a DLL, loading and unloading
I have a need to create a library of Object Oriented PHP code that
I have the need to create Sharepoint blog comments by code: SPSecurity.RunWithElevatedPrivileges(delegate() { sw.AllowUnsafeUpdates
I have a need to create a utility in Suze Linux. The utility will
I have a need to basically create a matrix of values in my Silverlight
I have dynamically created WrapPanel (_wp) with several Borders. And I need create handler

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.