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

  • Home
  • SEARCH
  • 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 8939859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:54:40+00:00 2026-06-15T10:54:40+00:00

i have a DB2 table (orderitems) that has columns named ORDERITEMS_ID and ORDERS_ID I’m

  • 0

i have a DB2 table (orderitems) that has columns named ORDERITEMS_ID and ORDERS_ID I’m looking to get a count of all of the orderitems_id that are associated with each orders_id. I can get the count but i would like the order_id associated with that count.

i’ve tried

SELECT COUNT(orderitems_id) as total 
FROM orderitems 
GROUP BY orders_id 
ORDER BY total DESC

i believe this is giving me the total count of each of the items in a order_id. but i’m not sure how to add the order_id with the result set

if i try the following

SELECT orders_id, COUNT(orderitems_id) as total 
FROM orderitems 
GROUP BY orders_id 
ORDER BY total DESC

this is a bad query

i’ve looked into joining but that seems to be dealing with two tables…not sure how to append this information.

  • 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-15T10:54:41+00:00Added an answer on June 15, 2026 at 10:54 am

    Try this:

    select distinct orders_id, 
           count(orderitems_id) as total 
    from orderitems 
    group by orders_id 
    order by total desc
    

    Summarizing precisely what you want to do often helps. In this case, you want a count of orderitems_id for each distinct orders_id, e.g. for each different value of orders_id and not for each line. When you want a result depending on the different values of a column, think distinct.

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

Sidebar

Related Questions

I have a db2 table with certain columns. I have a stored procedure for
I have a table in db2 which has the following fields int xyz; string
I have a table of non trivial size on a DB2 database that is
I have a 'legacy' DB2 database that has many other applications and users. Trying
I have the following runstats command: RUNSTATS ON TABLE MYSCHEMA.TEST_PRODUCT ON ALL COLUMNS WITH
I have 2 databases named DB1 and DB2 with a table called movies .
We have a staging table (DB2) that receives production numbers on a daily basis
I have a DB2 table with column comments that I would like to allow
I have a Java Program that will import data from a DB2 table to
I am using DB2 and have the following table (Table A - 3 Columns)

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.