sql COUNT question - sql

How can I modify this query to return results that exclude all records that have a duplicate DocNum?
SELECT IdEntry, DocNum, CardCode, QUOTENAME(CardName,'"'),
Convert(Decimal(10,2),PayAmount), Convert(Decimal(10,2),InvPayAmnt),
CONVERT(VARCHAR(10), T5.PmntDate,101), NumAtCard, PymMeth, ObjType
FROM DELAWARE.dbo.PWZ3
INNER JOIN OPWZ T5 ON T5.IdNumber = IdEntry
WHERE T5.PmntDate = '3/10/2011'
AND T5.Canceled = 'N'
AND Checked = 'Y'

Try
WITH
dups (DocNum)
AS (
SELECT DocNum
FROM DELAWARE.dbo.PWZ3
GROUP BY DocNum
HAVING COUNT(1) > 1
)
SELECT PWZ3.IdEntry, PWZ3.DocNum, PWZ3.CardCode, QUOTENAME(CardName,'"'),
Convert(Decimal(10,2),PayAmount), Convert(Decimal(10,2),InvPayAmnt),
CONVERT(VARCHAR(10), T5.PmntDate,101), NumAtCard, PymMeth, ObjType
FROM DELAWARE.dbo.PWZ3 PWZ3
INNER JOIN OPWZ T5 ON T5.IdNumber = PWZ3.IdEntry
LEFT JOIN Dups ON DUPS.DocNum = PWZ3.DocNum
WHERE T5.PmntDate = '3/10/2011'
AND T5.Canceled = 'N'
AND Checked = 'Y'
AND Dups.DocNum is null
(I might not have all the column aliases right)

I'm not sure I understood all the query, but doesn't a simple
SELECT ... GROUP BY docNum
suffice?

SELECT IdEntry, DocNum, CardCode, QUOTENAME(CardName,'"'),
Convert(Decimal(10,2),PayAmount), Convert(Decimal(10,2),InvPayAmnt),
CONVERT(VARCHAR(10), T5.PmntDate,101), NumAtCard, PymMeth, ObjType
FROM DELAWARE.dbo.PWZ3
INNER JOIN OPWZ T5 ON T5.IdNumber = IdEntry
JOIN (SELECT min(IdEntry) as minIdEntry from DELAWARE.dbo.PWZ3 group by DocNum) tmp on DELAWARE.dbo.PWZ3.IdEntry = minIdEntry
WHERE T5.PmntDate = '3/10/2011'
AND T5.Canceled = 'N'
AND Checked = 'Y'

Related

query won't work after pivot

