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

The Archive Base Latest Questions

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

Supposing I have the following rows: VARIABLENAME YYYYMM XXXXYY DATAOBS variable_1 | 201101 |

  • 0

Supposing I have the following rows:

VARIABLENAME   YYYYMM   XXXXYY   DATAOBS
variable_1   | 201101 | 111100 | 20
variable_2   | 201101 | 111100 | 20
variable_1   | 201102 | 111100 | 30
variable_2   | 201102 | 111100 | 30
variable_1   | 201101 | 222211 | 20
variable_2   | 201101 | 222211 | 20
variable_1   | 201102 | 222211 | 30
variable_2   | 201102 | 222211 | 30

I would like to know how to get the result:

VARIABLENAME      YYYYMM    XXXXYY   DATAOBS
variables_1and2 | 201101  | 111100 | 40
variables_1and2 | 201102  | 111100 | 60
variables_1and2 | 201101  | 222211 | 40
variables_1and2 | 201102  | 222211 | 60

where all the DATAOBS in rows containing variable_1 and variable_2 within each respective YYYYMM within in each respective XXXXYY are added and placed in a new row, variables_1and2, still within their respective YYYYMM and respective XXXXYY.

If it’s necessary to know, there are several hundred different YYYYMM and XXXXYY in the actual data I’m dealing with.

Thanks 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-06-07T12:54:21+00:00Added an answer on June 7, 2026 at 12:54 pm

    It sounds like you just want

    SELECT 'variables_1and2' variablename,
           yyyymm,
           xxxxyy,
           sum(dataobs) dataobs
      FROM your_table
     WHERE variablename in ('variable_1', 'variable_2' )
     GROUP BY yyyymm, xxxxyy
    

    If the intention is to insert these rows back into your table, you’d need an INSERT

    INSERT INTO your_table( variablename, yyyymm, xxxxyy, dataobs )
      SELECT 'variables_1and2' variablename,
             yyyymm,
             xxxxyy,
             sum(dataobs) dataobs
        FROM your_table
       WHERE variablename in ('variable_1', 'variable_2' )
       GROUP BY yyyymm, xxxxyy
    

    Of course, it would be very unusual for it to make sense to store both detail and aggregate data in the same table. That would generally be a poor design.

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

Sidebar

Related Questions

Supposing I have the following script <div id=something> <?php // execute a loop ?>
Supposing I have the following type from an external library: union foreign_t { struct
Supposing I have the following text: Lorem ipsum dolor sit amet, consectetur adipisicing elit,
Supposing I have the following LambdaExpression: var itemParam = Expression.Parameter(typeof(Thing), thing); var someValue =
Supposing I have the following: module A class B # ... end # ...
Supposing I have the following public class Foo { private Map<Integer,SomeObject> myMap; public Foo()
Supposing I have the following action: def index @posts = Post.joins(:tags).where(:tags => {:id =>
supposing I have the following data: id name source ----------------- 1 'n' 'a' 1
Supposing I have the following list: A | B | C | D or
Supposing we have the following records in an SQL Server table. Date 19/5/2009 12:00:00

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.