English 中文(简体)
分配预算,以填补LQ的排名部分
原标题:Distribute budget over for ranked components in SQL

假定我的预算为10美元(任何分类账),我想将其分配到有不同需求的外地的记录。 例:

rank     Req.    Fulfilled?  
1       $3           Y  
2       $4           Y  
3       $2           Y  
4       $3           N  

那些从1到3级的队伍应当得到实现,因为它们属于预算。 而排名第4位的人不应。

我希望能解决这个问题。

下面是我的初稿:

CREATE TABLE budget (
id VARCHAR (32),
budget INTEGER,
PRIMARY KEY (id));

CREATE TABLE component (
id VARCHAR (32),
rank INTEGER,
req INTEGER,
satisfied BOOLEAN,
PRIMARY KEY (id));

INSERT INTO budget (id,budget) VALUES ( 1 ,10);
INSERT INTO component (id,rank,req) VALUES ( 1 ,1,3);
INSERT INTO component (id,rank,req) VALUES ( 2 ,2,4);
INSERT INTO component (id,rank,req) VALUES ( 3 ,3,2);
INSERT INTO component (id,rank,req) VALUES ( 4 ,4,3);

事先感谢您的帮助。

Lee

最佳回答

你们所举的例子非常容易:

select rank, req,
       sum(req) over(order by rank) < (select budget from budget where id =  1 )
             as fulfilled
from component

但是,这并没有考虑到:

  • there are 2 units left over from the budget that could be allocated to a further component with a lower requirement
  • budget is allocated to components rank-first, not sure that s what you meant

因此,如果有一个构成部分(id=5, 等级=5, q=2)并且应当完成,则该数字就足够了。

TBH 我怀疑分配职能是最佳的灯塔,简单地通过“等级表”下令的盘问来操作,并根据目前的情况更新已完成的一栏。

问题回答

暂无回答




相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...

热门标签