Aggregating subquery values into a tilde delimited field

Hello, I’m trying to develop a BAQ that lists rows from my Part table and aggregate the PartSubs.SubPart into a single column making it ~ delimited.

The result I’m looking for is one output with:

  • Part.PartNum (i.e. MYPART)
  • Part.PartDescription (My Part Name)
  • Aggregated_SubPart (MYSECONDPART~MYTHIRDPART~MYFOURTHPART)

If this were raw query I’d have no trouble writing this out perfectly. This still BAQ interface is holding me back and making life more complicated. Unfortunately I can’t do this as a Kinetic Function, needs to be a BAQ as well.

My questions:

  • How do I aggregate i.e. STRING_AGG the values together?
  • I’m implementing this as a subquery but getting errors that SubPart isn’t indexed.
  • Is there no escape hatch for BAQs to write the query manually? cries

String_agg() works in a calculated field. Even though it’s not in the list, you can type it in.

Can you post more of what you tried so far? What you describe sounds easily do-able, you just have to find the right buttons in BAQ land.

Here’s the subquery and calculated field, don’t forget the “Group By” checkboxes.

Here’s the top level

Results. (We don’t use subs, so we don’t have more than one to list, but this will work)

This is so strange. When I check the syntax it reads: The multi-part identifier "PartSubs.SubPart" could not be bound.. Can I trouble you for an export of that query to have a look at how you implemented it? This is boggling my mind.

Ps. it’s so annoying that STRING_AGG isn’t listed!

When you add your field to the calculated field, find it in the menu and double click it to add it. Did you add that table more than once? If you did, it might have an alias on it.

Again, show me what you have, and I can probably see what you’re doing wrong.

I want to see what this says right here.

Query relationship:

Subquery fields:

Calculated fields:

I’m probably doing something stupidly simple incorrectly lol.

Can you grab the SQL from the query phrase screen too? I don’t see anything wrong in your screen shots.

/*  
 * Disclaimer!!! 
 * This is not a real query being executed, but a simplified version for general vision. 
 * Executing it with any other tool may produce a different result. 
 */

select  
	[Part].[Company] as [Part_Company], 
	[Part].[PartNum] as [Part_PartNum], 
	[Part].[PartDescription] as [Part_PartDescription], 
	[subParts].[Calculated_Subs] as [Calculated_Subs] 

from Erp.Part as [Part]
inner join  (select  
	[PartSubs].[Company] as [PartSubs_Company], 
	[PartSubs].[PartNum] as [PartSubs_PartNum], 
	(string_agg(PartSubs.SubPart, '~')) as [Calculated_Subs] 

from Erp.PartSubs as [PartSubs]
group by 
	[PartSubs].[Company], 
	[PartSubs].[PartNum])  as [subParts] on 
	  Part.Company = subParts.PartSubs_Company
	and  Part.PartNum = subParts.PartSubs_PartNum

lol it would have been faster for me to write this query manually :stuck_out_tongue:

/*  
 * Disclaimer!!! 
 * This is not a real query being executed, but a simplified version for general vision. 
 * Executing it with any other tool may produce a different result. 
 */

select  
	(STRING_AGG(PartSubs.SubPart, '~')) as [Calculated_Subs], 
	[Part].[SysRowID] as [Part_SysRowID], 
	[Part].[Company] as [Part_Company], 
	[Part].[PartNum] as [Part_PartNum], 
	[Part].[PartDescription] as [Part_PartDescription], 
	[SubParts].[Calculated_Subs] as [Calculated_Subs01] 
from Erp.Part as [Part]
inner join  (select  
	[PartSubs].[PartNum] as [PartSubs_PartNum], 
	[PartSubs].[Company] as [PartSubs_Company], 
	(STRING_AGG(PartSubs.SubPart, '~')) as [Calculated_Subs] 

from Erp.PartSubs as [PartSubs]
group by 
	[PartSubs].[PartNum], 
	[PartSubs].[Company])  as [SubParts] on 
	  Part.PartNum = SubParts.PartSubs_PartNum
	and  Part.Company = SubParts.PartSubs_Company
where (Part.PartNum = @partnum  
and Part.ProdCode = 'Moulding')

Do you try to create the calculated field in both levels? You just need to bring it in from the subquery like a field. Not re-write it.

Season 3 Wall GIF by The Simpsons

You’re right, I noticed that as you were replying. I must have left that in there accidentally in the original query before trying the subquery.

It’s working now :slight_smile: thanks for catching my stupidity bahaha.

Yeah, but now you learned something.

I learned that I hate GUIs even more? Haha. I’m one of those cli nerds (almost as bad as the vim people but not quite that level of chaotic :p).

Technically, you don’t need this to be a subquery either. You can do it all in the top level, (like you were trying), you just need to bring the partSub table into the top level. Obviously depending on what your end goal is.

Performance would be a factor here. This BAQ is used frequently and ultimately it comes down do if the subquery aggregation or join is fastest. I suspect the subquery would be worse for when I’m searching for one part (since it has to aggregate the whole table and combinations before joining).

Where are you using the BAQ? It’s a pretty simple one, and I can’t imagine the minute difference being noticeable anywhere unless you are running it multiple times in some sort of automation.

It’s not a huge deal, I’m just a perf nut. :stuck_out_tongue: if I can spend 10 minutes getting a 100ms optimization of a query I would. lol.