SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Lowst Common Dominator between two numbers
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sergeant_time
Yak Posting Veteran

68 Posts

Posted - 08/01/2012 :  12:53:41  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 08/01/2012 :  14:35:20  Show Profile  Reply with Quote
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;

Edited by - sunitabeck on 08/01/2012 14:36:22
Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2865 Posts

Posted - 08/01/2012 :  15:05:17  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000