SQL distinct COUNT and GROUP BY aggregation

SQL distinct COUNT and GROUP BY aggregation

rhramac
Enthusiast Enthusiast
257 Views
3 Replies
Message 1 of 4

SQL distinct COUNT and GROUP BY aggregation

rhramac
Enthusiast
Enthusiast

[ FlexSim 22.2.3 ]

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

0 Likes
Accepted solutions (1)
258 Views
3 Replies
Replies (3)
Message 2 of 4

jason_lightfootVL7B4
Autodesk
Autodesk
Accepted solution

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.

0 Likes
Message 3 of 4

rhramac
Enthusiast
Enthusiast
@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
Message 4 of 4

jason_lightfootVL7B4
Autodesk
Autodesk
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