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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:55:29+00:00 2026-05-23T04:55:29+00:00

I am trying to do rather complex SQL query to produce a report. This

  • 0

I am trying to do rather complex SQL query to produce a report. This is a database used by an inventory and accounting system.

Essentially I need to produce a report with the following columns

  • Month / Year (group results by month / year)
  • Reseller (order results by reseller with in the month / year group)
  • Total sales – Sales – Hardware
  • Total sales – Sales – Consumables

The following tables will need to be used in the report:

  • Invoice
  • Reseller
  • Job
  • JobStockItem
  • Stock

Essentially the query would need to start as:
1. Select all invoices from Invoice
2. Get the reseller name from Reseller.Name (join on Reseller.ID with Invoice.CustomerID)
3. Get the associated job ID from Job table (join on Job.InvoiceID with Invoice.ID)
4. Get each component of the invoice from JobStockItems (join JobStockItem.JobID on Job.ID)
5. Get the stock item in in the job from Stock (join on JobStockItems.StockId on Stock.ID) and see if the category (Stock.Category1) is either Hardware or Consumables
6. If the stock item is hardware or consumables, use the sale price in the JobStockItem (JobStockItem.PriceExTax) and add it towards the total for the month of the resellers purchases

The month and year come from the invoice date (Invoice.InvoiceDate).

Now I could produce this result myself by executing a bunch of queries and processing myself, one each for the above steps, but it’s going to end up slow and I’m sure there’d have to be a query out there that could wrap all those requirements up and do it in one?

I have not attempted to do the query yet as to be honest, I don’t know where to start – it’s a lot more complex than anything I’ve done in the past.

I am just using Management Studio, not using Reporting Services, Crystal Reports or anything. My aim is to dump the output to HTML when I have it working.

Thanks heaps in advance.

  • 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-23T04:55:30+00:00Added an answer on May 23, 2026 at 4:55 am

    It think if you Left Join into the JobStockItems table twice (once for hardware, and once for consumables), you can manage all that in one query. The final query will look something like this (Don’t have my editor up right now, so apologies for any typos)

    SELECT DATEPART(m, Invoice.InvoiceDate) month, 
           DATEPART(yy, Invoice.InvoiceDate) year, 
           Reseller.Name, 
           SUM(jobstockitems_hardware.Price) sales_hardware,
           SUM(jobstockitems_consumables.Price) sales_consumables, 
    FROM Invoice
    INNER JOIN Reseller
    ON Invoice.CustomerID = Reseller.ID
    INNER JOIN Job
    ON Invoice.ID = Job.InvoiceID
    LEFT JOIN (SELECT JobID, SUM(PriceExTax) Price 
               FROM JobStockItems 
               INNER JOIN Stock 
               ON JobStockItems.StockID = Stock.StockID
               AND Stock.Category1 = 'Hardware'
               GROUP BY JobID) jobstockitems_hardware
    ON Job.ID = jobstockitems_hardware.JobID
    LEFT JOIN (SELECT JobID, SUM(PriceExTax) Price 
               FROM JobStockItems 
               INNER JOIN Stock 
               ON JobStockItems.StockID = Stock.StockID
               AND Stock.Category1 = 'Consumables'
               GROUP BY JobID) jobstockitems_consumables
    ON Job.ID = jobstockitems_consumables.JobID
    GROUP BY DATEPART(m, Invoice.Date), 
             DATEPART(yy, Invoice.Date), 
             Reseller.Name
    ORDER BY DATEPART(yy, Invoice.Date) ASC, 
             DATEPART(m, Invoice.Date) ASC, 
             Reseller.Name ASC
    

    I’m assuming that Retailer has a column called Name that you want to return as well, feel free to change that to ID or whatever else you’d rather return.

    Edit: Fixed query to remove duplicates

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

Sidebar

Related Questions

I have a rather complex logical query I'm trying to execute. Essentially there are
Trying to assemble a rather complex jQuery selector here, and having trouble. Essentially, I'm
I'm trying to debug a rather complicated formula evaluator written in T-SQL UDFs (don't
I am trying to visualize a rather complex structure in the dot language. Because
I am trying to create a rather simple effect on a set of images.
I am trying to take a rather large CSV file and insert it into
I'm trying to do a Data Binding in the C# code behind rather than
Trying to get my css / C# functions to look like this: body {
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to keep all the presentation stuff in the xhtml on this project and

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.