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 2000 Forums
 SQL Server Development (2000)
 If else statement

Author  Topic 

oahu9872
Posting Yak Master

112 Posts

Posted - 2005-08-09 : 09:39:54
I have a column in my table called "zone", and the value in each record is either 1, 2, 3, 4, or 5. i need to write a sql statement that will print out each zone number only once, followed by all records with that zone, without repeating the zone number . . .

Zone 1
record
record
record
Zone 2
record
record

I would appreciate any help I could get. Thanks.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-09 : 09:42:46
Can you give some sample data with expected result?

Madhivanan

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

oahu9872
Posting Yak Master

112 Posts

Posted - 2005-08-09 : 09:50:16
Let's say I have a table with 3 columns: event, location, zone...

Columns = Event, Location, Zone
Record 1 = A, New York, 1
Record 2 = B, Boston, 1
Record 3 = C, Chicago, 2
Record 4 = D, Detroit, 2

I want the output to be something like . . .

1, A, NewYork, B, Boston, 2, C, Chicago, D, Detroit

So it prints out the zone ID (1 or 2) one time followed by each record in that zone without repeating a zone ID until it comes to a new one.

I figure I need a loop with some if..else and maybe a case statement. I'm kind of lost on this one.

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-09 : 09:51:31
Refer this
http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx?Pending=true

Madhivanan

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

oahu9872
Posting Yak Master

112 Posts

Posted - 2005-08-09 : 09:53:57
That link looks like it will get me started. Thanks for the help.
Go to Top of Page

oahu9872
Posting Yak Master

112 Posts

Posted - 2005-08-09 : 11:04:18
Madhivanan,

The code from that link was exactly what I needed. It works like it should now. Thanks.
Go to Top of Page
   

- Advertisement -