SQL Server : SUM DISTINCT GROUP BY - sql

I'm currently creating a query to sum the code below
select distinct
opch.docentry as 'AP Invoice #',
pch1.linenum as 'AP Invoice Line #',
pch1.itemcode as 'Item Code',
pch1.dscription as 'Item Name',
pch1.freetxt as 'Free Text',
pch1.priceafvat as 'Gross Price',
pch1.quantity as 'Quantity',
pch1.gtotal as 'Gross Total'
from
opch
inner join
pch1 on opch.docentry = pch1.docentry
inner join
opdn on opdn.docentry = pch1.basedocnum
inner join
pdn1 on opdn.docentry = pdn1.docentry
inner join
opor on opor.docentry = pdn1.basedocnum
inner join
por1 on opor.docentry = por1.docentry
where
pch1.u_budgetno = '57'
and opch.canceled = 'N'
and opdn.docstatus = 'C'
and opdn.canceled = 'N'
and opor.docstatus = 'C'
and opor.canceled = 'N'
order by
opch.docentry
This is the sum statement I've tried
select
opch.docentry,
sum(pch1.gtotal)
from
opch
inner join
pch1 on opch.docentry = pch1.docentry
inner join
opdn on opdn.docentry = pch1.basedocnum
inner join
pdn1 on opdn.docentry = pdn1.docentry
inner join
opor on opor.docentry = pdn1.basedocnum
inner join
por1 on opor.docentry = por1.docentry
where
pch1.u_budgetno = '57'
and opch.canceled = 'N'
and opdn.docstatus = 'C'
and opdn.canceled = 'N'
and opor.docstatus = 'C'
and opor.canceled = 'N'
group by
opch.docentry, opdn.docstatus
It however doesn't return the correct amount as it duplicates some answers based on the connected tables.
Requesting assistance on what I'm doing wrong.
*Update
This is the result from the select distinct query
enter image description here
I'm aiming for the sum query to sum the results from the pch1.gtotal column
Thank you.

The problem is in the join you are doing between the invoice and the delivery note. You are not joining the line so it multiplies the results. The correct join is done using BaseEntry, BaseLine and BaseType. The same applies to the join with the purchase order.
select
opch.docentry,
sum(pch1.gtotal)
from
opch
inner join
pch1 on opch.docentry = pch1.docentry
inner join
pdn1 on pdn1.docentry = pch1.baseentry and pdn1.linenum = pch1.baseline and pdn1.objtype = pch1.basetype
inner join
opdn on opdn.docentry = pdn1.docentry
inner join
por1 on por1.docentry = pdn1.baseentry and por1.linenum = pdn1.baseline and por1.objtype = pdn1.basetype
inner join
opor on opor.docentry = por1.docentry
where
pch1.u_budgetno = '57'
and opch.canceled = 'N'
and opdn.docstatus = 'C'
and opdn.canceled = 'N'
and opor.docstatus = 'C'
and opor.canceled = 'N'
group by
opch.docentry

Related

Put two queries into one query - Query - SQL - SAP B1

