简体   繁体   English

查看多个记录,其中每个发票合计预计有一个记录-SQL Server

[英]Seeing multiple records where one record per invoice is expected in an aggregate - SQL Server

I'm a CTO for a small manufacturing firm, obviously doing some very hands on stuff and am grateful to experts out there providing their time to people less experienced. 我是一家小型制造公司的CTO,显然是在做一些非常实际的事情,并感谢那里的专家为没有经验的人们提供时间。

I'm trying to construct a query for SQL Server to aggregate time for each machine in a manufacturing process. 我正在尝试构造一个SQL Server查询,以汇总制造过程中每台计算机的时间。

The results I get look something like: 我得到的结果看起来像:

InvoiceID Min DateIn      MachineName  MachineID
17993     12  2012-09-28  Beam Saw     1
17993     26  2012-09-28  Beam Saw     1
17993     17  2012-10-02  Edge Banding 3
17993     21  2012-10-02  Skipper      4
17993     23  2012-10-03  Shipping     5

etc for next invoice. 等下一张发票。

My code: 我的代码:

SELECT i.InvoiceID
    ,SUM(DATEPART(Minute,(wf.DateTimeOut - wf.DateTimeIn))) AS [Min]
    ,CAST(wf.DateTimeIn AS DATE) AS [DateIn]
    ,m.MachineName
    ,wf.MachineID
FROM Workflow wf 
    JOIN
        InvoicesPerJobPeriod ipjp
        ON
        ipjp.Workflowid = wf.ID
    JOIN Machines m
        ON 
        m.MachineID = wf.MachineID
    JOIN
        Invoices i
        ON ipjp.InvoiceID = i.InvoiceID
WHERE (i.InActive = '0') 
GROUP BY i.InvoiceID, m.MachineName, wf.MachineID, wf.DateTimeIn
ORDER BY i.InvoiceID, wf.MachineID

I don't understand why the grouping shows multiple records for the Beam Saw. 我不明白为什么分组会显示光束锯的多个记录。 Ideally, there should be one record per machine, per invoice. 理想情况下,每台机器每张发票应有一个记录。 The Datetimein is usually going to be the same day, but I need to display it, just in case a manufacturing step spans days. Datetimein通常是同一天,但是我需要显示它,以防制造步骤跨越几天。 In that case I would reasonable to have multiple records for a machine. 在那种情况下,我会为一台机器设置多个记录。

Thank you so much for your help. 非常感谢你的帮助。 It's greatly appreciated. 非常感谢。

Cliff 悬崖

If wf.DateTimeIn is different at all (in either the date or the time components) for two different Beam Saw records, they will generate separate lines in the GROUP BY clause, even though your non-aggregated usage in the SELECT statement is only referring to the date portion. 如果wf.DateTimeIn对于两个不同的Beam Saw记录完全不同(在日期或时间部分),即使您在SELECT语句中的非汇总用法仅引用了它们,它们也会在GROUP BY子句中生成单独的行。到日期部分。

If what you want is to group by the date portion of DateTimeIn only, you should write something like this: 如果要仅按DateTimeIn的日期部分分组,则应编写如下内容:

GROUP BY i.InvoiceID, m.MachineName, wf.MachineID, CAST(wf.DateTimeIn AS DATE)

This will ensure that any number of Beam Saw records that otherwise match and are entered on the same date will be grouped together into a single row. 这将确保在同一日期输入的任何数量的其他匹配的Beam Saw记录都将被分组为一行。

However, this may change the semantics of your aggregate statement, so you'll need to verify the query works as you desire after you make this change. 但是,这可能会更改聚合语句的语义,因此在进行此更改后,您需要验证查询是否按需工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM