DBF FoxPro 的缺失子句组

Missing clause group by DBF FoxPro

我在 C# 中有这个查询,我尝试为所有非聚合值添加 group by,但它不起作用。

我有 3 个 table:

  1. 首先table我有assemblynopart_norout_no(唯一)、wo_no等...
  2. 第二次我有 wo_no(与 table 1 相同)和 job_no,这是 1 和 3 之间的 "link" table
  3. 和第三个 table job_no(与第二个 table 相同)和 order_val

必须在第一个 table 中提取给定的 rout_no,第三个 table 中的 max(order_val) 使用第二个 table 将 link wo_nojob_no...
我希望更清楚。

错误:

Additional information: SQL: GROUP BY clause is missing or invalid.

        sql = string.Format(@" SELECT t1.assemblyno, t1.level, t1.wo_no, t1.rout_no, t1.due_date, t1.printed, t1.rev_no, t2.lot, t2.po, t2.qty_due, t3.comment, t3.jobno, MAX(t3.order_val), t3.part_no,t3.po,t3.price, t3.qty_order, t3.quote_no, t3.rev_no 
    FROM ('{0}') t1 
    LEFT JOIN ('{1}') t2 
    on t1.wo_no = 2.wo_no 
    LEFT JOIN ('{2}') t3 
    on t2.jobno = t3.jobno 
    GROUP BY  t1.assemblyno, t1.level, t1.wo_no, t1.rout_no, t1.due_date, t1.printed, t1.rev_no, t2.lot, t2.po, t2.qty_due, t3.comment, t3.jobno,t3.part_no,t3.po,t3.price, t3.qty_order, t3.quote_no, t3.rev_no 
WHERE t1.rout_no=" + "\'" + rNum + "\'" + ";", databaseTable_WOROUTH, databaseTable_WOJOBS, databaseTable_SOMAST);

这对所有数据库格式都是正确的,而不仅仅是 foxpro - 你要求最大 t3.order_val,但你没有告诉它如何选择最大的组。所以

如果你的数据库 person_name,性别,年龄

您可以通过 select gender, Max(age) from mytable group by gender 找到每个性别的最大年龄。

没有 group by 它不知道给你什么值的最大值?

通常,分组依据是您未在

上进行此类计算的所有值

看来您不会分享您的结构和更多信息。仅根据我们所拥有的,这可能就是您正在寻找的:

    sql = string.Format(@" SELECT 
        t1.assemblyno, t1.level, t1.wo_no, t1.rout_no, 
        t1.due_date, t1.printed, t1.rev_no, t2.lot, t2.po, t2.qty_due, 
        t3.comment, t3.jobno, MAX(t3.order_val), t3.part_no,t3.po,t3.price, t3.qty_order,
        t3.quote_no, t3.rev_no 
        FROM ('{0}') t1 
        LEFT JOIN ('{1}') t2  on t1.wo_no = 2.wo_no 
        LEFT JOIN (
    select * from ('{2}') tmp1
    inner join  
    (select jobNo, max(order_val) as order_val from ('{2}') group by jobNo) tmp2
    on tmp1.JobNo = tmp2.JobNo and tmp1.Order_Val=tmp2.Order_val
        ) t3 
        on t2.jobno = t3.jobno 
    WHERE t1.rout_no=?", databaseTable_WOROUTH, databaseTable_WOJOBS, databaseTable_SOMAST);

//cmd.Parameters.Add("@rnum", OleDbType.Char).Value = rnum;
//...

// 感谢 BASOZ 的想法和帮助

var sql = string.Format(@"
select top 1 
    t1.assemblyno, t1.level, t1.wo_no, t1.rout_no, t1.due_date, t1.printed, t1.rev_no, t2.lot, t2.po, t2.qty_due, 
    t3.comment, t3.jobno, t3.order_val, t3.part_no,t3.po,t3.price, t3.qty_order,t3.quote_no, t3.rev_no 
from {2} t3
    inner join {1} t2 on t3.jobno = t2.jobno
    inner join {0} t1 on t1.wo_no = t2.wo_no 
    where t1.rout_no=?
        and t3.order_val in (
        SELECT max(tt3.order_val) 
                            FROM {0} tt1 
                            left join {1} tt2 
                                on tt1.wo_no = tt2.wo_no
                            left join {2} tt3 
                                on tt2.jobno = tt3.jobno
                            where tt1.rout_no = ?
                            group by tt1.rout_no
        )
        {3} {4}
        order by t3.jobno desc ", "t1", "t2", "t3", startCheck, endCheck);