Home Database Mysql Tutorial 最小公约数

最小公约数

Jun 07, 2016 pm 03:57 PM
bom two columns Summary smallest friend need

[摘要]一个朋友在展BOM的时候有这样的需求,两列字段(数值):A ,B A=用量,B=底数,组成用量=用量/底数。A/B,若能被整除,显示整除的结果,若不能整除显示分数形式A/B(分数形式要是约分后的格式)3/6=1/3。 我的思路: 1.SQL取余判断是否能被整除 2.撰

[摘要]一个朋友在展BOM的时候有这样的需求,两列字段(数值):A ,B A=用量,B=底数,组成用量=用量/底数。A/B,若能被整除,显示整除的结果,若不能整除显示分数形式A/B(分数形式要是约分后的格式)3/6=1/3。

我的思路:

1.SQL取余判断是否能被整除

2.撰写两个整形数值的最大公约数,然后分子与分母分别除以最大公约数。

其中进行转换化成字符串格式。

一、自定义最大公约数函数:

Create FUNCTION [dbo].[GetMaxgongyue]
(
	@num1   int ,
	@num2  int 
)
RETURNS  int
as 
begin
DECLARE @times INT 
DECLARE @min INT 
DECLARE @result BIGINT 

IF( @num1 >= @num2 ) 
  SET @min=@num2 
ELSE 
  SET @min=@num1 

SET @times=@min 

WHILE(@times <= @min ) 
  BEGIN 
      IF( @num1%@times = 0 
          AND @num2%@times = 0 ) 
        BEGIN 
            SET @result=@times 

            BREAK 
        END 

      SET @times=@times - 1 
  END 

return @result
end
Copy after login
二、测试
drop table tt
Create table tt(t1  int,t2  int)
insert into  tt(t1 ,t2 )values(1,3)
insert into  tt(t1 ,t2 )values(2,4)
insert into  tt(t1 ,t2 )values(3,9)
insert into  tt(t1 ,t2 )values(6,3)
insert into  tt(t1 ,t2 )values(4,2)
insert into  tt(t1 ,t2 )values(2,331)
select t1,t2,t1%t2 取余,
case when t1%t2<>0 and dbo.GetMaxgongyue(t1,t2)=1 then CAST(t1 as NCHAR(4))+&#39;/&#39;+ CAST(t2 as NCHAR(4)) 
     when t1%t2<>0 and dbo.GetMaxgongyue(t1,t2)<>1 then CAST(t1/dbo.GetMaxgongyue(t1,t2) as NCHAR(4))+&#39;/&#39;+ CAST(t2/dbo.GetMaxgongyue(t1,t2) as NCHAR(4)) 
else cast(cast(t1/t2 as decimal(9,5)) as NCHAR(20)) end result
from tt
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between TikTok fans and friends? How to increase fans? What is the difference between TikTok fans and friends? How to increase fans? Mar 07, 2024 pm 09:52 PM

As one of the most popular short video platforms in China, Douyin has attracted a large number of users and fans. On TikTok, we often hear about fans and friends, but do you know what the difference is between TikTok fans and friends? This question will be answered in detail below. 1. What is the difference between Douyin fans and friends? On TikTok, fans are people who are interested in your content and choose to follow you. They will interact with you through likes, comments, etc. to establish a virtual connection. Friends, on the other hand, are people who know each other in real life and have real social relationships with each other. On social platforms like Douyin, the methods and depth of interactions between fans and friends may vary, but they are all important elements in building and maintaining a social network. The number of fans refers to those who follow your account

Today's AI may not be able to meet the needs of the board game circle Today's AI may not be able to meet the needs of the board game circle May 27, 2023 pm 11:53 PM

I don’t want AI to destroy the board game market, but I do want to try a German-style board game designed by pure AI. Article/Lao Lu If you want to talk about what is the hottest thing after this year’s Spring Festival, it is undoubtedly the AI ​​that is about to pass the Turing test. First there were the "amazing" AI drawings, and then there were the constantly surprising ChatGPT and NewBing, all of which are stimulating people's neurons and "warning" people about the future. For example, the various public relations drafts written by people using ChatGPT are completely comparable to those written by some public relations companies, and the effects of AI-drawn scenery paintings can meet various needs. Google has also begun to consider gradually reducing its recruitment needs for positions that can be replaced by AI. What also appeared at the same time was the worry of many people

