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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:07:08+00:00 2026-06-17T11:07:08+00:00

We have a application where users are entering their time efforts .We have two

  • 0

We have a application where users are entering their time efforts .We have two tables, Production table for entering all production tasks and NonProduction for entering all non production related tasks in the application.We also have a UserInfo table where all the user information is stored like his eCode, Name, Designation,RoleID,ImmediateSupervisor ECode to whom the user is reporting etc.
We are trying to make a sql query in such manner so that any users whose roleid is greater then 1 can drill down and extract the Production and NonProduction information of the users reporting under him but the condition is that the extracted report is of RoleID 1 only.
For example, A Manager RoleID is 4 ,so firstly he drill down and find all the users reporting to him (This info is in ImmediateSupervisor column on UserInfo table)i.e RoleID 3 or RoleID 4, then again we drill down to these Users and extract information who are reporting to them and continue until we drill down up to ROleID 1 and once we drill down to that level, we extract respective users Production and NonProduction details and display in the Report.

Below is the table structure query in Create Table format;

Production Table

CREATE TABLE [dbo].[Production]
(
    [ProductionTimeEntryID]  [int] IDENTITY(100, 1) NOT NULL,
    [CalendarDate]           [datetime] NOT NULL,
    [UserID]                 [int] NOT NULL,
    [NatureOfWorkID]         [int] NOT NULL,
    [RegionProjectID]        [int] NOT NULL,
    [CountyID]               [int] NOT NULL,
    [WorkTypeID]             [int] NOT NULL,
    [TaskID]                 [int] NOT NULL,
    [VolumeProcessed]        [int] NOT NULL,
    [NosOfError]             [int] NULL,
    [NosOfVolumeAudited]     [int] NULL,
    [TimeSpent]              [varchar](25) NULL,
    [Comment]                [varchar](250) NULL,
    [IsTaskCompleted]        [int] NOT NULL,
    [isCurrentDayTask]       [bit] NOT NULL,
    [SupervisorECode]        [nvarchar](50) NULL,
    CONSTRAINT [PK_Production] PRIMARY KEY CLUSTERED([ProductionTimeEntryID] ASC)
    WITH (
        PAD_INDEX = OFF,
        STATISTICS_NORECOMPUTE = OFF,
        IGNORE_DUP_KEY = OFF,
        ALLOW_ROW_LOCKS = ON,
        ALLOW_PAGE_LOCKS = ON
    ) ON [PRIMARY]
) ON [PRIMARY]
    GO

SET ANSI_PADDING OFF
    GO

    ALTER TABLE [dbo].[Production]  WITH NOCHECK ADD  CONSTRAINT 
[FK_Production_County] FOREIGN KEY([CountyID])
    REFERENCES [dbo].[County] ([CountyID])
    GO

    ALTER TABLE [dbo].[Production] CHECK CONSTRAINT [FK_Production_County]
    GO

    ALTER TABLE [dbo].[Production]  WITH NOCHECK ADD  CONSTRAINT 
[FK_Production_NatureOfWork] FOREIGN KEY([NatureOfWorkID])
    REFERENCES [dbo].[NatureOfWork] ([NatureOfWorkID])
    ON 
UPDATE CASCADE
       ON

DELETE CASCADE
    GO

    ALTER TABLE [dbo].[Production] CHECK CONSTRAINT [FK_Production_NatureOfWork]
    GO

    ALTER TABLE [dbo].[Production]  WITH NOCHECK ADD  CONSTRAINT 
[FK_Production_RegionAndProjectInfo] FOREIGN KEY([RegionProjectID])
    REFERENCES [dbo].[RegionAndProjectInfo] ([RegionProjectID])
    GO

    ALTER TABLE [dbo].[Production] CHECK CONSTRAINT 
[FK_Production_RegionAndProjectInfo]
    GO

    ALTER TABLE [dbo].[Production]  WITH CHECK ADD  CONSTRAINT 
[FK_Production_Task] FOREIGN KEY([TaskID])
    REFERENCES [dbo].[Task] ([TaskID])
    GO

    ALTER TABLE [dbo].[Production] CHECK CONSTRAINT [FK_Production_Task]
    GO

    ALTER TABLE [dbo].[Production]  WITH CHECK ADD  CONSTRAINT 
[FK_Production_USERINFO] FOREIGN KEY([UserID])
    REFERENCES [dbo].[USERINFO] ([UserID])
    ON 
UPDATE CASCADE
       ON