I'm trying to develop a query and i got something like this
SELECT a.No,
a.Finish,
a.Shift,
a.McCode,
d.NAME,
b.ItemCode,
b.ItemName,
b.Qty,
b.LotNo,
b.Description,
CASE
WHEN b.Description LIKE '%not good%' THEN 'NG'
ELSE 'OK'
END AS OKNG,
c.ItemCode AS matcode,
c.LotNo AS matlot,
CASE
WHEN e.GroupName = 'CONTACT' THEN 'CONTACT'
ELSE 'Coil/Silver/Wire'
END AS GroupName,
( c.Qty / ( a.Qty + a.QtyNg ) ) * b.Qty AS materialused
FROM PPRDDLV a
LEFT JOIN ICMUTTRAN b
ON a.PrdNo = b.TranNo
AND a.No = b.TranId
LEFT JOIN ICMUTTRAN c
ON a.PrdNo = c.TranNo
AND a.Finish = c.DatePost
AND c.TranTypeID = 6
AND c.LotNo <> '0'
LEFT JOIN popr d
ON a.OprCode = d.Code
LEFT JOIN ICITEM e
ON c.ItemId = e.id
WHERE c.qty IS NOT NULL
AND b.ItemCode IS NOT NULL
I got around 49000 records in around 2 seconds, then I wan't to pivot a column that has 2 kind of value, so I turned it to something like this
SELECT no,
finish,
shift,
mccode,
NAME,
itemcode,
itemname,
qty,
lotno,
description,
okng,
matcode,
matlot
FROM (SELECT a.No,
a.Finish,
a.Shift,
a.McCode,
d.NAME,
b.ItemCode,
b.ItemName,
b.Qty,
b.LotNo,
b.Description,
CASE
WHEN b.Description LIKE '%not good%' THEN 'NG'
ELSE 'OK'
END AS OKNG,
c.ItemCode AS matcode,
c.LotNo AS matlot,
CASE
WHEN e.GroupName = 'CONTACT' THEN 'CONTACT'
ELSE 'Coil/Silver/Wire'
END AS GroupName,
( c.Qty / ( a.Qty + a.QtyNg ) ) * b.Qty AS materialused
FROM PPRDDLV a
LEFT JOIN ICMUTTRAN b
ON a.PrdNo = b.TranNo
AND a.No = b.TranId
LEFT JOIN ICMUTTRAN c
ON a.PrdNo = c.TranNo
AND a.Finish = c.DatePost
AND c.TranTypeID = 6
AND c.LotNo <> '0'
LEFT JOIN popr d
ON a.OprCode = d.Code
LEFT JOIN ICITEM e
ON c.ItemId = e.id
WHERE c.qty IS NOT NULL
AND b.ItemCode IS NOT NULL) AS a
PIVOT (Max(materialused)
FOR groupname IN ([CONTACT],
[Coil/Silver/Wire])) AS b
but the query won't complete even after 10 minutes, it won't even show a single records. beside that one, the first query before I used pivot, I put an order by a.no but when I executed it, the result only shows to record 105 but the query is still working and no more record was loaded.
can anyone help me find out what's going on?
thank you!
Here's the sample data collected from the inner query and the expected value after the query.
enter image description here
I realize that I selected "matcode" in the outer select while it shouldn't be there (still didn't work though, and note for the column "material used", I'll be using it later after the pivot is done)
Try using conditional aggregate instead of pivot.
SELECT no,
finish,
shift,
mccode,
NAME,
itemcode,
itemname,
qty,
lotno,
description,
okng,
matcode,
matlot,
[CONTACT] = Max(CASE WHEN GroupName = 'CONTACT' THEN materialused END),
[Coil/Silver/Wire] = Max(CASE WHEN GroupName <> 'CONTACT' THEN materialused END)
FROM (<inner query>) AS a
GROUP BY no,
finish,
shift,
mccode,
NAME,
itemcode,
itemname,
qty,
lotno,
description,
okng,
matcode,
matlot
---IF you don't have index on filter columns then try to apply for performance
SELECT a.No,
a.Finish,
a.Shift,
a.McCode,
d.NAME,
b.ItemCode,
b.ItemName,
b.Qty,
b.LotNo,
b.Description,
CASE WHEN b.Description LIKE '%not good%' THEN 'NG'
ELSE 'OK'
END AS OKNG,
c.ItemCode,
c.LotNo,
MAX(CASE WHEN e.groupname ='CONTACT' THEN ( c.Qty / ( a.Qty + a.QtyNg ) ) * b.Qty END)OVER(ORDER BY a.no) AS [CONTACT],
MAX(CASE WHEN e.groupname ='Coil/Silver/Wire' THEN ( c.Qty / ( a.Qty + a.QtyNg ) ) * b.Qty END)OVER(ORDER BY a.no) AS [Coil/Silver/Wire]
FROM PPRDDLV a WITH(NOLOCK)
LEFT JOIN ICMUTTRAN b WITH(NOLOCK)
ON a.PrdNo = b.TranNo
AND a.No = b.TranId
LEFT JOIN ICMUTTRAN c WITH(NOLOCK)
ON a.PrdNo = c.TranNo
AND a.Finish = c.DatePost
AND c.TranTypeID = 6
AND c.LotNo <> '0'
LEFT JOIN popr d WITH(NOLOCK)
ON a.OprCode = d.Code
LEFT JOIN ICITEM e
ON c.ItemId = e.id
WHERE c.qty IS NOT NULL
AND b.ItemCode IS NOT NULL

Aggregate Function / Group By Clause

