我在C#中有这个查询,我试图为所有非聚合值添加group by
,但它不起作用。
我有3张桌子:
assemblyno
,part_no
,rout_no
(唯一),wo_no
等... wo_no
(表1中相同)和job_no
,这是1到3之间的“链接”表job_no
(与第二张表格相同)与order_val
必须使用将链接rout_no
和max(order_val)
的第二个表格,在第一个表格中为第一个表格中的wo_no
提取job_no
...
我希望更清楚。
错误:
其他信息:SQL: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 ('{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);
答案 0 :(得分:0)
对于所有数据库格式都是如此,不仅仅是foxpro - 你要求最大的t3.order_val,但是你没有告诉它如何选择最大的组那么做..所以
如果你有一个人名,性别,年龄的数据
您可以使用select gender, Max(age) from mytable group by gender
找出每个性别的最大年龄。
没有该组,它不知道什么给你最大的什么值集?
通常,group by是你在
上进行那种计算的所有值答案 1 :(得分:0)
看起来你不会分享你的结构和更多信息。仅基于我们所拥有的,可能这正是您所寻找的:
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);