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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:23:14+00:00 2026-05-30T02:23:14+00:00

Checked some similar posts but they all had a common ID and these don’t

  • 0

Checked some similar posts but they all had a common ID and these don’t so I’m hoping you ca help me.

What I want to achieve is this:

QTR1 | QTR2 | SUM(Sales) | SUM(Sales2)

 1      2          9         8
 1      3          9         7
 1      4          9         6
 2      1          8         9
 2      3          8         7

And so on for all values to get a total of 12 results (Each quarter is compared to the three other quarters. The qtr1 and qtr2 are simply to distinguish the columns). I currently can do the product of just the quarters…

SELECT d1.quarter 'qtr1' , a.quarter 'qtr2'
FROM datedim d1, datedim a
WHERE a.quarter <> d1.quarter
GROUP BY a.quarter, d1.quarter;

But when I add in the sums for the quarters SUM(earnings) from the earnings table the query hangs and never completes. The earnings table is connected to the date table containing the quarters by a surrogate key, so ideally it has to be

FROM datedim, earningsfact

In order to get both data types and aggregate the sales.

  • 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-30T02:23:15+00:00Added an answer on May 30, 2026 at 2:23 am

    Assuming I understand the problem correctly, here’s an example solution:

    declare @test table ([year] int, [quarter] int, sales money)
    
    insert into @test values (2001, 1, 1100)
    insert into @test values (2001, 2, 2100)
    insert into @test values (2001, 3, 3100)
    insert into @test values (2001, 4, 4100)
    insert into @test values (2002, 1, 1200)
    insert into @test values (2002, 2, 2200)
    insert into @test values (2002, 3, 3200)
    insert into @test values (2002, 4, 4200)
    insert into @test values (2003, 1, 1300)
    insert into @test values (2003, 2, 2300)
    insert into @test values (2003, 3, 3300)
    insert into @test values (2003, 4, 4300)
    
    ; with summary (quarter, salesTotal) as
    (
        select quarter, sum(sales)
        from @test
        group by [quarter]
    )
    
    select 
        s1.quarter [QTR1]
        , s2.quarter [QTR2]
        , s1.salesTotal [SUM(Sales1)]
        , s2.salesTotal [SUM(Sales1)]
    from
        summary s1
        join summary s2
            on s1.quarter != s2.quarter
    order by
        s1.quarter
        , s2.quarter
    

    It assumes that you have quarterly data for multiple years. The common table expression summarizes it all into quarters, and the select statement compares each quarter to every other quarter.

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

Sidebar

Related Questions

We had an employee leave the company, and they left some files checked out
I saw similar posts but some weren't clear. Here is the repeater head: <asp:Repeater
I checked some other similar questions but some of them are old and i'm
I have checked some of the similar questions which were asked. I want to
I checked out a post similar to this but the linkage was different the
[I posted a similar question on serverfault.com, but had very little feedback so trying
I saw that some others faced this similar problem. I have read and checked
I've checked similar tags but I can't seem to find out what I'm doing
I know there's already tons of similar questions, but I checked over twenty answers
I have checked a lot of other questions similar to this, but it seems

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.