Choose the applicable Go version, based on needs and features Choose the applicable Go version, based on needs and features Jan 20, 2024 am 09:28 AM

With the rapid development of the Internet, programming languages ​​are constantly evolving and updating. Among them, Go language, as an open source programming language, has attracted much attention in recent years. The Go language is designed to be simple, efficient, safe, and easy to develop and deploy. It has the characteristics of high concurrency, fast compilation and memory safety, making it widely used in fields such as web development, cloud computing and big data. However, there are currently different versions of the Go language available. When choosing a suitable Go language version, we need to consider both requirements and features. head

Demand exceeds supply! Demand for Huawei's computing power GPU exceeds one million units, prices have risen sharply Demand exceeds supply! Demand for Huawei's computing power GPU exceeds one million units, prices have risen sharply Mar 23, 2024 pm 08:51 PM

In the current wave of science and technology, computing power, as the core driving force for the development of cutting-edge technologies such as artificial intelligence and big data, has become increasingly important. As a domestic technology giant, Huawei’s self-developed computing power GPU has attracted much attention in the market. However, with the surge in demand, the supply of Huawei's computing GPUs has become stretched, and prices have also risen. According to sources, Huawei’s computing GPU shipments are expected to be approximately 100,000 pieces in 2023, and production capacity is expected to increase to hundreds of thousands of pieces in 2024. However, the number of orders received so far has reached millions of pieces, especially in January this year, the order volume reached hundreds of thousands. This shortage of supply has led to extended delivery times for Huawei's computing GPUs, and has also driven prices to continue to rise. from

Which array element has the smallest sum of absolute differences? Which array element has the smallest sum of absolute differences? Aug 29, 2023 am 10:09 AM

Here we will see an interesting problem. We have an array 'a' containing N elements. We need to find an element x that minimizes the value of |a[0]-x|+|a[1]-x|+...+|a[n-1]-x|. Then we need to find the minimized sum. Suppose the array is: {1,3,9,6,3}, and now x is 3. So the sum is |1-3|+|3-3|+|9-3|+|6-3|+|3-3|=11. To solve this problem, we need to choose the median of the array as x. If the size of the array is even, there will be two median values. They are all the best choices for x. Algorithm minSum(arr,n)begin &

What are the dom and bom objects? What are the dom and bom objects? Nov 13, 2023 am 10:52 AM

There are 5 DOM objects including "document", "element", "Node", "Event" and "Window"; 2. "window", "navigator", "location" and "history" and "screen" and other 5 BOM objects.

What is the difference between bom and dom What is the difference between bom and dom Nov 13, 2023 pm 03:23 PM

BOM and DOM are different in terms of role and function, relationship with JavaScript, interdependence, compatibility of different browsers, and security considerations. Detailed introduction: 1. Role and function. The main function of BOM is to operate the browser window. It provides direct access and control of the browser window. The main function of DOM is to convert the web document into an object tree, allowing developers to Use this object tree to obtain and modify the elements and content of the web page; 2. Relationship with JavaScript, etc.

Considering budget and needs, is iQOO Neo8 Pro or iQOO Neo9 more suitable? Considering budget and needs, is iQOO Neo8 Pro or iQOO Neo9 more suitable? Mar 24, 2024 pm 05:39 PM

As the competition in the smartphone market becomes increasingly fierce, consumers often consider many factors when choosing a new mobile phone, among which budget and demand are one of the two most important considerations. Among many mobile phone brands, iQOO, as an emerging brand that has attracted much attention, has launched a series of well-received products, among which iQOO Neo8 Pro and iQOO Neo9 have attracted much attention from consumers. So, considering the budget and needs, is it more suitable to choose iQOONeo8Pro or iQOONeo9?

See all articles