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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:49:09+00:00 2026-05-11T16:49:09+00:00

I’ll begin by admitting that my problem is most likely the result of bad

  • 0

I’ll begin by admitting that my problem is most likely the result of bad design since I can’t find anything about this elsewhere. That said, let’s get dirty.

I have an Activities table and an ActivitySegments table. The activities table looks something like:

activityid (ident) | actdate (datetime) | actduration (datetime) | ticketnumber (numeric) |

ActivitySegments looks something like

segmentid (ident) | ticketid (numeric) | activityid (numeric) | startdate | starttime | enddate | endtime

This is a time tracking function of an intranet. The “old way” of doing things is just using the activity table. They want to be able to track individual segments of work throughout the day with a start/stop mechanism and have them roll up into records in the activities table. The use case is the user should be able to select any/all segments that they’ve worked on that day and have them be grouped by ticketid and inserted into the activity table. I have that working. I’m sending a string of comma separated values that correspond to segmentids to a sproc that puts them in a temp table. So I have the above two tables and a temp table with one column of relevant segmentids. Can’t they all just get along?

What I need is to take these passed activitysegment Ids, group them by ticket number and sum the duration worked on each ticket (I already have the sql for that). Then insert this dataset into the activities table BUT also get the new activityid @@identity and update the activitiessegments table with the appropriate value.

In procedural programming I’d for loop the insert, get the @@identity and do something else to figure out which segmentids went into creating that activityid. I’m pretty sure I’m thinking about this all wrong, but the deadline approaches and I’ve been staring at SQL management studio for two days, wasted sheets of paper and burned through way too many cigarettes. I see SQL for Smarties in my near future, until then, can someone help me?

  • 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-11T16:49:10+00:00Added an answer on May 11, 2026 at 4:49 pm

    try this approach:

    declare @x table (tableID int not null primary key identity (1,1), datavalue varchar(10) null)
    INSERT INTO @x values ('one')
    INSERT INTO @x values ('aaaa')
    INSERT INTO @x values ('cccc')
    
    declare @y table (tableID int not null primary key               , datavalue varchar(10) null)
    
    declare @count int ---------------FROM HERE, see comment
    set @count=5;
    WITH hier(cnt) AS
            (
            SELECT  1 AS cnt
            UNION ALL
            SELECT  cnt + 1
            FROM    hier
            WHERE    cnt < @count
            ) -----------------------To HERE, see comment
    INSERT INTO @x
        (datavalue)
        OUTPUT INSERTED.tableID, INSERTED.datavalue
        INTO @y
    SELECT
        'value='+CONVERT(varchar(5),h.cnt)
        FROM hier  h
        ORDER BY cnt DESC
    
    
    select '@x',* from @x  --table you just inserted into
    select '@y',* from @y  --captured data, including identity
    

    here is output of the SELECTs

         tableID     datavalue
    ---- ----------- ----------
    @x   1           one
    @x   2           aaaa
    @x   3           cccc
    @x   4           value=5
    @x   5           value=4
    @x   6           value=3
    @x   7           value=2
    @x   8           value=1
    
    (8 row(s) affected)
    
         tableID     datavalue
    ---- ----------- ----------
    @y   4           value=5
    @y   5           value=4
    @y   6           value=3
    @y   7           value=2
    @y   8           value=1
    

    The “FROM HERE” – “TO HERE” is just a fancy way to create a table to join to, you can use your own table to join to there…

    use @y to process your updates, update from and join it in…

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

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.