I have a very simple Inventory in Warehouse query, and now I need to do a sum in the IsCommitted column with another query that I have called "Set Demand".
Like this = Sum( [IsCommited] + "Set Demand Query qty")
Warehouse query
SELECT T0.[ItemCode] AS 'Item No.',
T0.[WhsCode] AS 'Warehouse Code',
T0.[OnHand] AS 'In Stock',
T0.[IsCommited] AS 'Committed',
T0.[MinStock] AS 'Minimum Inventory',
T0.[MaxStock] AS 'Maximum Inventory',
T1.[ItmsGrpCod] AS 'Itemcode',
T2.Price AS 'StandardCost'
FROM [OITW] T0 INNER JOIN [OITM] T1 ON T0.ItemCode = T1.ItemCode LEFT JOIN [ITM1] T2 ON T1.ItemCode = T2.ItemCode and T2.PriceList = 26
WHERE (T0.[WhsCode] = (N'9500' )) AND (T1.[ItmsGrpCod] = (N'100' )) AND T0.[OnHand] > 0
Set Demand query.
SELECT T3.[ItemCode] ,
(-T3.[OnHand] + T1.[Quantity]) as 'Set Demand'
FROM [OITT] T0 WITH (NOLOCK) INNER JOIN [ITT1] T1 WITH (NOLOCK) ON T0.[Code] = T1.[Father], [OSRI] T2 WITH (NOLOCK), [OITW] T3 WITH (NOLOCK) INNER JOIN [OITM] T4 WITH (NOLOCK) ON T3.[ITEMCode] = T4.[ItemCode]
WHERE T0.[Code] = T2.[ItemCode] AND T1.[Code] = T3.[ItemCode] AND T2.[IntrSerial] = T3.[WhsCode] AND T2.[Status] <> 1 and T2.[U_IsCon] <> 'YES' and T3.[OnHand] - T1.[Quantity] < 0 and substring (T2.[WhsCode],8,1)<>'C' AND T2.[WhsCode] = '9000' AND t4.[ItmsGrpCod] = 100
What Im trying to do = ( [IsCommited] + "Set Demand Query qty")
SELECT T0.[ItemCode] AS 'Item No.',
T0.[WhsCode] AS 'Warehouse Code',
T0.[OnHand] AS 'In Stock',
(T0.[IsCommited] + (SELECT (-T3.[OnHand] + T1.[Quantity])
FROM [OITT] T0 WITH (NOLOCK) INNER JOIN [ITT1] T1 WITH (NOLOCK) ON T0.[Code] = T1.[Father], [OSRI] T2 WITH (NOLOCK), [OITW] T3 WITH (NOLOCK) INNER JOIN [OITM] T4 WITH (NOLOCK) ON T3.[ITEMCode] = T4.[ItemCode]
WHERE T0.[Code] = T2.[ItemCode] AND T1.[Code] = T3.[ItemCode] AND T2.[IntrSerial] = T3.[WhsCode] AND T2.[Status] <> 1 and T2.[U_IsCon] <> 'YES' and T3.[OnHand] - T1.[Quantity] < 0 and substring (T2.[WhsCode],8,1)<>'C' AND T2.[WhsCode] = '9000' AND t4.[ItmsGrpCod] = 100) as '**Commited&SetDemand**',
T0.[MinStock] AS 'Minimum Inventory',
T0.[MaxStock] AS 'Maximum Inventory',
T1.[ItmsGrpCod] AS 'Itemcode',
T2.Price AS 'StandardCost'
FROM [OITW] T0 INNER JOIN [OITM] T1 ON T0.ItemCode = T1.ItemCode LEFT JOIN [ITM1] T2 ON T1.ItemCode = T2.ItemCode and T2.PriceList = 26
WHERE (T0.[WhsCode] = (N'9500' )) AND (T1.[ItmsGrpCod] = (N'100' )) AND T0.[OnHand] > 0
Your help will be greatly appreciated. Thank you very much!!!!! :)
Try Something like the Below, I have added the second query with a join on the Item Code.
SELECT T0.[ItemCode] AS 'Item No.',
T0.[WhsCode] AS 'Warehouse Code',
T0.[OnHand] AS 'In Stock',
T0.[IsCommited] AS 'Committed',
T0.[MinStock] AS 'Minimum Inventory',
T0.[MaxStock] AS 'Maximum Inventory',
T1.[ItmsGrpCod] AS 'Itemcode',
T2.Price AS 'StandardCost'
, t0.[IsCommited] + demand.[Set Demand]
FROM [OITW] T0 INNER JOIN [OITM] T1 ON T0.ItemCode = T1.ItemCode LEFT JOIN
[ITM1] T2 ON T1.ItemCode = T2.ItemCode and T2.PriceList = 26
Inner Join
(
SELECT T3.[ItemCode] ,
(-T3.[OnHand] + T1.[Quantity]) as 'Set Demand'
FROM [OITT] T0 WITH (NOLOCK) INNER JOIN [ITT1] T1 WITH (NOLOCK) ON T0.[Code] =
T1.[Father], [OSRI] T2 WITH (NOLOCK), [OITW] T3 WITH (NOLOCK) INNER JOIN [OITM]
T4 WITH (NOLOCK) ON T3.[ITEMCode] = T4.[ItemCode]
WHERE T0.[Code] = T2.[ItemCode] AND T1.[Code] = T3.[ItemCode] AND T2.
[IntrSerial] = T3.[WhsCode] AND T2.[Status] <> 1 and T2.[U_IsCon] <> 'YES' and T3.[OnHand] - T1.
[Quantity] < 0 and substring (T2.[WhsCode],8,1)<>'C' AND T2.[WhsCode] = '9000'
AND t4.[ItmsGrpCod] = 100
) as demand on demand.ItemCode = t0.ItemCode
WHERE (T0.[WhsCode] = (N'9500' )) AND (T1.[ItmsGrpCod] = (N'100' )) AND T0.
[OnHand] > 0
Thank you all for your answers, much appreciated.
And also sorry for my delay in answering.
I was able to solve my problem through this code.
Please look below.
What sawsine said, was exactly that, the relationship between items was missing.
SELECT
OITW.ItemCode,
SUM(CONVERT(INT, (ITT1.quantity - OITW.OnHand))) as 'Set Demand'
FROM OSRI
inner join OSRN on OSRN.ItemCode = OSRI.ItemCode AND OSRN.SysNumber = OSRI.SysSerial
INNER JOIN OITT ON OSRI.ItemCode = OITT.Code
INNER JOIN ITT1 ON ITT1.Father = OITT.Code
INNER JOIN OITW ON ITT1.Code = OITW.ItemCode AND OSRI.IntrSerial = OITW.WhsCode
inner join OWHS on OSRI.WhsCode = OWHS.WhsCode
inner join OITM on ITT1.Code = OITM.ItemCode
WHERE 1=1
AND OSRI.WhsCode = N'9000'
and OSRI.[Status] = 0 and
(OSRN.U_Condition <> N'2'  or OSRN.U_Condition is Null) and
OITM.ItmsGrpCod IN (100, 102, 104) and
(LEN(ISNULL(OITM.CardCode,'''')) = 0 or ISNULL(OITM.CardCode,'''') = 'NL20')
AND (ITT1.quantity - OITW.OnHand) > 0
Group by OITW.ItemCode

SQL Statement Help Needed:Query within a Query

I am working on a query where I need to compile a list of of members who have a particular group of products associated with their, from a particular site and then for each member in that list retrieve all of the products on their account.
The first piece of the query is this:
SELECT
MEMBERS.memid
FROM
SITES
INNER JOIN
MEMBERS ON SITES.siteid = MEMBERS.siteid
INNER JOIN
SS ON MEMBERS.memid = SS.memid
INNER JOIN
PRODUCTCATS
INNER JOIN
PRODUCTS ON PRODUCTCATS.productcatid = PRODUCTS.productcatid
ON SS.productid = PRODUCTS.productid
INNER JOIN
EMPLOYEES ON SS.employeeid = EMPLOYEES.employeeid
WHERE
(PRODUCTS.productcatid = 77)
AND (SS.initialdate BETWEEN #rvPurchaseStart AND #rvPurchaseEnd)
From that list, I then need the following query to run for each member in that list:
SELECT
SITES.sitename, MEMBERS.scancode, MEMBERS.lname,
MEMBERS.fname, MEMBERS.mtypeid, MEMBERS.status,
PRODUCTS.description, SS.initialdate,
SS.initialquantity, SS.usedquantity, SS.dateexpire,
EMPLOYEES.lname AS Expr1, EMPLOYEES.fname AS Expr2
FROM
SITES
INNER JOIN
MEMBERS ON SITES.siteid = MEMBERS.siteid
INNER JOIN
SS ON MEMBERS.memid = SS.memid
INNER JOIN
PRODUCTCATS
INNER JOIN
PRODUCTS ON PRODUCTCATS.productcatid = PRODUCTS.productcatid
ON SS.productid = PRODUCTS.productid
INNER JOIN
EMPLOYEES ON SS.employeeid = EMPLOYEES.employeeid
WHERE
(PRODUCTS.productcatid <> '68')
AND (PRODUCTS.departmentid = '5')
AND (MEMBERS.status = 'A')
AND (SS.usedquantity < SS.initialquantity)
AND (PRODUCTS.scancode <> 'PASSCOMP1' OR
PRODUCTS.scancode <> 'PASSCOMP3' OR
PRODUCTS.scancode <> 'PASSCOMP5')
AND (PRODUCTS.inactive = 'False')
I really appreciate the help!!!
Sometnihg like this? Just add the fist query as table to your second query.
SELECT
SITES.sitename, MEMBERS.scancode,
MEMBERS.lname, MEMBERS.fname, MEMBERS.mtypeid,
MEMBERS.status, PRODUCTS.description, SS.initialdate,
SS.initialquantity, SS.usedquantity, SS.dateexpire,
EMPLOYEES.lname AS Expr1, EMPLOYEES.fname AS Expr2
FROM
SITES
INNER JOIN
MEMBERS ON SITES.siteid = MEMBERS.siteid
INNER JOIN
SS ON MEMBERS.memid = SS.memid
INNER JOIN
PRODUCTCATS
INNER JOIN
PRODUCTS ON PRODUCTCATS.productcatid = PRODUCTS.productcatid
ON SS.productid = PRODUCTS.productid
INNER JOIN
EMPLOYEES ON SS.employeeid = EMPLOYEES.employeeid
INNER JOIN
(SELECT
MEMBERS.memid
FROM
SITES
INNER JOIN
MEMBERS ON SITES.siteid = MEMBERS.siteid
INNER JOIN
SS ON MEMBERS.memid = SS.memid
INNER JOIN
PRODUCTCATS
INNER JOIN
PRODUCTS ON PRODUCTCATS.productcatid = PRODUCTS.productcatid
ON SS.productid = PRODUCTS.productid
INNER JOIN
EMPLOYEES ON SS.employeeid = EMPLOYEES.employeeid
WHERE
(PRODUCTS.productcatid = 77)
AND (SS.initialdate BETWEEN #rvPurchaseStart AND #rvPurchaseEnd)) TMP_TABLE ON MEMBERS.memid = TMP_TABLE.memid
WHERE
(PRODUCTS.productcatid <> '68')
AND (PRODUCTS.departmentid = '5')
AND (MEMBERS.status = 'A')
AND (SS.usedquantity < SS.initialquantity)
AND (PRODUCTS.scancode <> 'PASSCOMP1' OR PRODUCTS.scancode <> 'PASSCOMP3' OR PRODUCTS.scancode <> 'PASSCOMP5')
AND (PRODUCTS.inactive = 'False')

How to total SQL results

How do I get my results to total into one record instead of multiple instances of item_id. instead I need the total qty invoiced for each item.
Query Results
http://i.imgur.com/uakv7e5.jpg
select
inv_mast.default_product_group,
inv_mast.item_id,
inv_mast.item_desc,oe_hdr.order_date,
oe_line.qty_invoiced,
oe_line.extended_price
from
job_price_hdr
join oe_hdr on oe_hdr.job_price_hdr_uid = job_price_hdr.job_price_hdr_uid
join oe_line on oe_line.order_no = oe_hdr.order_no
join inv_mast on inv_mast.inv_mast_uid = oe_line.inv_mast_uid
where
oe_line.qty_invoiced> 0
and
oe_hdr.customer_id = 100080
and
default_product_group = 'FAST'
order by
inv_mast.item_id
Try this and let me know if you need more information
select
inv_mast.item_id,SUM(oe_line.qty_invoiced)
from
job_price_hdr
join oe_hdr on oe_hdr.job_price_hdr_uid = job_price_hdr.job_price_hdr_uid
join oe_line on oe_line.order_no = oe_hdr.order_no
join inv_mast on inv_mast.inv_mast_uid = oe_line.inv_mast_uid
where
oe_line.qty_invoiced> 0
and
oe_hdr.customer_id = 100080
and
default_product_group = 'FAST'
GROUP BY inv_mast.item_id
order by
inv_mast.item_id

SQL (Condition not applied on one column)

this Query have one problem with it , except FOR Column
it show multiple values in this coulmn , put as perthe condition it should only show one value '7' , ddepending on this value the coulmn value will be set , but now as it show all the value it cuase too much duplication and other issues
here the query :
SELECT T0.ItemCode, T0.ItemName, T0.CardCode, T0.CodeBars, T0.U_VEN_CODE, T2.UgpCode, T3.AltQty, T3.BaseQty, CASE WHEN T4.Uomentry = - 1 THEN T0.[BuyUnitMsr] ELSE t4.UomName END AS 'UoMName',
T4.UomEntry, T0.U_CAT_CODE, T0.U_CAT_NAME, T1.CardName,
(SELECT TOP (1) PDN1.U_AC_QTY_ORDER
FROM PDN1 INNER JOIN
OPDN ON PDN1.DocEntry = OPDN.DocEntry
WHERE (PDN1.ItemCode = T0.ItemCode) AND (OPDN.CardCode = T0.CardCode)
ORDER BY OPDN.DocDate DESC) AS OQuantity,
(SELECT TOP (1) PDN1_1.U_AC_QTY_BONUS
FROM PDN1 AS PDN1_1 INNER JOIN
OPDN AS OPDN_1 ON PDN1_1.DocEntry = OPDN_1.DocEntry
WHERE (PDN1_1.ItemCode = T0.ItemCode) AND (OPDN_1.CardCode = T0.CardCode)
ORDER BY OPDN_1.DocDate DESC) AS BQuantity, ITM1_1.Price, T0.U_DISC_PER, SMMU01.WhsCode, SMMU01.OnHand, SMAB01.WhsCode AS Expr1, SMAB01.OnHand AS Expr2,
SMKH01.WhsCode AS Expr3, SMKH01.OnHand AS Expr4, ITM9.PriceList, ITM9.Price AS Expr5, ITM1.PriceList AS Expr6, ITM1.Price AS Expr7
FROM OITM AS T0 INNER JOIN
OCRD AS T1 ON T0.CardCode = T1.CardCode INNER JOIN
OUGP AS T2 ON T0.UgpEntry = T2.UgpEntry INNER JOIN
UGP1 AS T3 ON T2.UgpEntry = T3.UgpEntry INNER JOIN
OITW AS SMMU01 ON T0.ItemCode = SMMU01.ItemCode INNER JOIN
OITW AS SMAB01 ON SMMU01.ItemCode = SMAB01.ItemCode INNER JOIN
OITW AS SMKH01 ON SMAB01.ItemCode = SMKH01.ItemCode INNER JOIN
ITM9 ON T0.ItemCode = ITM9.ItemCode AND ITM9.PriceList = '7' INNER JOIN
ITM1 ON T0.ItemCode = ITM1.ItemCode LEFT OUTER JOIN
ITM1 AS ITM1_1 ON T0.ItemCode = ITM1_1.ItemCode AND ITM1_1.PriceList = '10' LEFT OUTER JOIN
OUOM AS T4 ON T3.UomEntry = T4.UomEntry
WHERE (T0.Series = '65') AND (T4.UomEntry = 3 OR
T4.UomEntry = '-1') AND (SMMU01.WhsCode = 'W-SMMU01') AND (SMAB01.WhsCode = 'W-SMAB01') AND (SMKH01.WhsCode = 'W-SMKH01')
and here is the result of the coulmn
Expr6
1
2
3
4
5
6
7
8
9
10
how it possiable to let only shown '7' as decided in the condition ?
thx
SELECT [...]
, ITM1.PriceList AS Expr6
, ITM1.Price AS Expr7
FROM
OITM AS T0
INNER JOIN OCRD AS T1
ON T0.CardCode = T1.CardCode
INNER JOIN OUGP AS T2
ON T0.UgpEntry = T2.UgpEntry
INNER JOIN UGP1 AS T3
ON T2.UgpEntry = T3.UgpEntry
INNER JOIN OITW AS SMMU01
ON T0.ItemCode = SMMU01.ItemCode
INNER JOIN OITW AS SMAB01
ON SMMU01.ItemCode = SMAB01.ItemCode
INNER JOIN OITW AS SMKH01
ON SMAB01.ItemCode = SMKH01.ItemCode
INNER JOIN ITM9
ON T0.ItemCode = ITM9.ItemCode
AND ITM9.PriceList = '7'
INNER JOIN ITM1
ON T0.ItemCode = ITM1.ItemCode
LEFT OUTER JOIN ITM1 AS ITM1_1
ON T0.ItemCode = ITM1_1.ItemCode
AND ITM1_1.PriceList = '10'
LEFT OUTER JOIN OUOM AS T4
ON T3.UomEntry = T4.UomEntry
Your INNER JOIN ITM1 has not PriceList = '7' filter
You should be able to fix it with :
INNER JOIN ITM1
ON T0.ItemCode = ITM1.ItemCode
AND ITM1.PriceList = '7'
The question is now : why ITM1 and ITM9 are duplicated ?

sql 2008 error 'the column prod_desc1 was specified multiple times for "a"'

I've got this ginormous sql select statement I'm using in a SSRS 2008 report. I need field "prod_desc1" from the Product table, but also the same named field from Order_Line. I've aliased both of them but I'm still getting the error. Here's my statment:
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
any help would be greatly appreciated! :)