1

我试图有一个这样的表:

Manager     Expert          Adminis. Staff
Dept        A.P. | C.P.     A.P. | C.P.     A.P. | C.P.
Management      1   1       1   0              1    1   
Accounting      1   1       1   1              0    0
IT              1   1       2   1              0    0

AP = 可用位置编号 CP = 当前位置编号

AP 来自名为 Positions 的表:

[Positions](
    [id] [numeric](19, 0) IDENTITY(1,1) NOT NULL,   
    [NumOfAvailablePositions] [int] NULL,
    [PositionType] [varchar](255) NULL, 
    [DepartmentId] [numeric](19, 0) NULL
    )

CP 是 Title 表中 Title_Names 的计数:

[Title](
    [id] [numeric](19, 0) IDENTITY(1,1) NOT NULL,   
    [Title_Name] [varchar](50) NOT NULL,    
    [Department_Id] [numeric](19, 0) NULL,  
    [Position_Type] [varchar](255) NULL,
    )

总之,我试图得到这两个查询的结果:

SELECT   DepartmentId, PositionType, NumOfAvailablePositions  
FROM Positions 

SELECT Department_Id, Position_Type, Count (Position_Type) as CurrentPositions
FROM Title group by Department_Id, Position_Type

并将结果连接到一个表中(如上所述)。

我尝试使用以下脚本加入表格:

SELECT Department_Id, PositionType, Count(Position_Type) as PositionCountTitle, Positions.DepartmentId, Positions.NumOfAvailablePositions  
FROM Title, Positions 
GROUP BY Position_Type, Department_Id, Positions.DepartmentId, Positions.NumOfAvailablePositions 
HAVING Title.Department_Id = Positions.DepartmentId

但它产生了错误的结果。你有什么建议可以在 Jasper IReports 中解决这个问题吗?

4

2 回答 2

2

尝试FROM Title, Positions

FROM Title INNER JOIN Positions ON Title.[Position_Type]=Positions.[Position_Type]
于 2012-07-03T14:42:57.683 回答
0

可能有一种方法可以在不使用子查询的情况下做到这一点,但我发现这通常是了解查询的良好第一步。

SELECT title.Department_Id,
       (SELECT NumOfAvailablePositions FROM POSITIONS WHERE Department_Id = position.Department_Id AND PositionType = 'Expert') as expert_ap,
       (SELECT COUNT(*) FROM Title WHERE Department_Id = title.Department_Id AND PositionType = 'Expert') as expert_cp,
       (SELECT NumOfAvailablePositions FROM POSITIONS WHERE Department_Id = position.Department_Id AND PositionType = 'Adminis. Staff') as admin_ap,
       (SELECT COUNT(*) FROM Title WHERE Department_Id = title.Department_Id AND PositionType = 'Adminis. Staff') as admin_cp,
       (SELECT NumOfAvailablePositions FROM POSITIONS WHERE Department_Id = position.Department_Id AND PositionType IS NULL) as rest_ap,
       (SELECT COUNT(*) FROM Title WHERE Department_Id = title.Department_Id AND PositionType IS NULL) as rest_cp,
FROM Title title, Positions position
WHERE Title.Department_Id = Positions.DepartmentId

但是,此查询存在一个固有问题,如果 Positions 表有 Title 表没有的部门,则永远不会在查询中返回。如果您的情况不是这种情况,那么这不是问题。

于 2012-07-03T14:50:53.063 回答