Unix Technical Forum

Need Sql statement

This is a discussion on Need Sql statement within the SQL Server forums, part of the Microsoft SQL Server category; --> --Table Create table tableName (Field1 int, Field2 int) Table Data Field1 Field2 1 10 2 10 3 10 1 ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 02:38 AM
radha
 
Posts: n/a
Default Need Sql statement


--Table
Create table tableName (Field1 int, Field2 int)

Table Data
Field1 Field2
1 10
2 10
3 10
1 20
2 20
3 30

I need Like a Sql statement like

1 10 (any separator) 20
2 10 20
3 10 30


thx a lot waiting for result



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 02:38 AM
Erland Sommarskog
 
Posts: n/a
Default Re: Need Sql statement

radha (pottua_radha@yahoo.co.in) writes:
> --Table
> Create table tableName (Field1 int, Field2 int)
>
> Table Data
> Field1 Field2
> 1 10
> 2 10
> 3 10
> 1 20
> 2 20
> 3 30
>
> I need Like a Sql statement like
>
> 1 10 (any separator) 20
> 2 10 20
> 3 10 30
>
>
> thx a lot waiting for result


It is not clear what result you are looking for. Do you want max or
min values? Or if there are 19 different values for Field1, do you want
all 19 values of Field2 in one row?

In the first case it's simple:

SELECT Field1, MIN(Field2), MAX(Field2)
FROM tableName
GROUP BY Field1

The recommendation for the second case is usually that you should to it
client-side, as SQL does not lend itself for this sort of thing. An
iterative solution is somewhat tedious to write, but it may be the best
way. I seem to recall that someone - I believe it was Anith Sen or
David Portas - a while back posted a set-based solution, which used an
auxilliary table with numbers from 1 to whatever number that is needed.


--
Erland Sommarskog, SQL Server MVP, sommar@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:38 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com