Filtering by values - sql

I wrote a query a few days ago to determine whether a software is installed on a computer or not but I'm having troubles with filtering it showing only the "NO" values.
Would appreciate any help, thanks
Select Distinct Top 1000000 tblAssets.AssetName,
tsysOS.OSname,
Case
When soft01.AssetID Is Null Then 'NO'
Else 'YES'
End As Avecto,
Case
When soft02.AssetID Is Null Then 'NO'
Else 'YES'
End As CrowdStrike,
Case
When soft03.AssetID Is Null Then 'NO'
Else 'YES'
End As Cortex,
tblAssets.Firstseen,
tblAssets.Lastseen
From tblAssets
Left Join tblAssetCustom On tblAssets.AssetID = tblAssetCustom.AssetID
Left Join tsysOS On tsysOS.OScode = tblAssets.OScode
Left Join (Select tblSoftware.AssetID
From tblSoftware
Inner Join tblSoftwareUni On tblSoftwareUni.SoftID = tblSoftware.softID
Where tblSoftwareUni.softwareName Like '%Avecto%') As soft01 On
soft01.AssetID = tblAssets.AssetID
Left Join (Select tblSoftware.AssetID
From tblSoftware
Inner Join tblSoftwareUni On tblSoftwareUni.SoftID = tblSoftware.softID
Where tblSoftwareUni.softwareName Like '%CrowdStrike%') As soft02 On
soft02.AssetID = tblAssets.AssetID
Left Join (Select tblSoftware.AssetID
From tblSoftware
Inner Join tblSoftwareUni On tblSoftwareUni.SoftID = tblSoftware.softID
Where (tblSoftwareUni.softwareName Like '%Cortex%' Or
tblSoftwareUni.softwareName Like '%XDR%')) As soft03 On
soft03.AssetID = tblAssets.AssetID
Where tsysOS.OSname Like '%Win 20%'
Order By tblAssets.AssetName

Related

How to minimize my big query for showing counts using SQL

