# Use "Count" function while ignoring duplicates

**URL:** <https://www.epiusers.help/t/use-count-function-while-ignoring-duplicates/48236>\
**Category:** Epicor ERP 10\
**Created:** [September 27, 2018, 2:40pm UTC](https://www.epiusers.help/t/use-count-function-while-ignoring-duplicates/48236 "2018-09-27T14:40:56Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![hymal7](https://www.epiusers.help/user_avatar/www.epiusers.help/hymal7/32/9934_2.png) [@hymal7](https://www.epiusers.help/u/hymal7)\
**Post date:** [September 27, 2018, 2:40pm UTC](https://www.epiusers.help/t/use-count-function-while-ignoring-duplicates/48236/1 "2018-09-27T14:40:56Z")

</div>

Hi all,

I have a simple BAQ that counts the number of bins for a part since the part is located in multiple bins.  
It works OK but it also counts duplicate bin numbers i.e. if there are multiple lot numbers in the same bin, it will count the same bin more than once.  
Is it possible to do this count where it ignores multiple occurrences?

In the image below, Subquery2 is the inner query that contains the “count” function and ignores some specific bins.  
Subquery1 is the toplevel. i’ve selected a few parts to test.

 ![image](https://epiusers.help/uploads/default/original/2X/e/eb1672421691a29b1b26d7e05bfad344ff88e74b.png)

 ![image](https://epiusers.help/uploads/default/original/2X/6/6d4080d6a5f1f1805a91bb4b4f4b1421966dca73.png)

![image](https://epiusers.help/uploads/default/original/2X/5/5214c6725a3481e7c32fe23742a139e9639f507f.png)

---

<div class="post-metadata">

**Author:** ![knash](https://www.epiusers.help/user_avatar/www.epiusers.help/knash/32/74179_2.png) [@knash](https://www.epiusers.help/u/knash)\
**Post date:** [September 27, 2018, 2:47pm UTC](https://www.epiusers.help/t/use-count-function-while-ignoring-duplicates/48236/2 "2018-09-27T14:47:26Z")

</div>

the query phrase would be helpful here.

---

<div class="post-metadata">

**Author:** ![hymal7](https://www.epiusers.help/user_avatar/www.epiusers.help/hymal7/32/9934_2.png) [@hymal7](https://www.epiusers.help/u/hymal7)\
**Post date:** [September 27, 2018, 2:48pm UTC](https://www.epiusers.help/t/use-count-function-while-ignoring-duplicates/48236/3 "2018-09-27T14:48:51Z")

</div>

please see attached images for query phrase builder tables for both queries.

Thanks

---

<div class="post-metadata">

**Author:** ![knash](https://www.epiusers.help/user_avatar/www.epiusers.help/knash/32/74179_2.png) [@knash](https://www.epiusers.help/u/knash)\
**Post date:** [September 27, 2018, 2:50pm UTC](https://www.epiusers.help/t/use-count-function-while-ignoring-duplicates/48236/4 "2018-09-27T14:50:59Z")

</div>

the attached images did not come through.

---

<div class="post-metadata">

**Author:** ![hymal7](https://www.epiusers.help/user_avatar/www.epiusers.help/hymal7/32/9934_2.png) [@hymal7](https://www.epiusers.help/u/hymal7)\
**Post date:** [September 27, 2018, 2:52pm UTC](https://www.epiusers.help/t/use-count-function-while-ignoring-duplicates/48236/5 "2018-09-27T14:52:15Z")

</div>

sorry my bad, i will upload it

---

<div class="post-metadata">

**Author:** ![Mark\_Wonsil](https://www.epiusers.help/user_avatar/www.epiusers.help/mark_wonsil/32/51594_2.png) [@Mark\_Wonsil](https://www.epiusers.help/u/Mark_Wonsil)\
**Post date:** [September 27, 2018, 2:53pm UTC](https://www.epiusers.help/t/use-count-function-while-ignoring-duplicates/48236/6 "2018-09-27T14:53:52Z")

</div>

What does the link to the subquery look like? Does it include company? Are you multi-company?

---

<div class="post-metadata">

**Author:** ![knash](https://www.epiusers.help/user_avatar/www.epiusers.help/knash/32/74179_2.png) [@knash](https://www.epiusers.help/u/knash)\
**Post date:** [September 27, 2018, 2:55pm UTC](https://www.epiusers.help/t/use-count-function-while-ignoring-duplicates/48236/7 "2018-09-27T14:55:03Z")

</div>

That is not the query phrase I was asking for.

 ![Capture](https://epiusers.help/uploads/default/original/2X/e/ee8a1ef660366bbe3358f895959ed7b73e3903bb.jpeg)  
here is the location. You should then be able to copy and paste like this.

```
select 
	[Part].[PartNum] as [Part_PartNum],
	(left(Part.PartNum, (charindex('-',Part.PartNum)-1))) as [Calculated_BeforeDash],
	(SUBSTRING(Part.PartNum,CHARINDEX('-',Part.PartNum)+1,LEN(Part.PartNum))) as [Calculated_afterDash],
	(len(replace(Part.PartNum, '-', ''))) as [Calculated_LenWithoutDash],
	(len(Part.PartNum)-1) as [Calculated_LenPartMin1],
	(case when (Part.PartNum like '[0-9][0-9][0-9][-][0-9][0-9][0-9]') then 1 else 0 end) as [Calculated_formatcorrect]
from Erp.Part as Part
where (len(replace(Part.PartNum, '-', ''))) = Calculated.LenPartMin1 and Part.PartNum like '[0-9][0-9][0-9][-][0-9][0-9][0-9]'
```

---

<div class="post-metadata">

**Author:** ![hymal7](https://www.epiusers.help/user_avatar/www.epiusers.help/hymal7/32/9934_2.png) [@hymal7](https://www.epiusers.help/u/hymal7)\
**Post date:** [September 27, 2018, 2:55pm UTC](https://www.epiusers.help/t/use-count-function-while-ignoring-duplicates/48236/8 "2018-09-27T14:55:50Z")

</div>

Hi,

The link between queries is just Part.PartNum = PartBin.PartNum.  
There is only one company.  
There are multiple warehouses but I’ve put the filter in.

Thanks

---

<div class="post-metadata">

**Author:** ![hymal7](https://www.epiusers.help/user_avatar/www.epiusers.help/hymal7/32/9934_2.png) [@hymal7](https://www.epiusers.help/u/hymal7)\
**Post date:** [September 27, 2018, 2:57pm UTC](https://www.epiusers.help/t/use-count-function-while-ignoring-duplicates/48236/9 "2018-09-27T14:57:30Z")

</div>

sorry my bad.

here it is.

 ![image](https://epiusers.help/uploads/default/original/2X/4/408a957a4485d38ee32d3e2b72120cfd4f431d88.png)

select  
[Part].[PartNum] as [Part\_PartNum],  
[SubQuery2].[Calculated\_countBins] as [Calculated\_countBins]  
from Erp.Part as Part  
inner join (select  
[PartBin].[PartNum] as [PartBin\_PartNum],  
(count(\*)) as [Calculated\_countBins]  
from Erp.PartBin as PartBin  
where (PartBin.WarehouseCode = ‘main’ and not PartBin.BinNum in (‘desp’, ‘despatch’, ‘insp’, ‘spares’, ‘van’, ‘wip’, ‘zvan’))  
group by [PartBin].[PartNum]) as SubQuery2 on  
Part.PartNum = SubQuery2.PartBin\_PartNum  
where (Part.PartNum in (‘Part1’, ‘Part2’, ‘Part3’))

---

<div class="post-metadata">

**Author:** ![knash](https://www.epiusers.help/user_avatar/www.epiusers.help/knash/32/74179_2.png) [@knash](https://www.epiusers.help/u/knash)\
**Post date:** [September 27, 2018, 3:03pm UTC](https://www.epiusers.help/t/use-count-function-while-ignoring-duplicates/48236/10 "2018-09-27T15:03:03Z")

</div>

Like Mark asked are you multiple company? opps saw your answer.

---

<div class="post-metadata">

**Author:** ![knash](https://www.epiusers.help/user_avatar/www.epiusers.help/knash/32/74179_2.png) [@knash](https://www.epiusers.help/u/knash)\
**Post date:** [September 27, 2018, 3:04pm UTC](https://www.epiusers.help/t/use-count-function-while-ignoring-duplicates/48236/11 "2018-09-27T15:04:52Z")

</div>

Lets work on the subquery first

If you select \* from PartBin where WarehouseCode is Main

What are you seeing? Do you see the duplicates? Are the lots important?

If not then you might need to do another sub query to once you have your part and bin list.

it would look like this.

```
select PartBinGroup.Company, PartBinGroup.PartNum, COUNT(*) 
from 
(select PartBin.Company, PartBin.PartNum, PartBin.BinNum
 from Erp.PartBin as PartBin
 where PartBin.WarehouseCode = 'main' 
 and not PartBin.BinNum in ('desp', 'despatch', 'insp', 'spares', 'van', 'wip', 'zvan')
group by PartBin.Company, PartBin.PartNum, PartBin.BinNum) as PartBinGroup
Group By PartBinGroup.Company, PartBinGroup.PartNum
```

---

<div class="post-metadata">

**Author:** ![hymal7](https://www.epiusers.help/user_avatar/www.epiusers.help/hymal7/32/9934_2.png) [@hymal7](https://www.epiusers.help/u/hymal7)\
**Post date:** [September 27, 2018, 3:13pm UTC](https://www.epiusers.help/t/use-count-function-while-ignoring-duplicates/48236/12 "2018-09-27T15:13:23Z")

</div>

Im sorry i didnt follow you clearly, where in the BAQ did you want me to select \* from?  
The only \* i have is in the calculated field i.e. count(\*).

In our E10 database, there are certain parts with different lot numbers in the same bin number, therefore on part tracker there are multiple duplicate fields.

For the part on the example below, I would expect the count result = 3, not = 4 (which is what i am getting).

 ![image](https://epiusers.help/uploads/default/original/2X/2/2a6e748e35eb746e9fb7c71a2152fb8caf783cce.png)

---

<div class="post-metadata">

**Author:** ![knash](https://www.epiusers.help/user_avatar/www.epiusers.help/knash/32/74179_2.png) [@knash](https://www.epiusers.help/u/knash)\
**Post date:** [September 27, 2018, 3:25pm UTC](https://www.epiusers.help/t/use-count-function-while-ignoring-duplicates/48236/13 "2018-09-27T15:25:31Z")

</div>

This will get you started You can add your other criteria where needed.

This is 10.2.200 baq.

[test.baq](https://epiusers.help/uploads/default/original/2X/3/30b1a7f4868f26126e076894c14a0d83f9fab999.baq) (17.6 KB)

```
select 
	[PartBinGroup].[PartBin_Company] as [PartBin_Company],
	[PartBinGroup].[PartBin_PartNum] as [PartBin_PartNum],
	(Count(PartBinGroup.PartBin_BinNum)) as [Calculated_BinCount]
from (select 
	[PartBin].[Company] as [PartBin_Company],
	[PartBin].[PartNum] as [PartBin_PartNum],
	[PartBin].[BinNum] as [PartBin_BinNum]
from Erp.PartBin as PartBin
group by [PartBin].[Company],
	[PartBin].[PartNum],
	[PartBin].[BinNum]) as PartBinGroup
group by [PartBinGroup].[PartBin_Company],
	[PartBinGroup].[PartBin_PartNum]
```

---

<div class="post-metadata">

**Author:** ![Adam](https://www.epiusers.help/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@Adam](https://www.epiusers.help/u/Adam)\
**Post date:** [September 27, 2018, 3:25pm UTC](https://www.epiusers.help/t/use-count-function-while-ignoring-duplicates/48236/14 "2018-09-27T15:25:45Z")

</div>

Can you put a “group by” on the bin field in the subquery?

---

<div class="post-metadata">

**Author:** ![hymal7](https://www.epiusers.help/user_avatar/www.epiusers.help/hymal7/32/9934_2.png) [@hymal7](https://www.epiusers.help/u/hymal7)\
**Post date:** [September 27, 2018, 3:32pm UTC](https://www.epiusers.help/t/use-count-function-while-ignoring-duplicates/48236/15 "2018-09-27T15:32:09Z")

</div>

Hi Ken thanks a lot that worked !  
I can’t thank you enough.

Cheers !!
