English 中文(简体)
页: 1 按“重量”分类的订购记录
原标题:SQL Ordering records by "weight"

我们有一个以表格中“优先”号记录的制度。 我们根据表格的内容,例如,确定优先事项。

UPDATE table
SET priority=3
WHERE processed IS NULL

UPDATE table
SET priority=2
WHERE balance>50

UPDATE table
SET priority=1
WHERE value= blah 

(请无视以下事实:优先事项之间可能出现重叠:) iii

该表按优先顺序处理,因此,“价值”一栏中的所有各行都首先使用。

我的任务是增加一种选择,使记录具有可确定的“权重”。 例如,我们把处理工作50%作为优先工作1,25%优先工作2和25%优先工作3。 因此,从上述情况来看,每100个记录中,有50个是“价值”为“lah”,其中25个是“平衡”大于50个。

我试图说明如何做到这一点:对“优先权”的某种加权价值似乎是最佳的,但我可以 how头来看待这一点。 谁能帮助?

EDIT: 道歉应当说:这是在2008年多指标类集调查上进行的。

最佳回答

通常的想法是把任务收集到包 of中,在边境总数上进行区分:

select
  task_id
from (  
  select 
    task_id, 
    ((task_priority_order - 1) / task_priority_density) as task_processing_order
  from (
    select
      t.task_id                                            as task_id, 
      t.priority                                           as task_priority, 
      row_number() 
        over (partition by t.priority order by t.priority) as task_priority_order,
      case
        when t.priority = 3 then 50
        when t.priority = 2 then 25
        when t.priority = 1 then 25
      end                                                  as task_priority_density
    from
      table t
  )
)
order by task_processing_order

In the diapason from 0.0 to 0.(9) we got 100 records constructed from first 50 records with priority 3, first 25 records with priority 2 and first 25 records with priority 1.

接下来的迪帕松从1.0到1(9)是下一个记录桶。

如果再没有其他具有一定价值的任务,剩余的任务将按同样的比例放在桶内。 例如,如果任务不够,优先事项3,那么剩余任务将按50/50的比例安排。

任务——有些是确定任务的关键。

P.S. Sorry,我现在可以测试这一询问,因此,我非常赞赏任何合成物的更正。

<>Update: 意见更正。

问题回答

根据测试说明,产出如下。 如果你会就最终结果制定一些规则,那么我愿意再看一下。

<>Results

Priority    Processed       Balance Value
3           NULL            NULL    NULL
NULL        0               49      NULL
NULL        1               49      NULL
NULL        0               50      NULL
NULL        1               50      NULL
2           0               51      NULL
2           1               51      NULL
2           0               51      Notblah
1           1               51      blah

www.un.org/Depts/DGACM/index_spanish.htm 测试稿

DECLARE @Table TABLE (Priority INTEGER, Processed BIT, Balance INTEGER, Value VARCHAR(32))

INSERT INTO @Table VALUES 
  (NULL, NULL, NULL, NULL)
  , (NULL, 0, 49, NULL)
  , (NULL, 1, 49, NULL)
  , (NULL, 0, 50, NULL)
  , (NULL, 1, 50, NULL)
  , (NULL, 0, 51, NULL)
  , (NULL, 1, 51, NULL)
  , (NULL, 0, 51,  Notblah )
  , (NULL, 1, 51,  blah )

UPDATE @table SET priority=3 WHERE processed IS NULL
UPDATE @table SET priority=2 WHERE balance > 50
UPDATE @table SET priority=1 WHERE value =  blah 

SELECT  *
FROM    @table




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签