question

Rahul R avatar image
0 Likes"
Rahul R asked Jason Lightfoot commented

SQL distinct COUNT and GROUP BY aggregation

Hi,

I am trying to transform the input table but finding it difficult without DISTINCT COUNT. I am assuming I will have to use GROUP BY and joins to accomplish this transformation. Any help is appreciated.

Input

1676585827918.png


Output:

1676585851641.png

Here is a simulation model with sample data

SQL_test1.fsm

FlexSim 22.2.3
sql queriessql querysql joindistinct
1676585827918.png (31.3 KiB)
1676585851641.png (5.1 KiB)
sql-test1.fsm (28.3 KiB)
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Jason Lightfoot avatar image
0 Likes"
Jason Lightfoot answered Jason Lightfoot commented

I'd probably write a script for this but you can get close in SQL:

Table.query("SELECT a.Task_id, a.Task_assigned_at, a.Type, b.numSKUs,c.numTotes FROM Input a JOIN \
(SELECT Task_id,COUNT(SKU) AS numSKUs FROM  (SELECT Task_id, SKU FROM Input GROUP BY Task_id, SKU)) b on b.Task_id=a.Task_id \
JOIN  (SELECT Task_id,COUNT(tote_id) AS numTotes FROM (SELECT Task_id, tote_id FROM Input GROUP BY Task_id, tote_id)) c  ON c.Task_id=a.Task_id GROUP BY a.Task_id").cloneTo("Output")

COUNT (DISTINCT SKU) would be simpler - you could suggest it in the ideas section.

All that's left is putting the function to convert the datetime string and find the minimum.

· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Rahul R avatar image Rahul R commented ·
@Jason Lightfoot -Thank you for the sample code. By script, do you mean to traverse through the global table and create the output table?
0 Likes 0 ·
Jason Lightfoot avatar image Jason Lightfoot ♦♦ Rahul R commented ·
Exactly - I'd probably use a nested Map type (a map containing other maps) to register the unique Skus and tote IDs for each taskid
0 Likes 0 ·