-1

我有一个应该可以工作的查询,但似乎我是糟糕的数据库技术的受害者。我需要在 Pervasive SQL 数据库上运行下面的查询。使用 Pervasive 的产品制造商告诉我他们使用的版本是 10,它应该支持子查询,但我什至无法运行一个简单的子查询。所以,我想知道是否可以重写以下查询以消除子查询:

SELECT
    OuterTime.Employee,
    OuterTime.Date,        
    OuterTime.Pay_ID,        
    OuterTime.Description,        
    OuterTime.Equipment,        
    OuterTime.JC_Cost_Code,        
    OuterTime.JC_Category,        
    OuterTime.Units,  
    (   
        SELECT
            SUM(SubQueryTime.Units)
        FROM
            PRT_NEW__TIME AS SubQueryTime
        WHERE
            SubQueryTime.Employee = OuterTime.Employee
        GROUP BY
            SubQueryTime.Employee
    ) AS TotalHoursForEmp
FROM
    PRT_NEW__TIME AS OuterTime
4

2 回答 2

0

在标准 SQL 中,你可以做到这一点,虽然我不知道 PervasiveSQL 具体...

SELECT
    OuterTime.Employee,
    OuterTime.Date,        
    OuterTime.Pay_ID,        
    OuterTime.Description,        
    OuterTime.Equipment,        
    OuterTime.JC_Cost_Code,        
    OuterTime.JC_Category,        
    OuterTime.Units,
    COALESCE(TotalHoursForEmp.TotalUnits, 0)  AS TotalUnits
FROM
    PRT_NEW__TIME AS OuterTime
LEFT JOIN
    (   
        SELECT
            Employee,
            SUM(Units) AS TotalUnits
        FROM
            PRT_NEW__TIME
        GROUP BY
            Employee
    )
    AS TotalHoursForEmp
        ON TotalHoursForEmp.Employee = OuterTime.Employee
于 2011-11-15T15:39:36.417 回答
0

不是 Pervasive SQL 的人,但这行得通吗?不确定它是否能实现您的查询目标:

SELECT
    OuterTime.Employee,
    OuterTime.Date,        
    OuterTime.Pay_ID,        
    OuterTime.Description,        
    OuterTime.Equipment,        
    OuterTime.JC_Cost_Code,        
    OuterTime.JC_Category,        
    SUM(OuterTime.Units) AS TotalHoursForEmp
    GROUP BY OuterTime.Employee, OuterTime.Date, OuterTime.Pay_ID,
             OuterTime.Description, OuterTime.Equipment, OuterTime.JC_Cost_Code,
             OuterTime.JC_Category
FROM
    PRT_NEW__TIME AS OuterTime
于 2011-11-15T15:44:32.100 回答