简体   繁体   English

基于SQL中的行号求和

[英]Sum based on row number in SQL

Sorry if the title is inappropriate.Let me explain what am trying to do. 对不起,如果标题不合适。让我解释一下我想做什么。 I have a calender table which i used for fetching data from my sales table. 我有一个日历表,用于从我的销售表中获取数据。 Here is the resulting table. 这是结果表。

SoldQty  StandardDate   RowNum
1        2013-10-30     1
5        2013-10-31     2
1        2013-10-30     3
1        2013-10-31     4
4        2013-10-30     5
1        2013-10-31     6
2        2013-10-31     8
1        2013-10-31     9

What i need is the sum based on row number i need the sum of values of first three rows,then the next three and the last 3. Is there a way to achieve this or should i insert this into a temp table and write select statements based on the row number. 我需要的是基于行号的总和我需要前三行的值的总和,然后是接下来的三行和最后一行3.有没有办法实现这个或者我应该将它插入临时表并写入select语句基于行号。 Thanks in advance 提前致谢

Update: 更新:

Expected output should be like this 预期的输出应该是这样的

Sold 
1.4   ->(sum of 1 to 3)*.20
1.8   ->(sum of 4 to 6)*.30 
2     ->(sum of 7 to 9)*.50 

or just 要不就

  Sold 
  5.2 -> sum of all the above values

When you need to group result, you must have a group to do that ;-). 当您需要对结果进行分组时,您必须有一个组才能这样做;-)。

For you case you can use the column RowNum . 对于您的情况,您可以使用RowNum列。 The trick is to translate the sequence into grouped values. 诀窍是将序列转换为分组值。

1 - 0
2 - 0
3 - 0

4 - 1
5 - 1
6 - 1

This is quite easy to do with math. 这对数学很容易。 (RowNum -1 ) / 3 will do the trick. (RowNum -1 ) / 3将成功。

So the query for you is 所以你的查询是

select count(SoldQty) from table group by (RowNum -1 ) / 3

The query below will give you the sums by categories you described: 下面的查询将为您提供您所描述的类别的总和:

DECLARE @maxRowNumber INT

SELECT @maxRowNumber = MAX(RowNum)
FROM   Data;

WITH Sums(Description, Total)
     AS (SELECT 'Sum of 1 to 3',
                SUM(SoldQty) * .20
         FROM   Data
         WHERE  RowNum <= 3
         UNION ALL
         SELECT 'Sum of 4 to 6',
                SUM(SoldQty) * .30
         FROM   Data
         WHERE  RowNum >= 4
                AND RowNum <= 6
         UNION ALL
         SELECT 'Sum of '
                + CAST(@maxRowNumber-3 AS VARCHAR(10))
                + ' to '
                + CAST(@maxRowNumber AS VARCHAR(10)),
                SUM(SoldQty) * .30
         FROM   Data
         WHERE  RowNum >= @maxRowNumber - 3)
SELECT *
FROM   Sums 

To get the sum of the values from categories, replace * with SUM(Total) 要从类别中获取值的总和,请将*替换为SUM(Total)

The final query you are looking for is: 您要查找的最终查询是:

select 
   sum( SoldQty * case ( ( [numrow]-1) /3) 
                  when 0 then 0.2 
                  when 1 then 0.3 
                  when 2 then 0.5 
                  end ) as result
   from [yourCalendarTable]

Notice than ( [numrow]-1) /3 is an integer but a decimal because you operate with integers. 注意比( [numrow]-1) /3是一个整数但是十进制,因为你用整数操作。

Take count(*) and store it in one int variable. 取count(*)并将其存储在一个int变量中。 then use the method "danihp" said above. 然后使用上面说的“danihp”方法。 OR Add one more column with identity take Max(identity_field) then use the same method. 或者再添加一个带有身份的列(Max(identity_field)),然后使用相同的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM