Using Count() in BAQ - Incorrect Syntax

I am trying to create a query that gives a count of times a part/SN combo has been on a Job.

We repair equipment for customers and have been asked that if a unit has come in for repair more than 2 times to junk the unit. We create a unique Sales Order and Job each time it comes in, so I figured I would count the previous jobs instead of trying to count this against the SNTran table.

I was asked to be able to run this by all open jobs, so I have a query that is only pulling in Open Jobs. I am then creating a subquery pulling in the JobHead table again and linking it to the Top Level query by Company, PartNum, ShortChar02(where we store the SN). I am then creating a Calculated field and inputting count ( * ) (following an example in the ICE Tools User Guide). In the expression editor, it says Syntax OK; however when I go to the Analyze tab and click Analyze, I get an “Incorrect syntax near ‘(’” error message.

Even if I replace * with a field from JobHead, I receive the same error message.

I know the logic in the joins to pull the right records is spotty, but I can fix that later. I’ve checked the Group By boxes in the subquery that I’m using for my joins.

What am I missing? I feel like this should be simple so any assistance is appreciated.

1 Like

The basic idea is that the calculated field has to be an integer type, and then you group by all non-calculated fields that you are displaying.

Yeah I think you could use any field inside the count(). I try to be specific, but I think it’s honestly irrelevant–it’s not a “count unique entries”; it just counts rows.

The Calculated Field must be a nvarchar, Epicor has a bug where it doesnt work if its an integer. Last time I ran into it was on 10.2.300.x

Not sure why COUNT(*) doesnt work with int but does with nvarchar @Dmitry_Kashulin

2 Likes

@hkeric.wci Sure it does…if you put something inside of count():

image

image

2 Likes

But that is wild with count(*). I just tried that as you said with nvarchar. Weird.

Try it now in a Sub-Query. I just ran into it yesterday in 10.2.300.2x, let me try it in 10.2.400.x

So make a Sub-Query add COUNT(*) on a calc field.

Then in your TopLevel Query make a Calculated Field that calls the Sub-Query (Sub Select basically)

1 Like

Interesting so in 10.2.400 I can use it in a Sub-Query, I couldnt in 10.2.300 even COUNT(*) works in 10.2.400. Maybe it was something in 10.2.300

image

image

Anyways :slight_smile: works now, so perhaps a bug previously (I know I had it in 10.2.300.2x) or its one of those Bugs that only show up when you have few more tables + few more calculated fields.

But in 10.2.300.9 works fine hm.
image

O well :slight_smile: try nvarchar if integer doesnt work for you.

Whew had me worried for a moment. Until I got to the bottom of this bost. I just went through this today. I needed to create a list from the partbin, that excluded any rows that had a count of lotnumber greater than 1. I was pretty sure it worked. On 10.2.400.16, so maybe you are right.

1 Like

Interestingly, here a bit of information about performance and using count(*)

1 Like

Thanks for nvarchar suggestions, but that isn’t working either. I’ve included screens below in case someone sees something I missed. I’m experiencing the same results in 10.2.200.31 and 10.2.500.2.

I even created a BAQ against PartTran where I only tried to count the TranType and that gave the same error message.

Out of curiosity can you rename your Field Name to something else other than Count

Because when I name my COLUMN Count I get the Error! Woot and when I change it to something else, it works.

Maybe thats what it was @JasonMcD

image

6 Likes

That was it, didn’t even think about the field name! Thanks!

@tmcmullen you can also do Sub-Select SubQuery.

Then you wouldnt need to do GROUP BY on your Top-Query.

In a Nutshell:

  1. Make a Sub-Query but DO NOT Use it on the Designer
  2. On your TopLevel Query add the Sub-Query to a Calculated Field
  3. On the Sub-Query under SubQuery Criteria you can add WHERE Clause.

Example:

SubQuery1:


SubQuery2:


Result:


image

Basically in a simpler view it does

SELECT (SELECT count(*) FROM table WHERe...) as YourCalculatedColumn

It is proven that JOINs are usually faster and sometimes SQL will even convert your SUB-SELECT when it can, but if you get into a more complex scenario where you are getting tangled in Sub-Queries, GROUPS, etc… sometimes Sub-Selects work easier. FYI :slight_smile: thought Id share something.

The best time they work is when you need to get let’s say The Last Approved Revision nothing easier than a SubSelect and then Order By ApprovedDate DESC, but when not using Aggregate like count(*) then you must make sure you set your SubQuery2 to return only 1 row, 1 column.

image

7 Likes

Good lesson learnt from this post, and it’s the simple stuff we forget or just do things because we have been doing it so long, we don’t event think about it.

Note to self , don’t use TSQL reserved words as field names for calculated fields :slight_smile:

Yep, this one just got bookmarked. Curly braces? Subquery criteria? Table values? I feel like a beginner.

Need a mind-blown emoji reaction to posts. :exploding_head:

1 Like

Any idea how to get total number of rows returned? Lets say if I do a filter of dates. Query returned 120 rows which shows under “Query Execution Messages”. I want to know how many rows returned in that filter/list.

Thank you in advance!!

You do a calculacted field

count() over (partition by company)

With your partition by something that’s the same for all the rows.

This will count all of the rows, and add this onto a column. You can hide this in the grid and show it on the tracker at view only, and it won’t change since it’s the same for all of the rows.

2 Likes

Thank you @Banderson for your reply. This works perfectly the way I wanted.

Much appreciated!!

1 Like