简体   繁体   English

SQL Server:显示一个基于其他行进行计算的新列

[英]SQL Server : show a new column that does calculation base on other rows

I'm using SQL Server, I have a table which I have simplified as the follow: 我正在使用SQL Server,我有一个表,将其简化如下:

item ----- order-----Active
-------------------------------    
a-------------1---------true
b-------------2---------false
c-------------3---------true
d-------------4---------true
e-------------5---------false
f-------------6---------true

I want to query that return the three columns and an extra column call new-order, which for each item it subtract one for every inactive(Active being false) items that has a order number lower than itself. 我想查询返回的三列和一个额外的列,称为new-order,对于每个项目,它为订单号低于其自身的每个非活动(活动为假)项目减去一个。 So the above table will become 因此上表将变为

item ----- ordering-----Active----------NewOrder
-------------------------------------------------    
a-------------1---------true----------1
b-------------2---------false----------2
c-------------3---------true----------2
d-------------4---------true----------3
e-------------5---------false----------4
f-------------6---------true----------4

My attempt: 我的尝试:

Select 
    g.item, g.ordering, g.active,  
    g.ordering - (Select Count(x.ordering)
                  from grocery As x
                  where x.ordering < g.ordering 
                    and x.active = false
                  group by x.ordering) As NewOrder
 From 
     grocery as g

which doesn't work because the subquery contain more than one row. 这不起作用,因为子查询包含多个行。 But I honestly don't have any idea how to approach this. 但老实说,我不知道如何解决这个问题。 Is this even possible using just subquery? 仅使用子查询是否有可能?

Appreciate any advice or help 感谢任何建议或帮助

Edit: 编辑:

In the database there are more than 6 rows, and not in the correct order. 在数据库中,有超过6行,并且没有正确的顺序。 Basically an item's new order is its own ordering# subtract the number of inactive items whose ordering# is lower than the item's ordering#. 基本上,商品的新订单是其自己的订单号#减去其订单号低于该商品的订单号的无效商品的数量。 Again inactive items is items whose active = false. 同样,非活动项目是活动= false的项目。

I think following query will give you the result you want, you can omit NULL values. 我认为以下查询将为您提供所需的结果,您可以省略NULL值。 You basically want the NEWORDER when active is true so you can omit NULL values for false activetrue时,您基本上需要NEWORDER ,因此可以将NULL值省略为false

select
a.*,
b.NEWORDER
from
(
select
*
from
test1
)
as a
left join
(
    select [order] as NO,ROW_NUMBER() over(order by [order]) as NEWORDER from test1 where active='true'
)
as b
on a.[order]=b.no

OUTPUT 输出值

 item   order   active  NEWORDER
-------------------------------------
a       1       true    1
b       2       false   NULL
c       3       true    2
d       4       true    3
e       5       false   NULL
f       6       true    4

Here is one way 这是一种方法

SELECT item,
       [order],
       active,
       NewOrder = Sum(t)OVER(ORDER BY [order]) 
FROM   (SELECT *,
               CASE WHEN Lag(Active)OVER(ORDER BY [order]) = 'false' THEN 0 ELSE 1 END AS t
        FROM   (VALUES ('a',1,'true' ),
                       ('b',2,'false' ),
                       ('c',3,'true' ),
                       ('d',4,'true' ),
                       ('e',5,'false' ),
                       ('f',6,'true' )) tc (item, [order], Active)) a 

Result : 结果:

+------+-------+--------+----------+
| item | order | active | NewOrder |
+------+-------+--------+----------+
| a    |     1 | true   |        1 |
| b    |     2 | false  |        2 |
| c    |     3 | true   |        2 |
| d    |     4 | true   |        3 |
| e    |     5 | false  |        4 |
| f    |     6 | true   |        4 |
+------+-------+--------+----------+

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

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