I am taking counts from CTE but common is Status(working,pening) and Divisions
but my query is becoming big because left join is same but just status and division are different. I have wrote 10 left join count but by passing status and divisions.
Below is my whole sql query
declare #createdBy int=79
;with cte as (
select max(w.WorkingNo)WorkingNo
from
working w
join workingdealhistory wd on wd.WorkHistoryId=w.workingNo and
w.status IN ('WORKING','PENDING') and w.mhlId>0 and w.IsActive=1
join TreasureTrove t on t.CandidateId=w.CandidateId and t.DepartmentId=2
group by w.CandidateId,w.status
)
select distinct m.potentialHospitalNo
,m.hospital
,ph.clientname
,cdiwr.working cdiworking
,cdipn.pending cdipending
,himwr.working himworking
,himpn.pending himpending
,cmurwr.working cmurworking
,cmurpn.pending cmurpending
,odmwr.working odmworking
,odmpn.pending odmpending
,traumawr.working traumaworking
,traumapn.pending traumapending
,ph.ClientId
from PotentialHospitlMaster m (NOLOCK)
Inner JOIN HospitalStatus HS (NOLOCK) On m.potentialHospitalNo=HS.ClientId
inner join potentialhospital ph on ph.potentialhospitalno=m.potentialhospitalno
--this is status='working' and division='CDI'
LEFT join(select COUNT(w.WorkingNo)working, MHLId from cte c inner join
working w on w.WorkingNo=c.WorkingNo
inner join WorkingDealHistory WH on w.WorkingNo=WH.WorkHistoryId
inner join potentialhospital phs on phs.potentialHospitalNo=w.MHLId where w.Status='working' and wh.Division='CDI' group by w.MHLId) as cdiwr on cdiwr.MHLId=ph.potentialHospitalNo
--this is status='pending' and division='CDI'
LEFT join(select COUNT(w.WorkingNo)pending, MHLId from cte c inner join
working w on w.WorkingNo=c.WorkingNo
inner join WorkingDealHistory WH on w.WorkingNo=WH.WorkHistoryId
inner join potentialhospital phs on phs.potentialHospitalNo=w.MHLId where w.Status='pending' and wh.Division='CDI' group by w.MHLId) as cdipn on cdipn.MHLId=ph.potentialHospitalNo
--this is status='working' and division='HIM'
LEFT join(select COUNT(w.WorkingNo)working, MHLId from cte c inner join
working w on w.WorkingNo=c.WorkingNo
inner join WorkingDealHistory WH on w.WorkingNo=WH.WorkHistoryId
inner join potentialhospital phs on phs.potentialHospitalNo=w.MHLId where w.Status='working' and wh.Division='HIM' group by w.MHLId) as himwr on himwr.MHLId=ph.potentialHospitalNo
--this is status='pending' and division='HIM'
LEFT join(select COUNT(w.WorkingNo)pending, MHLId from cte c inner join
working w on w.WorkingNo=c.WorkingNo
inner join WorkingDealHistory WH on w.WorkingNo=WH.WorkHistoryId
inner join potentialhospital phs on phs.potentialHospitalNo=w.MHLId where w.Status='pending' and wh.Division='HIM' group by w.MHLId) as himpn on himpn.MHLId=ph.potentialHospitalNo
--this is status='working' and division='CMUR'
LEFT join(select COUNT(w.WorkingNo)working, MHLId from cte c inner join
working w on w.WorkingNo=c.WorkingNo
inner join WorkingDealHistory WH on w.WorkingNo=WH.WorkHistoryId
inner join potentialhospital phs on phs.potentialHospitalNo=w.MHLId where w.Status='working' and wh.Division='CMUR' group by w.MHLId) as cmurwr on cmurwr.MHLId=ph.potentialHospitalNo
--this is status='pending' and division='CMUR'
LEFT join(select COUNT(w.WorkingNo)pending, MHLId from cte c inner join
working w on w.WorkingNo=c.WorkingNo
inner join WorkingDealHistory WH on w.WorkingNo=WH.WorkHistoryId
inner join potentialhospital phs on phs.potentialHospitalNo=w.MHLId where w.Status='pending' and wh.Division='CMUR' group by w.MHLId) as cmurpn on cmurpn.MHLId=ph.potentialHospitalNo
--this is status='working' and division='ODM'
LEFT join(select COUNT(w.WorkingNo)working, MHLId from cte c inner join
working w on w.WorkingNo=c.WorkingNo
inner join WorkingDealHistory WH on w.WorkingNo=WH.WorkHistoryId
inner join potentialhospital phs on phs.potentialHospitalNo=w.MHLId where w.Status='working' and wh.Division='ODM' group by w.MHLId) as odmwr on odmwr.MHLId=ph.potentialHospitalNo
--this is status='pending' and division='ODM'
LEFT join(select COUNT(w.WorkingNo)pending, MHLId from cte c inner join
working w on w.WorkingNo=c.WorkingNo
inner join WorkingDealHistory WH on w.WorkingNo=WH.WorkHistoryId
inner join potentialhospital phs on phs.potentialHospitalNo=w.MHLId where w.Status='pending' and wh.Division='ODM' group by w.MHLId) as odmpn on odmpn.MHLId=ph.potentialHospitalNo
--this is status='working' and division='Trauma'
LEFT join(select COUNT(w.WorkingNo)working, MHLId from cte c inner join
working w on w.WorkingNo=c.WorkingNo
inner join WorkingDealHistory WH on w.WorkingNo=WH.WorkHistoryId
inner join potentialhospital phs on phs.potentialHospitalNo=w.MHLId where w.Status='working' and wh.Division='Trauma' group by w.MHLId) as traumawr on traumawr.MHLId=ph.potentialHospitalNo
--this is status='pending' and division='Trauma'
LEFT join(select COUNT(w.WorkingNo)pending, MHLId from cte c inner join
working w on w.WorkingNo=c.WorkingNo
inner join WorkingDealHistory WH on w.WorkingNo=WH.WorkHistoryId
inner join potentialhospital phs on phs.potentialHospitalNo=w.MHLId where w.Status='pending' and wh.Division='Trauma' group by w.MHLId) as traumapn on traumapn.MHLId=ph.potentialHospitalNo
where m.IsActive=1 and HS.UpdatedStatus='MSA Sent' and HS.CreatedBy=#createdBy
Is there anything which we can minimize query by using group by or anything.
I've taken a bit of a stab at your schema, and I think you could do it with something like:
DECLARE #createdBy INT = 79;
WITH cte AS
(
SELECT w.CandidateId, w.status, WorkingNo = MAX(w.WorkingNo)
FROM working AS w
JOIN workingdealhistory AS wd
ON wd.WorkHistoryId = w.workingNo
AND w.status IN ('WORKING', 'PENDING')
AND w.mhlId > 0
AND w.IsActive = 1
JOIN TreasureTrove AS t
ON t.CandidateId = w.CandidateId
AND t.DepartmentId = 2
GROUP BY
w.CandidateId, w.status;
)
SELECT DISTINCT
m.potentialHospitalNo,
m.hospital,
ph.clientname,
cnt.CDIworking,
cnt.CDIPending,
cnt.HIMworking,
cnt.HIMPending,
cnt.CMURworking,
cnt.CMURPending,
cnt.ODMworking,
cnt.ODMPending,
cnt.Traumaworking,
cnt.TraumaPending
ph.ClientId
FROM PotentialHospitlMaster AS m
INNER JOIN HospitalStatus AS HS
ON m.potentialHospitalNo = HS.ClientId
INNER JOIN potentialhospital AS ph
ON ph.potentialhospitalno = m.potentialhospitalno
LEFT JOIN
(
SELECT w.MHLId,
CDIworking = COUNT(CASE WHEN w.Status = 'working' AND wh.Division = 'CDI' THEN w.WorkingNo END),
CDIPending = COUNT(CASE WHEN w.Status = 'pending' AND wh.Division = 'CDI' THEN w.WorkingNo END),
HIMworking = COUNT(CASE WHEN w.Status = 'working' AND wh.Division = 'HIM' THEN w.WorkingNo END),
HIMPending = COUNT(CASE WHEN w.Status = 'pending' AND wh.Division = 'HIM' THEN w.WorkingNo END),
CMURworking = COUNT(CASE WHEN w.Status = 'working' AND wh.Division = 'CMUR' THEN w.WorkingNo END),
CMURPending = COUNT(CASE WHEN w.Status = 'pending' AND wh.Division = 'CMUR' THEN w.WorkingNo END),
ODMworking = COUNT(CASE WHEN w.Status = 'working' AND wh.Division = 'ODM' THEN w.WorkingNo END),
ODMPending = COUNT(CASE WHEN w.Status = 'pending' AND wh.Division = 'ODM' THEN w.WorkingNo END),
Traumaworking = COUNT(CASE WHEN w.Status = 'working' AND wh.Division = 'Trauma' THEN w.WorkingNo END),
TraumaPending = COUNT(CASE WHEN w.Status = 'pending' AND wh.Division = 'Trauma' THEN w.WorkingNo END)
FROM working AS w
INNER JOIN WorkingDealHistory AS wh
ON w.WorkingNo = wh.WorkHistoryId
INNER JOIN cte AS c
ON c.CandidateId = w.CandidateId
AND c.status = w.status
AND c.WorkingNo = w.WorkingNo
GROUP BY
w.MHLId
) AS cnt
ON cnt.MHLId = ph.potentialHospitalNo
WHERE m.IsActive = 1
AND HS.UpdatedStatus = 'MSA Sent'
AND HS.CreatedBy = #createdBy;
N.B. I've removed NOLOCK as putting this everywhere is a bad habit to kick. Also, I am deeply suspicious of most queries that use DISTINCT across a large number of columns like this. More often than not the duplicates are a symptom of an error with the query, and DISTINCT is just a nasty plaster trying to cover up the real issue. You should work out where duplicates are coming from and look to remove either early or at least in a deterministic fashion.

