3

我相信它们是我问题的简单答案。

我有 2 个表 - 一个订单标题表(每个订单一行,称为“Order”)和一个订单详细信息表(每个订单标题 1 行或多行,称为“Order_Line”)。我正在使用 Sql Server2005 和他们的 SSRS 报告工具。我只想取回一行,但我得到了每个订单的详细信息表中有多少行。我需要加入明细表以获取我想与其他订单标题级别项目一起显示的值。

我尝试选择不同但得到相同的结果。我的查询是:

select o.order_num, o.cust_num, c.cust_desc, o.shipto_num, o.st_name, o.st_address1, o.ent_date, 
o.ord_date, o.req_date, ol.order_line, ol.seq_num, u.user_desc, ol.orig_inv_num, ol.prod_num,
p.prod_desc1, ol.prod_desc1, ol.prod_desc2, ol.grs_price, ol.disc_pct, ol.net_price, ol.ord_qty,
ol.opn_qty, ol.pck_qty, ol.bo_qty, ol.shp_qty, rs.rsn_code_desc, ol.supp_cde, ol.line_ctr,
v.vend_desc, ca.pline_desc, ca.major_grp, ol.major_grp, w.whse_desc, o.wb,s.ship_via_desc,       
sa.slsm_desc, t.ar_term_desc,
o.shp_date, o.sys_num, o.inv_Flag, o.inv_num, o.tot_ord_$,o.inv_date, o.ord_src, r.route_desc,
o.cred_type, o.cred_num, o.total_sale
from [order] o
left outer join order_line ol on o.order_num = ol.order_num
left outer join customer c on o.cust_num = c.cust_no
left outer join ship_via s on o.ship_via_num = s.ship_via_id
left outer join whse_addr w on o.shp_whse = w.whse_num
left outer join salesman sa on o.slsm_num = sa.slsm_num
left outer join terms t on o.ar_term_num = t.ar_term_num
left outer join [route] r on o.route_num = r.route_num
left outer join product p on ol.prod_num = p.prod_num
left outer join prc_ov_rsn rs on ol.prc_ov_rsn_code = rs.rsn_cod
left outer join vend v on ol.prim_vend_num = v.vend_num
left outer join [user_id] u on ol.[user_id] = u.[user_num] 
left outer join category ca on ol.pline_Num = ca.pline_Id
where o.cust_num = '606797'
order by o.cust_num, o.shipto_num, o.order_num

无论每个订单有多少 Order_Line 记录,我怎样才能让查询只返回一行?

4

1 回答 1

0

我将尝试从一般 SQL 的角度回答这个问题,因为我不了解 SQL Server 本身。希望这是有道理的,但如果我完全关闭,请告诉我,我会杀死这个答案。

您可以尝试解决此问题的一种方法是创建一个依赖子查询,您可以在其中使用LIMIT 1. 因此,连接最终将只匹配一个订单明细行。像这样:

select ...all columns you want...
from [order] o
left outer join order_line ol on o.order_num = ol.order_num
left outer join customer c on o.cust_num = c.cust_no
left outer join ship_via s on o.ship_via_num = s.ship_via_id
left outer join whse_addr w on o.shp_whse = w.whse_num
left outer join salesman sa on o.slsm_num = sa.slsm_num
left outer join terms t on o.ar_term_num = t.ar_term_num
left outer join [route] r on o.route_num = r.route_num
left outer join product p on ol.prod_num = p.prod_num
left outer join prc_ov_rsn rs on ol.prc_ov_rsn_code = rs.rsn_cod
left outer join vend v on ol.prim_vend_num = v.vend_num
left outer join [user_id] u on ol.[user_id] = u.[user_num] 
left outer join category ca on ol.pline_Num = ca.pline_Id
where o.cust_num = '606797' AND order_line.order_num =
    (SELECT order_num 
     FROM order_line 
     WHERE order_line.order_num = order.order_num LIMIT 1)
order by o.cust_num, o.shipto_num, o.order_num

结果是您将分别获得每个订单行以及您从 order_line 表中选择的列,但是由于您将其限制为 1 个 order_line 行,因此您应该在结果集中每个订单只获得一行。

通过适当的索引等,这有望不会导致任何显着的额外加载时间。让我知道这是否有意义。

注意:显然 SQL Server 没有limit像您在 Oracle、MySQL、PostgreSQL、SQLite...等中找到的那样的子句。我找到了对看起来像的东西的引用SELECT TOP 1...,但我不知道这是否是等价的. 任何阅读此内容以寻找类似答案的人,显然您必须将 替换LIMIT 1为该特定 DBMS 中语法正确的任何内容。并让我知道表达行数限制的正确方法。

于 2013-10-15T22:36:27.637 回答