SQL:按1到10的比例排列数字

时间:2021-10-16 22:51:50

I have a table in a SQL Server 2008 database with a number column that I want to arrange on a scale 1 to 10.

我在SQL Server 2008数据库中有一个表,我想在1到10的比例上安排一个数字列。

Here is an example where the column (Scale) is what I want to accomplish with SQL

下面是一个示例,其中列(Scale)是我想用SQL完成的

Name    Count   (Scale)
----------------------
A       19      2       
B       1       1
C       25      3
D       100     10
E       29      3
F       60      7   

In my example above the min and max count is 1 and 100 (this could be different from day to day).

在我上面的例子中,最小和最大计数是1和100(这可能每天都不同)。

I want to get a number to which each record belongs to.

我想得到每个记录所属的数字。

1 = 0-9
2 = 10-19
3 = 20-29 and so on...

It has to be dynamic because this data changes everyday so I can not use a WHERE clause with static numbers like this: WHEN Count Between 0 and 10...

它必须是动态的,因为这个数据每天都在变化所以我不能使用带有静态数字的WHERE子句,如下所示:WHEN Count 0到10之间...

4 个解决方案

#1


1  

Try this, though note technically the value 100 doesn't fall in the range 90-99 and therefore should probably be classed as 11, hence why the value 60 comes out with a scale of 6 rather than your 7:

尝试这一点,虽然技术上注意值100不在90-99的范围内,因此应该被归类为11,因此为什么值60的比例为6而不是7:

SQL Fiddle

MS SQL Server 2008 Schema Setup:

MS SQL Server 2008架构设置:

Query 1:

create table #scale
(
    Name Varchar(10),
    [Count] INT
)

INSERT INTO #scale
VALUES
    ('A', 19),
    ('B', 1),
    ('C', 25),
    ('D', 100),
    ('E', 29),
    ('F', 60)


SELECT name, [COUNT],  
    CEILING([COUNT] * 10.0 / (SELECT MAX([Count])  - MIN([Count]) + 1 FROM #Scale)) AS [Scale]
FROM #scale

Results:

| NAME | COUNT | SCALE |
|------|-------|-------|
|    A |    19 |     2 |
|    B |     1 |     1 |
|    C |    25 |     3 |
|    D |   100 |    10 |
|    E |    29 |     3 |
|    F |    60 |     6 |

This gets you your answer where 60 becomes 7, hence 100 is 11:

这可以得到你的答案,其中60变为7,因此100是11:

SELECT name, [COUNT],  
    CEILING([COUNT] * 10.0 / (SELECT MAX([Count])  - MIN([Count]) FROM #Scale)) AS [Scale]
FROM #scale

#2


1  

You can make Scale column a PERSISTED COMPUTED column as:

您可以将“缩放”列设为PERSISTED COMPUTED列,如下所示:

alter table test drop column Scale

ALTER TABLE test ADD
Scale AS (case when Count between 0 and 9 then 1 
               when Count between 10 and 19 then 2
               when Count between 20 and 29 then 3
               when Count between 30 and 39 then 4
               when Count between 40 and 49 then 5 
               when Count between 50 and 59 then 6
               when Count between 60 and 69 then 7
               when Count between 70 and 79 then 8 
               when Count between 80 and 89 then 9
               when Count between 90 and 100 then 10
          end
          )PERSISTED
GO

DEMO

#3


1  

select ntile(10) over (order by [count])

#4


0  

WITH MinMax(Min, Max) AS (SELECT MIN(Count), MAX(Count) FROM Table1)
SELECT Name, Count, 1 + 9 * (Count - Min) / (Max - Min) AS Scale
FROM Table1, MinMax

#1


1  

Try this, though note technically the value 100 doesn't fall in the range 90-99 and therefore should probably be classed as 11, hence why the value 60 comes out with a scale of 6 rather than your 7:

尝试这一点,虽然技术上注意值100不在90-99的范围内,因此应该被归类为11,因此为什么值60的比例为6而不是7:

SQL Fiddle

MS SQL Server 2008 Schema Setup:

MS SQL Server 2008架构设置:

Query 1:

create table #scale
(
    Name Varchar(10),
    [Count] INT
)

INSERT INTO #scale
VALUES
    ('A', 19),
    ('B', 1),
    ('C', 25),
    ('D', 100),
    ('E', 29),
    ('F', 60)


SELECT name, [COUNT],  
    CEILING([COUNT] * 10.0 / (SELECT MAX([Count])  - MIN([Count]) + 1 FROM #Scale)) AS [Scale]
FROM #scale

Results:

| NAME | COUNT | SCALE |
|------|-------|-------|
|    A |    19 |     2 |
|    B |     1 |     1 |
|    C |    25 |     3 |
|    D |   100 |    10 |
|    E |    29 |     3 |
|    F |    60 |     6 |

This gets you your answer where 60 becomes 7, hence 100 is 11:

这可以得到你的答案,其中60变为7,因此100是11:

SELECT name, [COUNT],  
    CEILING([COUNT] * 10.0 / (SELECT MAX([Count])  - MIN([Count]) FROM #Scale)) AS [Scale]
FROM #scale

#2


1  

You can make Scale column a PERSISTED COMPUTED column as:

您可以将“缩放”列设为PERSISTED COMPUTED列,如下所示:

alter table test drop column Scale

ALTER TABLE test ADD
Scale AS (case when Count between 0 and 9 then 1 
               when Count between 10 and 19 then 2
               when Count between 20 and 29 then 3
               when Count between 30 and 39 then 4
               when Count between 40 and 49 then 5 
               when Count between 50 and 59 then 6
               when Count between 60 and 69 then 7
               when Count between 70 and 79 then 8 
               when Count between 80 and 89 then 9
               when Count between 90 and 100 then 10
          end
          )PERSISTED
GO

DEMO

#3


1  

select ntile(10) over (order by [count])

#4


0  

WITH MinMax(Min, Max) AS (SELECT MIN(Count), MAX(Count) FROM Table1)
SELECT Name, Count, 1 + 9 * (Count - Min) / (Max - Min) AS Scale
FROM Table1, MinMax