Having some minor issues with my query. I am trying just to get sums for a project. I know I am probably missing something super simple but I am getting caught up. If someone could take a look and give me a suggestion on how I can figure this out.
select
(select count(distinct(o1.orderno))
from mck_hvs.orderheader o1 with(nolock)
where o1.orderno = od.orderno and o1.refrigerate = 'Y'
) as TotalColdOrders,
(select count(distinct(o2.orderno))
from mck_hvs.orderdetails o2 with(nolock)
where o2.orderno = od.orderno and o2.drugclass
not in ('null', 'Rx')
) as ControlledOrders,
(select count(distinct(o3.orderno))
from mck_hvs.orderheader o3 with(nolock)
where o3.orderno = od.orderno and o3.pucksideinorder = 'Y' and
o3.totesideinorder = 'N' and o3.numitems > 4
) as RobotOrders,
(select count(distinct(o4.orderno))
from mck_hvs.orderheader o4 with(nolock)
where o4.orderno = od.orderno and o4.pucksideinorder = 'Y' and
o4.totesideinorder = 'Y'
) as ComboOrders,
(select count(distinct(o5.rxnum))
from mck_hvs.orderdetails o5 with(nolock)
where o5.refrigerate = 'Y'
) as TotalColdScripts,
(select count(distinct(o6.rxnum))
from mck_hvs.orderdetails o6 with(nolock)
where o6.orderno = od.orderno and o6.drugclass
not in ('null', 'RX')
) as ControlledScripts,
(select sum(o7.numscripts)
from mck_hvs.orderheader o7 with(nolock)
where o7.orderno = od.orderno and o7.pucksideinorder = 'Y' and
o7.totesideinorder = 'N' and o7.numitems > 4
) as RobotScripts,
(select sum(o8.numscripts)
from mck_hvs.orderheader o8 with(nolock)
where o8.orderno = od.orderno and o8.pucksideinorder = 'Y' and
o8.totesideinorder = 'Y'
) as ComboOrderScripts,
count(distinct(od.orderno)) as TotalOrders,
count(distinct(od.rxnum)) as TotalScripts
from
mck_hvs.orderdetails od with( nolock )
You should try this:
SELECT
od.orderno,
COUNT(DISTINCT
CASE WHEN oh.refrigerate = 'Y' THEN od.orderno END
) AS totalcoldorders,
COUNT(DISTINCT
CASE WHEN od.drugclass NOT IN ('null', 'Rx') THEN od.orderno END
) AS controlledorders,
COUNT(DISTINCT
CASE
WHEN oh.pucksideinorder = 'Y'
AND oh.totesideinorder = 'N' AND oh.numitems > 4
THEN od.orderno
END
) AS robotorders,
COUNT(DISTINCT
CASE
WHEN oh.pucksideinorder = 'Y' AND oh.totesideinorder = 'Y'
THEN oh.orderno
END
) AS comboorders,
COUNT(DISTINCT
CASE WHEN od.refrigerate = 'Y' THEN od.rxnum END
) AS totalcoldscripts,
COUNT(DISTINCT
CASE WHEN od.drugclass NOT IN ('null', 'RX') THEN od.rxnum END
) AS controlledscripts,
SUM(
CASE
WHEN oh.pucksideinorder = 'Y'
AND oh.totesideinorder = 'N' AND oh.numitems > 4
THEN oh.numscripts
END
) AS robotscripts,
SUM(
CASE
WHEN oh.pucksideinorder = 'Y' AND oh.totesideinorder = 'Y'
THEN oh.numscripts
END
) AS comboorderscripts,
(
SELECT COUNT(DISTINCT orderno)
FROM mck_hvs.orderdetails WITH (nolock)
) AS totalorders,
(
SELECT COUNT(DISTINCT rxnum)
FROM mck_hvs.orderdetails WITH (nolock)
) AS totalscripts
FROM mck_hvs.orderdetails od WITH (nolock)
LEFT JOIN mck_hvs.orderheader oh WITH (nolock)
ON oh.orderno = od.orderno
GROUP BY od.orderno
ORDER BY od.orderno;

Optimize SQL, same line multiple times in CASE

