在一对多关系中进行内连接时只需要一行

时间:2013-10-15 22:19:32

标签: sql

我确信他们是我问题的简单答案。

我有2个表 - 一个订单表头(每个订单一行,称为“订单”)和一个订单明细表(每个订单头有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记录,我怎样才能让查询只返回一行?

1 个答案:

答案 0 :(得分: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没有像您在Oracle,MySQL,PostgreSQL,SQLite等中找到的limit子句。我发现了看起来像SELECT TOP 1...的东西,但我不知道那是否相同。任何读这篇文章寻找类似答案的人,显然你必须用LIMIT 1替换那个在特定DBMS中语法正确的东西。让我知道表达行数限制的正确方法。