Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have 2 Tables as buss and line containing data as :
SQL
buss (busSrno, BusName). busSrno is PK.
1  bus1
2  bus2
3  bus3

line (lineNo, Frombus, Tobus) : here Frombus and Tobus columns will contain values of busSrno from buss table. lineNo is PK.
1  1  2
2  1  3
3  2  3
4  3  2


Now I have the following Query :
SQL
select concat(
       (select BusName as 'frombusName' from buss where busSrno in    (select frombus from line)), '-',
       (select BusName as 'tobusName' from buss where busSrno in 
(select tobus from line))
             ) as LineName;


It shows Following ERROR as : Sub-query returns more than 1 row.
Expected o/p : Under LineName column it will show as :
SQL
LineName

  bus1-bus2
  bus1-bus3
  bus2-bus3
  bus3-bus2


Kindly let me know the solution.

Thanks n Regards,
Posted
Updated 5-Jan-11 0:41am
v2

 
Share this answer
 
Comments
shwetavc30 5-Jan-11 7:40am    
Hi,

Thanks for ur help..
I got the Solution by Re-Writing Query as :-
select concat(b1.busname, '---', b2.busname) as 'LineName' from buss b1, buss b2, line
where b1.busSrno = line.FromBus and b2.busSrno = line.ToBus;

Thanks n Regards,
yaprig 6-Jan-11 0:31am    
:)
You have not completely understood idea behind purpose of IN query.

It is giving error because there are corrosponding many frombus in line table for corrosponding busSrno in buss.

further more concat function only need Value-joiner-Value type syntax to join but there is returning multiple value inspace of single value so that it will cause error.
 
Share this answer
 
Comments
shwetavc30 5-Jan-11 7:20am    
Kindly let me know another solution then for the mentioned expected result. I can do the same by writing stored procedure. But I wish to do this in a single query. Please let me know whether it is possible.
shwetavc30 5-Jan-11 7:39am    
Hi Hiren,

Thanks for ur help..
I got the Solution by Re-Writing Query as :-
select concat(b1.busname, '---', b2.busname) as 'LineName' from buss b1, buss b2, line
where b1.busSrno = line.FromBus and b2.busSrno = line.ToBus;

Thanks n Regards,
Hiren solanki 5-Jan-11 7:40am    
glad it helped you.
Hi all,

Thanks for ur help..
I got the Solution by Re-Writing Query as :-
select concat(b1.busname, '---', b2.busname) as 'LineName' from buss b1, buss b2, line
where b1.busSrno = line.FromBus and b2.busSrno = line.ToBus;

Thanks n Regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900