I've written a SQL Servre stored procedure (SQL Server 2014) that looks at a certain column in a certain table, if the result is null I want to take the value from an other column.
I've ended up with writing this line twice:
(Select t_EC
from TTCIB
where rtrim(ltrim(t_item)) = rtrim(ltrim(t1.t_item))
and t_comp = '50')
Once to check if it is null.
And the second time to actually add the value to my result if it wasn't null.
Is there a way to optimize this? So that the system doesn't need to run this line twice? (it has ltrim function twice and rtrim function twice so it would be useful)
SELECT DISTINCT TOP 15
(SELECT t_EC
FROM TTCIB
WHERE rtrim(ltrim(t_item)) = rtrim(ltrim(t1.t_item))
AND t_comp = '18') as EC18,
CASE
WHEN (SELECT t_EC FROM TTCIB
WHERE rtrim(ltrim(t_item)) = rtrim(ltrim(t1.t_item))
AND t_comp = '50') IS NULL
THEN (SELECT t_EC
FROM TTCIB
WHERE rtrim(ltrim(t_item)) = rtrim(ltrim(t1.t_item))
AND t_comp = '51')
ELSE (SELECT t_EC
FROM TTCIB
WHERE rtrim(ltrim(t_item)) = rtrim(ltrim(t1.t_item))
AND t_comp = '50')
END AS EC50,
RTRIM(t1.t_sern) AS SerialNumber, t1.t_item AS Item,
RTRIM(t1.t_desc) AS ItemDesc, t1.t_ofbp AS SoldToBP,
RTRIM(t2.t_nama) AS BPName, t1.t_cwte AS Warranty,
t1.t_dltm AS DeliveryTime, t_optm AS InstallationTime,
t_clst AS Cluster, ISNULL(t3.t_endt,'1970-01-01') AS EndDate,
t4.t_csig as ItemSignalCode, t5.t_dsca as ItemSignalCodeDesc
FROM
FnGetSerialNos(#SerialNumber, #MasterCompany) t1
INNER JOIN
ttccom100900 t2 ON t1.t_ofbp = t2.t_bpid
LEFT OUTER JOIN
ttsctm120900 t3 ON t1.t_term = t3.t_term
LEFT OUTER JOIN
ttcibd001900 t4 ON ltrim(t1.t_item) = ltrim(t4.t_item)
LEFT OUTER JOIN
ttcmcs018900 t5 ON t4.t_csig = t5.t_csig
Use an INNER JOIN to read all relevant values from TTCIB, and use a COALESCE() function instead of CASE WHEN ... END:
SELECT DISTINCT TOP 15
x.t_18 as EC18
,COALESCE(x.t_50, x.t_51) AS EC50
,RTRIM(t1.t_sern) AS SerialNumber
,t1.t_item AS Item
,RTRIM(t1.t_desc) AS ItemDesc
,t1.t_ofbp AS SoldToBP
,RTRIM(t2.t_nama) AS BPName
,t1.t_cwte AS Warranty
,t1.t_dltm AS DeliveryTime
,t_optm AS InstallationTime
,t_clst AS Cluster
, ISNULL(t3.t_endt,'1970-01-01') AS EndDate
,t4.t_csig as ItemSignalCode
, t5.t_dsca as ItemSignalCodeDesc
FROM FnGetSerialNos(#SerialNumber, #MasterCompany) t1
INNER JOIN (
SELECT RTRIM(LTRIM(t_EC)) AS t_EC
,MAX(CASE WHEN t_comp = '18' THEN t_EC END) AS t_18
,MAX(CASE WHEN t_comp = '50' THEN t_EC END) AS t_50
,MAX(CASE WHEN t_comp = '51' THEN t_EC END) AS t_51
FROM TTCIB
GROUP BY t_EC
) x
ON RTRIM(LTRIM(t1.t_item)) = x.t_EC
INNER JOIN ttccom100900 t2
ON t1.t_ofbp = t2.t_bpid
LEFT OUTER JOIN ttsctm120900 t3
ON t1.t_term = t3.t_term
Left Outer JOIN ttcibd001900 t4
ON ltrim(t1.t_item) = ltrim(t4.t_item)
Left Outer JOIN ttcmcs018900 t5
ON t4.t_csig = t5.t_csig
;
faster(for all kinds of db)
coalesce((Select t_EC from TTCIB where rtrim(ltrim(t_item)) = rtrim(ltrim(t1.t_item)) and t_comp = '50'),(Select t_EC from TTCIB where rtrim(ltrim(t_item)) = rtrim(ltrim(t1.t_item)) and t_comp = '51')) as EC50
fastest(only for sqlserver, oracle with this method is quite complex)
(Select top 1 t_EC from TTCIB where rtrim(ltrim(t_item)) = rtrim(ltrim(t1.t_item)) and t_comp in ('50','51') order by t_comp asc) as EC50

How to make a cell blank in a row from a query result?

I have a query as below:
SELECT
cc.chain_desc as chain_desc
,cc.chain_id as chain_id
,COUNT(distinct t.trans_id) as TranCount
FROM TRANSACTION AS t
LEFT OUTER JOIN location AS l
ON t.location_id = l.location_id
LEFT OUTER JOIN trans_line AS tl
ON t.trans_id = tl.trans_id
LEFT OUTER JOIN contract as c
ON t.contract_id = c.contract_id
LEFT OUTER JOIN chain_desc as cc
ON l.chain_id = cc.chain_id
WHERE
t.loc_country = 'U'
AND c.issuer_id IN (156966,166203)
AND t.trans_date >= '2016-10-01 00:00'
and t.trans_date < '2016-10-31 00:00'
AND tl.cat NOT IN ('DEF','DEFD','DEFC')
GROUP BY cc.chain_desc, cc.chain_id
UNION
SELECT
'TOTAL'
,0
,COUNT(distinct t.trans_id)
FROM TRANSACTION AS t
LEFT OUTER JOIN location AS l
ON t.location_id = l.location_id
LEFT OUTER JOIN trans_line AS tl
ON t.trans_id = tl.trans_id
LEFT OUTER JOIN contract as c
ON t.contract_id = c.contract_id
LEFT OUTER JOIN chain_desc as cc
ON l.chain_id = cc.chain_id
WHERE
t.loc_country = 'U'
AND c.issuer_id IN (156966,166203)
AND t.trans_date >= '2016-10-01 00:00'
and t.trans_date < '2016-10-31 00:00'
AND tl.cat NOT IN ('DEF','DEFD','DEFC')
The above query when executed reurns the below result:
I need the result to be displayed as below:
The column "Chain_Id" is of "integer" type, how can I make that blank?.
you can simply select null
.....
UNION
SELECT
'TOTAL'
, NULL::INTEGER
,COUNT(distinct t.trans_id)
FROM TRANSACTION AS t
LEFT OUTER JOIN location AS l
ON t.location_id = l.location_id
LEFT OUTER JOIN trans_line AS tl
ON t.trans_id = tl.trans_id
LEFT OUTER JOIN contract as c
ON t.contract_id = c.contract_id
LEFT OUTER JOIN chain_desc as cc
ON l.chain_id = cc.chain_id
WHERE
t.loc_country = 'U'
AND c.issuer_id IN (156966,166203)
AND t.trans_date >= '2016-10-01 00:00'
and t.trans_date < '2016-10-31 00:00'
AND tl.cat NOT IN ('DEF','DEFD','DEFC')
because null is not a type you could try add above the first query
DEFINE test INT;
LET test = NULL;
......
SELECT
'TOTAL'
, test
,COUNT(distinct t.trans_id)
.....
Or like suggusted by #Jonathan Leffler use NULL::INTEGER or CAST(NULL AS INTEGER)
In Informix you can use NULL in the projection list, but the column must have a type. Since in Informix NULL does not have a type, you need to use a CAST.
SELECT NULL::INTEGER AS id FROM systables WHERE tabid = 1;
SELECT CAST(NULL AS INTEGER) AS id FROM systables WHERE tabid = 1;
You can check the answers to this other question (Informix: Select null problem).
You can check the IBM Knowledge Center (NULL Keyword).
One way is to convert to NULL:
(case when cc.chain_id <> 0 then cc.chain_id end) as chain_id
Another is to convert everything to a string:
(case when cc.chain_id <> 0 then cast(cc.chain_id as varchar(255)) else '' end) as chain_id

Invoke a column twice with different conditions

I really appreciate any help with this matter :)
Am Working on a Report now and I had faced some troubles
I have this Query and it work fine , now I want to add a coulmn that is already exist in the query(from the same table) , but this time i'll change the condition of it , BTW the conditions in both of the 2 column are based on one other column
like for example If I have this :
Select Price from ITM1 WHERE PriceList = '1'
and also this
Select Price from ITM1 WHERE PriceList = '10'
how I can write in the same query and let them display in two different column ?
I will put the Query here in case if some one can help me through it :
you can see THE Column Price & PriceList in the lower part of it ,Bolded.
I just need to make the samething again but with a new coulmn name thats it.
Using the IN Operator will give you what you want. However, there are other changes that you can make to your query which would boost performance - but it's out of scope to the question. I'm unclear as to what you're trying to do with the different "columns" Please help explain. Else see #Dave.Gugg's answer which does just that.
SELECT T0.ItemCode,
T0.ItemName,
T0.CardCode,
T0.CodeBars,
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) dbo.PDN1.U_AC_QTY_ORDER
FROM dbo.PDN1
INNER JOIN dbo.OPDN ON dbo.PDN1.DocEntry = dbo.OPDN.DocEntry
WHERE (dbo.PDN1.ItemCode = T0.ItemCode)
AND (dbo.OPDN.CardCode = T0.CardCode)
ORDER BY dbo.OPDN.DocDate DESC
) AS OQuantity,
(
SELECT TOP (1) PDN1_1.U_AC_QTY_BONUS
FROM dbo.PDN1 AS PDN1_1
INNER JOIN dbo.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.Price,
T0.U_DISC_PER
FROM dbo.OITM AS T0
INNER JOIN dbo.OCRD AS T1 ON T0.CardCode = T1.CardCode
INNER JOIN dbo.OUGP AS T2 ON T0.UgpEntry = T2.UgpEntry
INNER JOIN dbo.UGP1 AS T3 ON T2.UgpEntry = T3.UgpEntry
INNER JOIN dbo.ITM1 ON T0.ItemCode = dbo.ITM1.ItemCode
AND dbo.ITM1.PriceList IN ('1', '10')
LEFT JOIN dbo.OUOM AS T4 ON T3.UomEntry = T4.UomEntry
WHERE (T0.Series = '65')
AND (
T4.UomEntry = 3
OR T4.UomEntry = '-1'
)
If you want a different column (this may perform better than two joins):
SELECT T0.ItemCode,
T0.ItemName,
T0.CardCode,
T0.CodeBars,
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) dbo.PDN1.U_AC_QTY_ORDER
FROM dbo.PDN1
INNER JOIN dbo.OPDN ON dbo.PDN1.DocEntry = dbo.OPDN.DocEntry
WHERE (dbo.PDN1.ItemCode = T0.ItemCode)
AND (dbo.OPDN.CardCode = T0.CardCode)
ORDER BY dbo.OPDN.DocDate DESC
) AS OQuantity,
(
SELECT TOP (1) PDN1_1.U_AC_QTY_BONUS
FROM dbo.PDN1 AS PDN1_1
INNER JOIN dbo.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,
CASE
WHEN ITM1.PriceList = '1'
THEN ITM1.Price
ELSE '0'
END AS Price1,
CASE
WHEN ITM1.PriceList = '10'
THEN ITM1.Price
ELSE '0'
END AS Price2,
T0.U_DISC_PER
FROM dbo.OITM AS T0
INNER JOIN dbo.OCRD AS T1 ON T0.CardCode = T1.CardCode
INNER JOIN dbo.OUGP AS T2 ON T0.UgpEntry = T2.UgpEntry
INNER JOIN dbo.UGP1 AS T3 ON T2.UgpEntry = T3.UgpEntry
INNER JOIN dbo.ITM1 ON T0.ItemCode = dbo.ITM1.ItemCode
AND dbo.ITM1.PriceList IN ('1', '10')
LEFT JOIN dbo.OUOM AS T4 ON T3.UomEntry = T4.UomEntry
WHERE (T0.Series = '65')
AND (
T4.UomEntry = 3
OR T4.UomEntry = '-1'
)
You should be able to just join to the table a second time, but you will need to make the joins outer:
SELECT T0.ItemCode ,
T0.ItemName ,
T0.CardCode ,
T0.CodeBars ,
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 )
dbo.PDN1.U_AC_QTY_ORDER
FROM dbo.PDN1
INNER JOIN dbo.OPDN ON dbo.PDN1.DocEntry = dbo.OPDN.DocEntry
WHERE ( dbo.PDN1.ItemCode = T0.ItemCode )
AND ( dbo.OPDN.CardCode = T0.CardCode )
ORDER BY dbo.OPDN.DocDate DESC
) AS OQuantity ,
( SELECT TOP ( 1 )
PDN1_1.U_AC_QTY_BONUS
FROM dbo.PDN1 AS PDN1_1
INNER JOIN dbo.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 ,
dbo.ITM1.Price ,
ITM1Second.Price,
T0.U_DISC_PER
FROM dbo.OITM AS T0
INNER JOIN dbo.OCRD AS T1 ON T0.CardCode = T1.CardCode
INNER JOIN dbo.OUGP AS T2 ON T0.UgpEntry = T2.UgpEntry
INNER JOIN dbo.UGP1 AS T3 ON T2.UgpEntry = T3.UgpEntry
LEFT OUTER JOIN dbo.ITM1 ON T0.ItemCode = dbo.ITM1.ItemCode
AND dbo.ITM1.PriceList = '10'
LEFT OUTER JOIN dbo.ITM1 ITM1Second ON T0.ItemCode = ITM1Second.ItemCode
AND ITM1Second.PriceList = '1'
LEFT OUTER JOIN dbo.OUOM AS T4 ON T3.UomEntry = T4.UomEntry
WHERE ( T0.Series = '65' )
AND ( T4.UomEntry = 3
OR T4.UomEntry = '-1'
)