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 2008 Forums
 Transact-SQL (2008)
 How to roll rows into one raw

Author  Topic 

emyk
Yak Posting Veteran

57 Posts

Posted - 2011-12-26 : 12:57:13
I have a query that returns multiple rows and What I am trying to do here is that using the returned rows to create a new table and insert a raw (1). My approach is that to get the duplicate count by col3 (col3 & col1 are always same value)and then divide the sum of col3 by the count, but I am still struggling to implement my approach. Any suggestions on how to better address this?


Table1
col1 col2 col3
car 22 345
car 34 345
car 234 345

Desired Table
col1 col2
car 345

edit: moved to proper forum

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-12-26 : 13:10:06
You can use the DISTINCT keyword to remove the duplicates

insert into NewTable
(col1, col2)
select distinct col1, col3 from Table1
Go to Top of Page

emyk
Yak Posting Veteran

57 Posts

Posted - 2011-12-26 : 13:22:46
Thank You Sunitabeck. Distinct did all the trick.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-12-26 : 17:14:32
YVW;
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-12-27 : 08:00:34
Why do you want to have a new table? You could create a view for this

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -