Link to home
Start Free TrialLog in
Avatar of JonYen
JonYenFlag for United Kingdom of Great Britain and Northern Ireland

asked on

SQL 2005 Server Query

I have a query which produces a dataset showing details of my open sales orders

It's using a DISTINCT statement in order to only show the header information of each sales order and not every sales order line item.   However, for this query I now need to show something which is only detailed at row level but I don't want to every row.    Is there a way to include column  t2.POtrgnum (which is the linked purchase order code) only once for each row?    In every case there will only be one row of each sales order which has this field populated.


SELECT DISTINCT T1.[DocNum], t1.numatcard, T1.[CardCode]'Customer No.', t4.firstname+' '+t4.lastname as 'Owner', T1.[CardName]'Customer Name', T1.[DocDate], T1.[DocDueDate]'Due Date', 
CASE

WHEN ((select sum(t2.quantity) from rdr1 t2 inner join ordr t20 on T20.DocEntry = T2.DocEntry where (t2.itemcode = 'cmt') and t20.docnum = t1.docnum) IS NULL) THEN (select sum(t2.quantity) from rdr1 t2 inner join ordr t20 on T20.DocEntry = T2.DocEntry where ((t2.SWW = 'PATTERN') OR (t2.SWW = 'PRINT-PRESS')) and t20.docnum = t1.docnum)

WHEN ((select sum(t2.quantity) from rdr1 t2 inner join ordr t20 on T20.DocEntry = T2.DocEntry where (t2.itemcode = 'cmt') and t20.docnum = t1.docnum) IS NOT NULL AND (select sum(t2.quantity) from rdr1 t2 inner join ordr t20 on T20.DocEntry = T2.DocEntry where ((t2.SWW = 'PATTERN') OR (t2.SWW = 'PRINT-PRESS')) and t20.docnum = t1.docnum) IS NOT NULL) THEN (select sum(t2.quantity) from rdr1 t2 inner join ordr t20 on T20.DocEntry = T2.DocEntry where (t2.itemcode = 'cmt') and t20.docnum = t1.docnum)+(select sum(t2.quantity) from rdr1 t2 inner join ordr t20 on T20.DocEntry = T2.DocEntry where ((t2.SWW = 'PATTERN') OR (t2.SWW = 'PRINT-PRESS')) and t20.docnum = t1.docnum) 

ELSE (select sum(t2.quantity) from rdr1 t2 inner join ordr t20 on T20.DocEntry = T2.DocEntry where (t2.itemcode = 'cmt') and t20.docnum = t1.docnum)
END'Qty',

t6.name'Design Status', t1.u_designdate, t7.name'Dye Print Status', t1.u_dyeprintsch, t5.name'Dye Press Status', t1.u_dyepressdate,t10.name'Laser Cutting Status',t1.U_lasercutdate, t8.name'Dye Sewing Status',t1.u_dyesewdate,  t30.[Name]'Factory'

FROM OCRD T0  INNER JOIN ORDR T1 ON T0.CardCode = T1.CardCode INNER JOIN RDR1 T2 ON T1.DocEntry = T2.DocEntry INNER JOIN OCRG T3 ON T0.GroupCode = T3.GroupCode INNER JOIN OHEM T4 ON T1.OwnerCode = T4.empID left JOIN [dbo].[@DYEPRESSSTAT]  T5 ON T1.U_DyePressStat = t5.code left JOIN [dbo].[@DESIGN_STATUS] T6 ON T1.U_DesignStat = t6.code left JOIN [dbo].[@DYEPRINTSTAT]  T7 ON T1.U_DyePrintStat = t7.code left JOIN [dbo].[@DYESEWSTAT]  T8 ON T1.U_DyeSewStat = t8.code left JOIN [dbo].[@LASER_CUT] T10 ON T1.U_LaserCutStat = t10.code INNER JOIN OITM T9 ON T2.ItemCode = T9.ItemCode left join [dbo].[@dyesewfact] t30 on t1.[U_Dyesewfact]=t30.code

WHERE T1.[DocStatus] ='O' and t9.qrygroup42='Y' ORDER BY t1.u_designdate ASC, T1.[U_DyePrintSch] asc, T1.[U_DyePressDate] asc, T1.[U_DyeSewDate] asc,  T1.[DocNum] ASC

Open in new window

Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Is this for a SQL Server 2005 database?
ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JonYen

ASKER

yes - it's SAP Business One verson 8.82 which is running on SQL Server 2005
Just want to try to understand why adding t2.POtrgnum  to the SELECT list won't solve your issue?
Avatar of JonYen

ASKER

what seems to happen when I do this is I get a duplicate row everytime there is a value in the t2.potrgnum field
Can you post a sample result?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JonYen

ASKER

sample data attached.   it is only producing one row for the records where there is no PO Target but in  cases where there is a PO Target number it produces 2 rows for each record
there is no attachment