0

我们有一个应用程序,用户正在输入他们的时间努力。我们有两个表,Production用于输入所有生产任务的表和NonProduction用于在应用程序中输入所有非生产相关任务的表。我们还有一个UserInfo存储所有用户信息的表,例如他的 eCode、Name、Designation、RoleID、ImmediateSupervisor ECode,用户正在向其报告等。我们正在尝试以这种方式进行 sql 查询,以便任何角色 ID 大于 1 的用户都可以向下钻取并提取在他下报告的用户的生产和非生产信息,但条件是提取的报告仅具有 RoleID 1。例如,A Manager RoleID 是 4,所以他首先向下钻取并找到所有向他报告的用户(此信息在 UserInfo 表的 ImmediateSupervisor 列中)即 RoleID 3 或 RoleID 4,然后我们再次向下钻取这些用户并提取向他们报告的信息并一直持续到我们向下钻取到 ROleID 1 并且一旦我们向下钻取到该级别,

下面是Create 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

用户信息表:

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

如何进行此类查询或查询类型以将其合并到我的应用程序中?

4

2 回答 2

2

您有一个具有树结构的表,提取它的标准方法是使用递归 cte,如下所示:

;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

请注意,我还没有设法测试此代码,但我相信您可以自己解决任何小问题。

有关递归 cte:s 的更多信息。

我也忍不住注意到您的树数据结构存在一些潜在问题: ImmediateSupervisor 似乎引用了 UserECode,我可以在您发布的表结构中看到两个问题:

  1. 从 ImmediateSupervisor 到 UserECode 没有外键。这可能会导致用户的主管不存在。
  2. UserECode 不是唯一的。这可能导致多个用户使用相同的代码,这意味着每个用户有多个主管,这将真正搞砸递归 cte 查询。

我在这里没有全貌,但我会更改 ImmediateSupervisor 列以引用 PK UserId。

于 2013-01-17T09:27:06.830 回答
-1
with
ProductionCTE(CalendarDate,RoleID,UserID,UserECode,UserName,ImmediateSupervisor,NatureOfWorkName,RegionProjectName,CountyName,WorkTypeName,TaskName,VolumneProcessed,TimeSpent,Comment)
as
(
select P.CalendarDate,U.RoleID,U.UserID,U.UserECode,U.UserName,U.ImmediateSupervisor,N.NatureofWorkName,
R.RegionProjectName,C.Countyname,W.WorktypeName,T.TaskName,P.VolumeProcessed,P.Timespent,P.Comment
from production P inner join NatureOfWork N
on N.NatureofWorkID=P.natureofworkid
inner join dbo.RegionAndProjectInfo R
on R.RegionProjectID=P.RegionProjectID
inner join county C
on C.countyid=P.countyid
inner join worktype W
on W.Worktypeid=P.worktypeID
inner join task T
on T.taskid=P.TaskID
inner join UserInfo U
on U.Userid=P.userid
where P.userid=952

    union all

        select P.CalendarDate,U.RoleID,U.UserID,U.UserECode,U.UserName,U.ImmediateSupervisor,N.NatureofWorkName,
        R.RegionProjectName,C.Countyname,W.WorktypeName,T.TaskName,P.VolumeProcessed,P.Timespent,P.Comment
        from production P inner join NatureOfWork N
        on N.NatureofWorkID=P.natureofworkid
        inner join dbo.RegionAndProjectInfo R
        on R.RegionProjectID=P.RegionProjectID
        inner join county C
        on C.countyid=P.countyid
        inner join worktype W
        on W.Worktypeid=P.worktypeID
        inner join task T
        on T.taskid=P.TaskID
        inner join UserInfo U
        on U.Userid=P.userid
        inner join ProductionCTE
        on U.ImmediateSupervisor=ProductionCTE.UserECode
)
select distinct CalendarDate,RoleID,UserID,UserECode,UserName,ImmediateSupervisor,NatureOfWorkName,RegionProjectName,CountyName,WorkTypeName,TaskName,VolumneProcessed,TimeSpent,Comment from ProductionCTE order by UserECode

实施递归 CTE 解决了这个问题,现在我能够根据层次结构提取记录。

于 2013-01-18T09:12:21.757 回答