1

我写了这个 sql 查询

update temp_calpoints1  
set perwet = (select perwet
              from process 
              where processid = temp_calpoints1.processid) 

截至目前,它为每个用户更新 10%,如下所示。

orderid            processid          uid            ordervalue            perwet
---------------------------------------------------------------------------------    
1                     1                1               10000              10
1                     1                2               10000              10
1                     1                3               10000              10
1                     2                1               10000              10
1                     2                2               10000              10
1                     3                1               10000              10

我希望如果超过 1 个用户以相同的顺序参与 1 个进程,它应该平均分配百分比

这意味着它必须像这样插入

orderid        processid             uid         ordervalue          perwet
------------------------------------------------------------------------------    
1                    1                1               10000             3.33 
1                     1               2               10000             3.33
1                     1               3               10000             3.33
1                     2               1               10000             5.00
1                     2               2               10000             5.00
1                     3               1               10000            10.00

任何的想法?

表结构

     CREATE TABLE [dbo].[temp_calpoints1](
     [orderid] [int] NULL,
     [processid] [int] NULL,
     [uid] [int] NULL,
     [ordervalue] [bigint] NULL,
     [perwet] [float] NULL
     ) ON [PRIMARY]



     CREATE TABLE [dbo].[process](
     [processid] [float] NULL,
     [processdesc] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
     [perwet] [int] NULL
     ) ON [PRIMARY]
4

4 回答 4

0
update temp_calpoints1  set perwet = (select perwet/count(uid)
  from process 
  where 
  processid=temp_calpoints1.processid group by uid)
于 2013-03-20T05:16:24.540 回答
0
update temp_calpoints1 set perwet = processcallpoints.perwet
from (select MAX(process.perwet) / COUNT(1) perwet from temp_calpoints1 inner join process on temp_calpoints1.processid = process.processid group by process.processid) processcallpoints

顺便说一句,你不能有 3.33,因为列类型是 int,所以你会得到 3

于 2013-03-20T06:17:52.220 回答
0

像这样的东西:

SQLFIDDLE示例

UPDATE temp_calpoints1  
SET perwet = (SELECT p.perwet/(SELECT CASE WHEN count(*) = 0
                                      THEN 1 ELSE count(*) * 1.00  END
                             FROM temp_calpoints1 t2
                             WHERE t2.processid = temp_calpoints1.processid)
              FROM  process p 
              WHERE p.processid = temp_calpoints1.processid) 

结果:

| ORDERID | PROCESSID | UID | ORDERVALUE | PERWET |
---------------------------------------------------
|       1 |         1 |   1 |      10000 |   3.33 | 
|       1 |         1 |   2 |      10000 |   3.33 |
|       1 |         1 |   3 |      10000 |   3.33 | 
于 2013-03-20T06:48:24.143 回答
0

你可以试试这样

update t set perwet = ((select perwet from process where processid = t.processid)  
/(select count(uid) from temp_calpoints1 group by orderid,processid having processid = 
t.processid AND orderid=t.orderid )) from temp_calpoints1 t;

SQL小提琴

于 2013-03-20T06:59:02.113 回答