用于分割的SQL查询

时间:2013-03-20 05:11:55

标签: sql sql-server

我写过这个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 个答案:

答案 0 :(得分:0)

update temp_calpoints1  set perwet = (select perwet/count(uid)
  from process 
  where 
  processid=temp_calpoints1.processid group by uid)

答案 1 :(得分: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

答案 2 :(得分:0)

类似的东西:

<强> SQLFIDDLEExample

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 | 

答案 3 :(得分: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 Fiddle