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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:18:54+00:00 2026-06-07T11:18:54+00:00

I have a select statement that gathers data from one monthly-summary table like the

  • 0

I have a select statement that gathers data from one monthly-summary table like the following

companyname, employee, id, usage_a, ... usage_m 

I need to create report from 6 monthly-summary tables that looks like the following

companyname, employee, id, 
jan_usage_a, ...june_usage_a, average (jun_jan_usage_a), 
...  
jan_usage_m, ...june_usage_m, average (jan_jun_usage_m)

I have done something similar with less columns, but i had to create a table with columns for each month’s usage and then merge data in month to month with a single query. At the end I would export to excel create another column to average the each usage over the months required.

For the report above I’d have to create a table over one hundred columns.
My question is is there a better way to do this?
Thanks.

Sorry for the title if it’s a little unclear.

  • 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-07T11:18:56+00:00Added an answer on June 7, 2026 at 11:18 am

    If you have six different tables, then you need to join them together:

    select tjan.companyname, tjan.employee, tjan.id, <rest of tjan columns>, <rest of jfeb columns>
    from tjan join
         tfeb 
         on tjan.companyname = tfeb.companyname and
            tjan.employee = tfeb.employee and
            tjan.id = tfeb.id
    etc. etc. etc.
    

    The problem that you have is that the populations in the different months may be different, so the joins will lose rows. A good way to handle this is with a driving table:

    select . . .
    from (select companyname, employee, id from tjan union
          select companyname, employee, id from tfeb union
          . . .
         ) driving left outer join
         tjan
         on tjan.companyname = driving.companyname and
            tjan.employee = driving.employee and
            tjan.id = driving.id left outer join
         tfeb
         on tfeb.companyname = driving.companyname and
            tfeb.employee = driving.employee and
            tfeb.id = driving.id left outer join
        . . .
    

    You can do all this in one SQL statement. There are repetitive parts (such as the column names in the select). Consider using Excel to generate these.

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

Sidebar

Related Questions

I have a SELECT statement like this: SELECT T1.COD, T1.NAME, (SELECT MAX(T2.DATA) FROM dbo.TAB2
I have a SELECT statement that looks like this: SELECT * FROM photos, p_votes
I have to following SQL Statement that I want to conver to LINQ Select
I want to execute one mysql statement where I have a select that is
I have a column in my select statement that looks like this: SELECT CASE
I have a select statement that returns a table full of SELECT statements (It
I have a select statement that returns a field in a table. records =
I have a table that I need a MYSQL Select statement so I can
I have the following SQL statement SELECT [Motor] ,[Time] FROM [logger].[dbo].[motor] WHERE day([Time]) =
I'm trying to make a select statement that joins results from three in-line table

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.