DELETE CASCADE
    GO

    ALTER TABLE [dbo].[Production] CHECK CONSTRAINT [FK_Production_USERINFO]
    GO

    ALTER TABLE [dbo].[Production]  WITH CHECK ADD  CONSTRAINT 
[FK_Production_WorkType] FOREIGN KEY([WorkTypeID])
    REFERENCES [dbo].[WorkType] ([WorkTypeID])
    ON 
UPDATE CASCADE
       ON

DELETE CASCADE
    GO

    ALTER TABLE [dbo].[Production] CHECK CONSTRAINT [FK_Production_WorkType]
    GO

    ALTER TABLE [dbo].[Production] ADD  DEFAULT((0)) FOR [IsTaskCompleted]
    GO

    ALTER TABLE [dbo].[Production] ADD  DEFAULT((0)) FOR [isCurrentDayTask]
    GO

UserInfo Table:

CREATE TABLE [dbo].[USERINFO](
[UserID] [int] IDENTITY(1,1) NOT NULL,
[UserECode] [nvarchar](50) NOT NULL,
[UserName] [nvarchar](250) NOT NULL,
[CCCode] [nvarchar](50) NULL,
[CCName] [nvarchar](50) NULL,
[Password] [varchar](50) NULL,
[IsFlagEnabled] [bit] NULL,
[IsFirstTimeUserLoggedIn] [bit] NULL,
[EmailAddress] [nvarchar](250) NULL,
[Designation] [varchar](50) NULL,
[ShiftStartTime] [varchar](8) NULL,
[ShiftEndTime] [varchar](8) NULL,
[WeekendShiftStartTime] [varchar](8) NULL,
[WeekendShiftEndTime] [varchar](8) NULL,
[RoleID] [int] NULL,
[ShiftEndFlagStatus] [int] NOT NULL,
[ShiftStartTimeWithTimeStamp] [datetime] NULL,
[ShiftEndTimeWithTimeStamp] [datetime] NULL,
[LoggedInDateTime] [datetime] NULL,
[FirstLoggedInOnthedayflag] [int] NOT NULL,
[ImmediateSupervisor] [nvarchar](50) NULL,
 CONSTRAINT [PK_USER] PRIMARY KEY CLUSTERED 
(
    [UserID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[USERINFO] ADD  DEFAULT ((1)) FOR [RoleID]
GO
ALTER TABLE [dbo].[USERINFO] ADD  DEFAULT ((1)) FOR [ShiftEndFlagStatus]
GO
ALTER TABLE [dbo].[USERINFO] ADD  DEFAULT ((1)) FOR [FirstLoggedInOnthedayflag]
GO

How to make such query or type of query to incorporate same in my application?

  • 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-17T11:07:10+00:00Added an answer on June 17, 2026 at 11:07 am

    You have a table with a tree structure, the standard way to extract this is to use a recursive cte, something like this:

    ;with UserTree as
    (
        select UserECode, UserID
          from USERINFO
         where UserID = <the managers user-id>
        union all
        select UserECode, UserID
          from UserTree parent
               inner join USERINFO child
                       on child.ImmediateSupervisor = parent.UserECode
    )
    select * --change this to the specific columns that you need
      from UserTree ut
           inner join Production p
                   on p.UserID = ut.UserID
     where RoleId = 1
    

    Please note that I haven’t managed to test this code, but I’m sure you can fix any minor issues yourself.

    More information about recursive cte:s.

    I also couldn’t help but notice a couple of potential issues with your tree datastructure: ImmediateSupervisor appears to reference UserECode, and I can see two problems in the table structure that you have posted:

    1. No foreign key from ImmediateSupervisor to UserECode. This could result in users that have non-existing supervisors.
    2. UserECode is not unique. This could result in multiple users with the same code, which means multiple supervisors per user, and that will really screw up the recursive cte query.

    I don’t have the whole picture here, but with what I have I would change the ImmediateSupervisor-column to refer to the PK UserId instead.

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

Sidebar

Related Questions

Problem Overview: My application has an enrollment form. Users have a habit of entering
I have an application where users select their own display columns. Each display column
I have a silverlight application which users will be running in various time zones
I have an issue with a task management application where occasionally users close their
We have an application where users enter prices all day. These prices are recorded
I have users entering data in SharePoint (Running on SQL Server), but my application
I have an application with Users, Posts and Comments. Users has_many Posts, Posts has_many
I have an application where users can store items. Now each type of account
I have an application with Users, Machines, and Tests. Users have many Machines and
In my web application users have the ability to upload games which are then

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.