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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:43:23+00:00 2026-05-18T20:43:23+00:00

I have two tables detail and head. The detail table will be written first.

  • 0

I have two tables detail and head. The detail table will be written first. Later, the head table will be written. The head is a summary of the detail table. I would like to keep a reference from the detail to the head table. I have a solution but it is not elegant and requires duplicating the joins and filters that were used during summation. I am looking for a better solution. The below is an example of what I currently have. In this example, I have simplified the table structure. In the real world, the summation is very complex.

-- Preparation
create table #detail (
      detail_id int identity(1,1)
    , code char(4)
    , amount money 
    , head_id int null
    );

create table #head (
      head_id int identity(1,1)
    , code char(4)
    , subtotal money
    );

insert into #detail ( code, amount ) values ( 'A', 5 );
insert into #detail ( code, amount ) values ( 'A', 5 );
insert into #detail ( code, amount ) values ( 'B', 2 );
insert into #detail ( code, amount ) values ( 'B', 2 );


-- I would like to somehow simplify the following two queries
insert into #head ( code, subtotal )
    select code, sum(amount)
    from #detail
    group by code

update #detail 
  set head_id = h.head_id
from #detail d
  inner join #head h on d.code = h.code

-- This is the desired end result
select * from #detail

Desired end result of detail table:

  detail_id  code  amount  head_id
  1          A     5.00    1
  2          A     5.00    1
  3          B     2.00    2
  4          B     2.00    2
  • 1 1 Answer
  • 3 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-18T20:43:23+00:00Added an answer on May 18, 2026 at 8:43 pm

    From your subject I would say this.

    Have you seen the OUTPUT clause?

    It will let you assign your ID’s to variables so you won’t need the temp tables.

    But from your body, I don’t really understand what you’re doing…

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

Sidebar

Related Questions

I have two tables Master Detail In the Master & Detail table I get
I have two tables Master Detail In the Master & Detail table, i have
I have two tables: P and PC (master/detail joined by the column Id) Table
Currently, I have two MySQL tables. First table stores relation between the friend and
Currently, I have two MySQL tables. First table stores relation between the friend and
I have two tables, a header and a detail table: header id name detail
I have two large tables -Master table A: 1.4million rows -Detail table B: 9
I am looking to make a Master Detail View. I have two tables tbFamily
I have two tables called A and B . Table A contains request details
I have two tables: table a ida valuea 1 a 2 b 3 c

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.