如何从该 sql 查询中消除子查询?

发布于 2024-12-15 20:21:34 字数 726 浏览 1 评论 0原文

我有一个应该可以工作的查询,但似乎我是糟糕的数据库技术的受害者。我需要在 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

I have a query which should work, but it seems I am a victim of a poor database technology. I need to run the query below on a Pervasive SQL database. The manufacturer of the product using Pervasive tells me the version they are using is 10 which should support subqueries, but I have yet to be able to run even a simple subquery. So, I am wondering if the following query can be rewritten to eliminate the subquery:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒人说梦 2024-12-22 20:21:34

在标准 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

In standard SQL you can do this, though I have no idea about PervasiveSQL specifically...

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
不念旧人 2024-12-22 20:21:34

不是 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

Not a Pervasive SQL guy, but would this work? Not sure it accomplishes your query goals:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文