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)
 Need Help with SQL Query

Author  Topic 

bharatsql
Starting Member

22 Posts

Posted - 2006-04-03 : 16:34:31
I need a help with SQl query where based on the primary_key (Eship_ID) & status flag (Status_Fl) the records are colapsed into single record & are appended.

Here is what the source data looks like:

Eship_ID Broker_Dealer Status_FL

134084 A.G. Edwards & Sons, Inc Available
134084 All Available
134084 Banc of America Investment Services Inc Not-Available
134084 Citicorp Investment Services Available
134084 Citigroup Global Markets, Inc. Available

While o/p should look like:

Eship Id Sales Channel Exclude sales Channel
(If St_Fl= Available) (If Status_Fl= Not-Available)
134084 A.G. Edwards & Sons, Inc,All Banc of America Investment Services Inc

Need it urgently!!!

Thanks

timmy
Master Smack Fu Yak Hacker

1242 Posts

Posted - 2006-04-03 : 19:09:50
How do you determine which one of the 'available' items will be in the second column?
A starting point:
SELECT T.EshipID, (SELECT TOP 1 Broker_Dealer FROM myTable WHERE Eship_ID = T.Eship_ID AND Status_FL = 'Available'),
(SELECT TOP 1 Broker_Dealer FROM myTable WHERE Eship_ID = T.Eship_ID AND Status_FL <> 'Available')
FROM myTable T
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-04-03 : 20:21:08
Is this want you want ?
create table table1
(
Eship_ID int,
Broker_Dealer varchar(50),
Status_FL varchar(20)
)

insert into table1
select 134084, 'A.G. Edwards & Sons, Inc', 'Available' union all
select 134084, 'All', 'Available' union all
select 134084, 'Banc of America Investment Services Inc', 'Not-Available' union all
select 134084, 'Citicorp Investment Services', 'Available' union all
select 134084, 'Citigroup Global Markets, Inc.', 'Available'
go

create function concatBroker
(
@Eship_ID int,
@Status_FL varchar(20)
)
RETURNS varchar(1000)
as
begin
declare @Broker_Dealer varchar(1000)

select @Broker_Dealer = ''
select @Broker_Dealer = @Broker_Dealer + Broker_Dealer + ', '
from table1
where Eship_ID = @Eship_ID
and Status_FL = @Status_FL
order by Broker_Dealer
select @Broker_Dealer= left(@Broker_Dealer, len(@Broker_Dealer) - 2)

return @Broker_Dealer
end
go

select Eship_ID, dbo.concatBroker(Eship_ID, 'Available'), dbo.concatBroker(Eship_ID, 'Not-Available')
from table1
group by Eship_ID

go
drop function concatBroker
go
drop table table1




KH

Choice is an illusion, created between those with power, and those without.
Go to Top of Page

bharatsql
Starting Member

22 Posts

Posted - 2006-04-04 : 09:16:04
Hi Timmy,
What I need is to concat the result set into two columns based on the Eshipid & Status_Fl. So that there should be single row per eship_id.

Thanks
Go to Top of Page

bharatsql
Starting Member

22 Posts

Posted - 2006-04-04 : 09:18:24
Hi Khtan,
I need something like that but the (insert) values are not static as they shall be changing. Also, based on the data provided above what I need is to concat the result set into two columns based on the Eshipid & Status_Fl. So that there should be a single row per eship_id.

Thanks
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-04-04 : 09:41:08
quote:
Originally posted by bharatsql

Hi Khtan,
I need something like that but the (insert) values are not static as they shall be changing. Also, based on the data provided above what I need is to concat the result set into two columns based on the Eshipid & Status_Fl. So that there should be a single row per eship_id.

Thanks


Have you run the script ?

The "insert into table1 select .." is just for creating some sample data for testing of the script.

Yes there are 2 columns based on eshipid and status_fl and there is a single row per eship_id.



KH


Go to Top of Page
   

- Advertisement -