Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Build a query for below scnario

Author  Topic 

amit120k
Starting Member

16 Posts

Posted - 2013-08-02 : 08:00:50
tbl1
ItemType ItemID
----------------
a 1
b 2
c 3
tbl2
ItemType ItemCount
----------------
a NULL
b NULL

Take count of a and b from tbl1 and update that to tbl2 in single Query.

stepson
Aged Yak Warrior

545 Posts

Posted - 2013-08-02 : 08:17:26
[code]

update A

SET A.itemCount=B.cItemID

from tbl1 A
inner join
(
select itemType,count(itemID) as cItemID
from tbl1
group by itemType)B on A.itemType=B.itemType

[/code]


S

Ce-am pe mine am si-n dulap, cand ma-mbrac zici ca ma mut
sabinWeb
Go to Top of Page

stepson
Aged Yak Warrior

545 Posts

Posted - 2013-08-02 : 09:13:53
[code]
update A

SET A.itemCount=B.cItemID

from tbl2 as A
inner join
(
select itemType,count(itemID) as cItemID
from tbl1
group by itemType)B on A.itemType=B.itemType

[/code]

at A it's tbl2 not tbl1

Ce-am pe mine am si-n dulap, cand ma-mbrac zici ca ma mut
sabinWeb
Go to Top of Page

amit120k
Starting Member

16 Posts

Posted - 2013-08-03 : 13:01:16
Hey the solution worked for me..
thanks dude...
Go to Top of Page

stepson
Aged Yak Warrior

545 Posts

Posted - 2013-08-03 : 14:50:35
with welcome

Ce-am pe mine am si-n dulap, cand ma-mbrac zici ca ma mut
sabinWeb
Go to Top of Page
   

- Advertisement -