Only include emails once in SQL query

Having a bit of a dead brain moment right now.
I have this query to give me BP Codes,Names, Contacts and Sales Person. I only want to include an existing email once in the query. If the same email exists for multiple BP's then only 1 BP should be included (lets say TOP 1).
SELECT DISTINCT
OCRD.CardCode,
OCRD.CardName,
OCPR.Name,
OCPR.E_MailL,
OSLP.SlpName
FROM OCRD
INNER JOIN OCRG ON OCRG.GroupCode = OCRD.GroupCode
INNER JOIN OCPR ON OCPR.CardCode = OCRD.CardCode
INNER JOIN OOND ON OOND.IndCode = OCRD.IndustryC
INNER JOIN OSLP ON OCRD.SlpCode = OSLP.SlpCode
WHERE OCRG.GroupName <> 'Retail' AND
OOND.IndName = 'Aged Care' and
OCRD.frozenFor = 'N' AND
OCPR.E_MailL LIKE '%#%'
ORDER BY OCRD.CardCode ASC
For the life of me I cant figure it out right now. Any help will be appreciated.
Based on this stackoverflow post, I assume this might work
SELECT DISTINCT ON (OCPR.E_MailL)
OCRD.CardCode,
OCRD.CardName,
OCPR.Name,
OCPR.E_MailL,
OSLP.SlpName
FROM OCRD
INNER JOIN OCRG ON OCRG.GroupCode = OCRD.GroupCode
INNER JOIN OCPR ON OCPR.CardCode = OCRD.CardCode
INNER JOIN OOND ON OOND.IndCode = OCRD.IndustryC
INNER JOIN OSLP ON OCRD.SlpCode = OSLP.SlpCode
WHERE OCRG.GroupName <> 'Retail' AND
OOND.IndName = 'Aged Care' and
OCRD.frozenFor = 'N' AND
OCPR.E_MailL LIKE '%#%'
ORDER BY OCRD.CardCode ASC, OCRD.E_MAIL

