Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Lowst Common Dominator between two numbers

Author  Topic 

sergeant_time
Yak Posting Veteran

73 Posts

Posted - 2012-08-01 : 12:53:41
Is there a code to get the Lowest Common Dominator bewteen to numbers? Or is there a expression to get the Lowest Common Dominator?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-01 : 14:35:20
I don't know of an expression or built-in function in T-SQL to do this, but if you have a numbers table it is easy enough. In the code below, I am constructing a numbers table and then calculating the LCD.
CREATE TABLE #N(n INT NOT NULL PRIMARY KEY CLUSTERED );
;WITH N(n) AS (SELECT 1 UNION ALL SELECT n+1 from N WHERE n < 1000)
INSERT INTO #N SELECT * FROM N OPTION (MAXRECURSION 0);

DECLARE @x1 INT, @x2 INT;
SET @x1 = 9; SET @x2 = 6;

SELECT TOP 1
@x1*N1.n
FROM
#N N1 CROSS JOIN #N N2
WHERE
N1.n * @x1 = N2.n*@x2
ORDER BY 1;

DROP TABLE #N;
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-08-01 : 15:05:17
Borrowing Sunita's code, I think this give the LCD

CREATE TABLE #N(n INT NOT NULL PRIMARY KEY CLUSTERED );
;WITH N(n) AS (SELECT 2 UNION ALL SELECT n+1 from N WHERE n < 1000)
INSERT INTO #N SELECT * FROM N OPTION (MAXRECURSION 0);

DECLARE @x1 INT, @x2 INT;
SET @x1 = 7; SET @x2 = 6;



SELECT TOP 1
N1.n
FROM
#N N1
WHERE
@x1*1.0 / N1.n = @x1 /N1.n
and @x2*1.0 / N1.n = @x2 / N1.n
UNION
SELECT 1
ORDER BY 1 desc;

DROP TABLE #N;


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -