'How to calculate LCM value using SQL Server?

I have below data and I need to calculate LCM (Lowest calculate multiply) value based on group id using a T-SQL query. Your help would be appreciated.

Groupid GroupValue
------------------
1        2
1        4
1        6
2        5
2        5
2       10
3        3
3       12
3        6
3        9

Expected result is below.

Groupid   GroupLCM
------------------
   1        12
   2        10
   3        36


Solution 1:[1]

One possible way is to use tally tables like below

See working demo

; with detailedSet as 
(
    select 
        Groupid,
        GroupValue=abs(GroupValue),
        biggest=max(GroupValue) over (partition by Groupid),
        totalNumbers= count(1) over (partition by Groupid)
    from num
    )
,   
possibleLCMValues as 
(
    select Groupid, counter
    from detailedSet b
    cross apply 
    (
select counter= row_number() over ( order by (select null))  * biggest 
from sys.objects o1 cross join sys.objects o2
)c
where c.counter%GroupValue =0
group by Groupid, counter
having count(1)=max(totalNumbers)
)
,
LCMValues as
(
    select 
        Groupid,
        LCM=min(counter) 
    from possibleLCMValues
    group by Groupid
)

select * from LCMValues

Solution 2:[2]

I found the solution which I post on below stack flow question. In Final result table we just use max value again group id and we get LCM value.

Just note Like, I post this question for more optimize solution to remove for loop otherwise it working properly using for loop as well.

How to update the column without loop in SQL Server?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 NP007