SQL Server : SUM DISTINCT GROUP BY

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

How to exclude a group of items from a report in SQL for Lansweeper

I have a SQL query that runs a report for unauthorized software. There is some computers that I want to exclude from the report, I created a group containing the computers that need to be exempt. I can not get the where not command to work in conjunction with the other variables. I am green to SQL so this might be simple but I can not figure out what is wrong with my syntax.
The group name that I want excluded is 'Test'. Without the not command it will show only the computers in the group. So the group works I just need it to do the inverse and only show what is not in the group.
Select Top 1000000 tblAssets.AssetID,
tblAssets.AssetUnique,
tblAssets.Description,
tblSoftwareUni.softwareName As Software,
tblSoftware.softwareVersion As Version,
tblSoftware.Lastchanged,
tsysOS.Image As icon,
tblAssetGroups.AssetGroup
From tblSoftware
Inner Join tblAssets On tblSoftware.AssetID = tblAssets.AssetID
Inner Join tblSoftwareUni On tblSoftware.softID = tblSoftwareUni.SoftID
Inner Join tblAssetCustom On tblAssets.AssetID = tblAssetCustom.AssetID
Inner Join tsysOS On tblAssets.OScode = tsysOS.OScode
Inner Join tblAssetGroupLink On tblAssets.AssetID = tblAssetGroupLink.AssetID
Inner Join tblAssetGroups On tblAssetGroups.AssetGroupID =
tblAssetGroupLink.AssetGroupID
Where Not tblAssetGroups.AssetGroup = 'Noah Test' And tblSoftwareUni.Approved =
2 And tblAssetCustom.State = 1
Order By tblAssets.AssetName,
Software
For this operation, you can use the LIKE keyword. Except, as you indicated, we need NOT
So,
Select Top 1000000 tblAssets.AssetID,
tblAssets.AssetUnique,
tblAssets.Description,
tblSoftwareUni.softwareName As Software,
tblSoftware.softwareVersion As Version,
tblSoftware.Lastchanged,
tsysOS.Image As icon,
tblAssetGroups.AssetGroup
From tblSoftware
Inner Join tblAssets On tblSoftware.AssetID = tblAssets.AssetID
Inner Join tblSoftwareUni On tblSoftware.softID = tblSoftwareUni.SoftID
Inner Join tblAssetCustom On tblAssets.AssetID = tblAssetCustom.AssetID
Inner Join tsysOS On tblAssets.OScode = tsysOS.OScode
Inner Join tblAssetGroupLink On tblAssets.AssetID = tblAssetGroupLink.AssetID
Inner Join tblAssetGroups On tblAssetGroups.AssetGroupID =
tblAssetGroupLink.AssetGroupID
Where tblAssetGroups.AssetGroup NOT LIKE '%Noah Test%' And tblSoftwareUni.Approved =
2 And tblAssetCustom.State = 1
Order By tblAssets.AssetName,
Software
If the text is always going to just be Noah Test, you could use NOT EQUALS, which is evaluated with != or <>
Select Top 1000000 tblAssets.AssetID,
tblAssets.AssetUnique,
tblAssets.Description,
tblSoftwareUni.softwareName As Software,
tblSoftware.softwareVersion As Version,
tblSoftware.Lastchanged,
tsysOS.Image As icon,
tblAssetGroups.AssetGroup
From tblSoftware
Inner Join tblAssets On tblSoftware.AssetID = tblAssets.AssetID
Inner Join tblSoftwareUni On tblSoftware.softID = tblSoftwareUni.SoftID
Inner Join tblAssetCustom On tblAssets.AssetID = tblAssetCustom.AssetID
Inner Join tsysOS On tblAssets.OScode = tsysOS.OScode
Inner Join tblAssetGroupLink On tblAssets.AssetID = tblAssetGroupLink.AssetID
Inner Join tblAssetGroups On tblAssetGroups.AssetGroupID =
tblAssetGroupLink.AssetGroupID
Where tblAssetGroups.AssetGroup != 'Noah Test' And tblSoftwareUni.Approved =
2 And tblAssetCustom.State = 1
Order By tblAssets.AssetName,
Software

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 ?