我在SSRS 2008报告中使用了这个巨大的sql select语句。我需要Product表中的字段“prod_desc1”,但也需要来自Order_Line的相同命名字段。我给他们两个别名,但我仍然得到错误。这是我的陈述:
SELECT order_num, quote_num, cust_num, cust_desc, ship_via_desc, whse_desc, slsm_desc, ar_term_desc, vend_desc, pline_desc, major_grp, cust_po_num, ord_date, req_date, shp_date, ord_type, tot_ord_$, pline_num, prod_num, price_ext, STATUS, prod_desc1 as prod_desc, prod_desc1 as com1, prod_desc2 as com2, net_price, seq_num
FROM (SELECT o.order_num, o.quote_num, o.cust_num, c.cust_desc, s.ship_via_desc, w.whse_desc, sa.slsm_desc, t.ar_term_desc, v.vend_desc, ca.pline_desc, ca.major_grp, o.cust_po_num, o.ord_date, o.req_date, o.shp_date, o.ord_type, o.tot_ord_$, ol. pline_num, ol.prod_num, price_ext, 'Open' AS STATUS, p.prod_desc1, ol.prod_desc1, ol.prod_desc2, ol.net_price, seq_num
FROM [ORDER] o
left outer join order_line ol on o.order_num = ol.order_num
left outer join product p on ol.prod_num = p.prod_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.slsm2_num = sa.slsm_num
left outer join terms t on o.ar_term_num = t.ar_term_num
left outer join vend v on ol.prim_vend_num = v.vend_num
left outer join category ca on ol.pline_Num = ca.pline_Id
WHERE (@OrderNum is null OR o.order_num = (@OrderNum)) and
o.cust_num IN (@CustomerNumber) and
(ol.prim_vend_num IN (@VendNum) or ol.prim_vend_num is NULL) and
o.req_date between (@ReqDateFrom) and (@ReqDateTo) and
o.ord_date between (@EntDateFrom) and (@EntDateTo) and
(c.slsm_num = (@SlsmnRealNum) and ol.major_grp IN ('C', 'R', 'W') or c.slsm2_num = (@SlsmnRealNum) and ol.major_grp IN ('P', 'S')) and
ol.major_grp IN (@MajorGrp) and
ol.pline_num IN (@ProductLine) and
(@PONum is null OR o.cust_po_num LIKE ('%' + @PONum + '%')) and
(@ItemNum is null OR ol.prod_num = (@ItemNum))
UNION ALL
SELECT o.order_num, o.quote_num, o.cust_num, c.cust_desc, s.ship_via_desc, w.whse_desc, sa.slsm_desc, t.ar_term_desc, v.vend_desc, ca.pline_desc, ca.major_grp, o.cust_po_num, o.ord_date, o.req_date, o.shp_date, o.ord_type, o.tot_ord_$, ol.pline_num, ol.prod_num, net_ext, 'Closed' AS STATUS, p.prod_desc1, ol.prod_desc1, ol.prod_desc2, ol.net_price, seq_num
FROM ORDER_HISTORY o
left outer join order_history_line ol on o.order_num = ol.order_num
left outer join product p on ol.prod_num = p.prod_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.slsm2_num = sa.slsm_num
left outer join terms t on o.ar_term_num = t.ar_term_num
left outer join vend v on ol.prim_vend_num = v.vend_num
left outer join category ca on ol.pline_Num = ca.pline_Id
WHERE (@OrderNum is null OR o.order_num = (@OrderNum)) and
o.cust_num IN (@CustomerNumber) and
(ol.prim_vend_num IN (@VendNum) or ol.prim_vend_num is NULL) and
o.req_date between (@ReqDateFrom) and (@ReqDateTo) and
o.ord_date between (@EntDateFrom) and (@EntDateTo) and
(c.slsm_num = (@SlsmnRealNum) and ol.major_grp IN ('C', 'R', 'W') or c.slsm2_num = (@SlsmnRealNum) and ol.major_grp IN ('P', 'S')) and
ol.major_grp IN (@MajorGrp) and
ol.pline_num IN (@ProductLine) and
(@PONum is null OR o.cust_po_num LIKE ('%' + @PONum + '%')) and
(@ItemNum is null OR ol.prod_num = (@ItemNum))) as a
WHERE (STATUS = CASE WHEN @OrderType = 'Both' THEN STATUS ELSE @OrderType END) ORDER BY cust_desc, req_date, vend_desc
任何帮助将不胜感激! :)