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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:55:19+00:00 2026-06-17T16:55:19+00:00

I need a little help wrapping my head around CURSOR. I made an aspx

  • 0

I need a little help wrapping my head around CURSOR.

I made an aspx report, it was quite simple. For each project I needed to see how many hours went on Research, Development and Internal. That first one was quite simple, I just hardcoded the solution with 3 groups and an equal 3 types of time. Boss thought that was a brilliant report so he wanted it to cover all customers. Unfortunately the next customer had 5 groups and each group had 2-7 different types of time to each.

Now I need a dynamic solution where I can specify the groups (done) and specify which types of time goes into each group (done), last I need to combine those two with the table that record hours spent.

I have a basic cursor where I can list the projects, the type of time and how much time was spent within this group of time.

What I have is:
Project A -|- Group A -|- 5
Project B -|- Group A -|- 2
Project C -|- Group A -|- 10

Project A -|- Group B -|- 1
Project B -|- Group B -|- 10
Project C -|- Group B -|- 2

Project A -|- Group C -|- 0
Project B -|- Group C -|- 3
Project C -|- Group C -|- 7

Problem is, I need the report to be
Header____Group A|Group B|Group C|Group N
Project A -|- 5 -|- 1 -|- 0 -|- x
Project B -|- 2 -|- 10 -|- 3 -|- y
Project C -|- 10 -|- 2 -|- 7 -|- z

DECLARE @iTimeTypeGroupID int

DECLARE cur CURSOR LOCAL READ_ONLY
FOR 
SELECT iRefTimeTypeGroupID
FROM tbl_TimeTypeGrouping
WHERE iRefCustomerID = @customerID

OPEN cur

FETCH NEXT FROM cur
INTO @iTimeTypeGroupID

WHILE @@FETCH_STATUS = 0
BEGIN
    SELECT PT.iRefProjectID
        , PT.iRefTimeTypeID
        , SUM(PT.decNumberOfHours) sumNumberOfHours
    FROM tbl_ProjectTransaction PT
    WHERE iRefTimeTypeID IN (
        SELECT iRefTimeTypeID 
        FROM tbl_TimeTypeGrouping 
        WHERE iRefTimeTypeGroupID = @iTimeTypeGroupID
            AND iRefCustomerID = @customerID)
    GROUP BY PT.iRefProjectID
        , PT.iRefTimeTypeID

    FETCH NEXT FROM cur
    INTO @iTimeTypeGroupID
END

CLOSE cur
DEALLOCATE cur
  • 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-17T16:55:20+00:00Added an answer on June 17, 2026 at 4:55 pm

    I am not sure about the column names. So in this example I expect the tbl_ProjectTransaction to have a column called projectName and tbl_TimeTypeGrouping to have a GroupName. Like someone commeted in the question you should not use a cursor in this case. You should use a dynamic pivot. Here is and example:

    Get the names of the groups like this:

    DECLARE @cols VARCHAR(MAX)
    SELECT @cols=STUFF
    (
        (
            SELECT 
                ',' +QUOTENAME(tbl_TimeTypeGrouping.sGroupName) -- ????
            FROM
                tbl_TimeTypeGrouping
            FOR XML PATH('')
        )
    ,1,1,'')
    

    This will give you:

    '[Group A],[Group B],[Group C],[Group N]'
    

    And then do a dynamic pivot like this:

    DECLARE @query NVARCHAR(4000)=
    N'SELECT
        *
    FROM
    (
        SELECT
            tbl_ProjectTransaction.sProjectName, -- ????
            tbl_ProjectTransaction.decNumberOfHours,
            tbl_TimeTypeGrouping.sGroupName -- ???
        FROM
            tbl_ProjectTransaction
            JOIN tbl_TimeTypeGrouping
                ON tbl_ProjectTransaction.iRefTimeTypeID=tbl_TimeTypeGrouping.iRefTimeTypeID
    ) AS SourceTable
    PIVOT
    (
        SUM(decNumberOfHours)
        FOR GroupName IN ('+@cols+')
    ) As Pvt'
    EXECUTE(@query)
    

    References:

    • Using PIVOT and UNPIVOT
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm playing around with MySQL stored procedures and I need a little help wrapping
I need a little help. I'm trying to make a little project in MVC
I need a little help with an IRC bot I'm creating (very simple). <?php
I need a little help with (probably) something really simple. I want to use
I need a little help, I have a report that I am running that
I need little help for url rewriting in wordpress. basically what i need: mydomain.com/mysomewordpresspost
I would need little help here. I'm trying to get the git respository from
Need a little help with a SQL / ActiveRecord query. Let's say I have
Need a little help with my jquery here I want all my button with
I need a little help with an if statement in php. I'm trying to

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.