sql server - Counting occurrences for each row and update to new column -


i have table: enter image description here

i need count occurrence of same line in column closetime , write number of occurrences in last column count clause item , same account.

my single select concrete item , account

select closetime, count(*) closetime statement account = 2013169 , item = 'eurusd' group closetime having count(*) > 1 order count(*) desc 

it possible count in 1 update query?

depending on use case, not idea. better off creating "count" calculated column or leaving off base table entirely , creating view included count column. also, avoid trying have column name reserved word, count.

however, if want it, can in 1 query. make sure on same page, belive trying group closetime , account , item not included in group (so, counted if same close time , different item), want able update target account , item.

in case, like

with cte1 (select closetime, count(*) numcount dbo.[statement] group closetime) update dbo.[statement] set [count] = cte1.numcount dbo.[statement] sd join cte1 on sd.closetime = cte1.closetime sd.account = <your account num> , sd.item = <your item> 

edit:

if understand comments want correctly, use query like:

with cte1 (select closetime, count(*) numcount dbo.[statement] group closetime) select sd.*, --i advise not using * in production cte1.numcount [count] statement sd join cte1 on sd.closetime = cte1.closetime sd.account = <your account num> , sd.item = <your item> 

while use cte, single sql statement , display counts selected account num , item closetime.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

c++ - Accessing inactive union member and undefined behavior? -

php - Get uncommon values from two or more arrays -